Skip to content

bpo-40838 document getdoc, getmodule, getsourcefile in inspect returns None #30575

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

Merged
merged 6 commits into from
May 18, 2022

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Jan 13, 2022

CC @corona10 as my mentor.

I think that this can be backported to 3.9 and 3.10.

https://bugs.python.org/issue40838

@@ -503,6 +503,7 @@ Retrieving source code
If the documentation string for an object is not provided and the object is
a class, a method, a property or a descriptor, retrieve the documentation
string from the inheritance hierarchy.
Might return ``None`` for invalid docstrings.

Choose a reason for hiding this comment

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

This method returns None if the object doesn't have a docstring. I don't think a docstring even could be invalid.

I'd actually prefer this method returning an empty string if the object doesn't have a docstring. Needing to use doc = getdoc(x) or '' everytime is annoying but I can understand changing this would be a backwards incompatible change.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've updated this to be: Might return None for missing or invalid docstrings

I mean this case by invalid:

    if not isinstance(doc, str):
        return None

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

This piece of documentation very consistently uses the imperative mood ("return None" is an order directed to the function, telling the function what to do, while "might return None" is a description to the reader of what the function might do). I think it would be nice to maintain consistency with the rest of this piece of documentation 🙂

Additionally, the description for getdocstring doesn't use the word "docstring" at all currently, so it would be nice to stick to "documentation string" when describing that function 🙂

sobolevn and others added 2 commits January 14, 2022 12:05


.. function:: getsourcefile(object)

Return the name of the Python source file in which an object was defined. This
Return the name of the Python source file in which an object was defined
or ``None`` if no way can be identified to get the source. This
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
or ``None`` if no way can be identified to get the source. This
or ``None`` if it cannot be identified. This

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! But, I've copied this exact piece from inspect.py, I think it is better to keep it in sync (at least try to do that) 🙂

@sobolevn sobolevn closed this Jan 15, 2022
@sobolevn sobolevn reopened this Jan 15, 2022
Co-authored-by: Jelle Zijlstra <[email protected]>
@AlexWaygood AlexWaygood added the needs backport to 3.11 only security fixes label May 17, 2022
@AlexWaygood AlexWaygood requested a review from JelleZijlstra May 17, 2022 10:53
@JelleZijlstra JelleZijlstra self-assigned this May 17, 2022
@JelleZijlstra JelleZijlstra removed the needs backport to 3.9 only security fixes label May 18, 2022
@JelleZijlstra JelleZijlstra merged commit b86d783 into python:main May 18, 2022
@miss-islington
Copy link
Contributor

Thanks @sobolevn for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.11.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-92909 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label May 18, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 18, 2022
…t` returns `None` (pythonGH-30575)

Co-authored-by: Alex Waygood <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
(cherry picked from commit b86d783)

Co-authored-by: Nikita Sobolev <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 18, 2022
…t` returns `None` (pythonGH-30575)

Co-authored-by: Alex Waygood <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
(cherry picked from commit b86d783)

Co-authored-by: Nikita Sobolev <[email protected]>
miss-islington added a commit that referenced this pull request May 18, 2022
…t` returns `None` (GH-30575)

Co-authored-by: Alex Waygood <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
(cherry picked from commit b86d783)

Co-authored-by: Nikita Sobolev <[email protected]>
@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label May 18, 2022
@bedevere-bot
Copy link

GH-92910 is a backport of this pull request to the 3.10 branch.

miss-islington added a commit that referenced this pull request May 18, 2022
…t` returns `None` (GH-30575)

Co-authored-by: Alex Waygood <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
(cherry picked from commit b86d783)

Co-authored-by: Nikita Sobolev <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants