Skip to content

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

Closed
wants to merge 0 commits into from

Conversation

lmbelo
Copy link

@lmbelo lmbelo commented Sep 17, 2023

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.

Copy link
Contributor

@ikonst ikonst left a 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"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why format a string?

Copy link
Author

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"))
Copy link
Contributor

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?

Copy link
Author

@lmbelo lmbelo Sep 17, 2023

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'''

@github-actions

This comment has been minimized.

@lmbelo lmbelo force-pushed the master branch 2 times, most recently from 9a18856 to 07df68d Compare September 26, 2023 17:46
@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@wrinch
Copy link

wrinch commented Oct 7, 2023

Any chance this can get merged in? I'd really like to test it out.

Copy link
Member

@JelleZijlstra JelleZijlstra left a 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.

@lmbelo
Copy link
Author

lmbelo commented Oct 8, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The "include-docstrings" option doesn't work
4 participants