-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix/enhance stubgen docstrings for functions, properties and types #16134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it necessitate changing tests?
mypy/stubgenc.py
Outdated
if is_static_property(obj): | ||
trailing_comment = " # read-only" if readonly else "" | ||
static_properties.append(f"{name}: ClassVar[{inferred}] = ...{trailing_comment}") | ||
if include_docstrings and docstr: | ||
static_properties.extend(f"{docstr}".split("\n")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why format a string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, my mistake. I will replace unnecessary formats.
mypy/stubgenc.py
Outdated
else: # regular property | ||
if readonly: | ||
ro_properties.append("@property") | ||
ro_properties.append(f"def {name}(self) -> {inferred}: ...") | ||
if include_docstrings and docstr: | ||
ro_properties.extend(f" {docstr}".split("\n")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to indent after splitting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, right.
def my_func(self):
'''docstrings here'''
This comment has been minimized.
This comment has been minimized.
9a18856
to
07df68d
Compare
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Any chance this can get merged in? I'd really like to test it out. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put in a guard against non-str docstrings? I know it shouldn't happen but it's possible at runtime, and I'd rather not have stubgen crash on any code.
>>> def f(): pass
...
>>> f.__doc__ = 42
>>> f.__doc__
42
No need to handle them specially, just ignore all docstrings that aren't instances of str.
Yes, sure. I Will do. |
Fixes #16114
Outputs docstrings for function, properties and types provided by extension modules to the stub file using stubgen.
Note: functions with overloaded signatures are not stripped.