GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-131116: Fix inspect._finddoc to work with cached_property objects. by lincolnj1 · Pull Request #131165 · python/cpython

Conversation

@lincolnj1

@lincolnj1 lincolnj1 commented Mar 12, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Calling inspect.getdoc on a cached_property that inherits a docstring and does not define one would return None rather than the inherited docstring. This is because inspect._finddoc would treat the cached_property obj as a methoddescriptor and encounter an AttributeError trying to access obj.__name__. This PR adds a special case for cached_properties in _finddoc so that it can find the inherited docstring.

  • Issue: inspect.getdoc() failing to return docstring for cached_property for subclass that overrides code but not docstring #131116

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
lincolnj1 and others added 6 commits March 12, 2025 10:41
@lincolnj1
attempted fix of issue python#131116. Test case failing on non change…
19d5c42
…d parts
@lincolnj1
Moved new test fodder to seperate file to fix errors
02ec2c1
@lincolnj1
Cleaned workspace and actually added the changed files this time
8786c3c
@lincolnj1
Merge branch 'python:main' into getdoc-fix
9fe818c
@lincolnj1
Seperated new tests into new test case
5772f7b
@lincolnj1
Merge branch 'getdoc-fix' of github.com:lincolnj1/cpython into getdoc…
f68119e
…-fix
@bedevere-app bedevere-app Bot added the awaiting review label Mar 12, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Mar 12, 2025
inspect.getdoc() failing to return docstring for cached_property for subclass that overrides code but not docstring #131116
Closed
@lincolnj1 lincolnj1 changed the title gh-131116: Fixed inspect._finddoc to work with cached_property objects. gh-131116: Fix inspect._finddoc to work with cached_property objects. Mar 12, 2025
@blurb-it
📜🤖 Added by blurb_it.
2406055
@scotscotmcc

scotscotmcc commented Mar 12, 2025

Copy link
Copy Markdown
Contributor

In Lib/test/test_inspect/inspect_fodder.py, the last class has a bunch of comments where the comment includes the specific line number, i.e. # line 95 is line 95 of the original file. The updates in this PR include adding lines to that file and so those comments are no longer on the lines that they reference.

I'm having trouble getting the test suite working locally to get a sense of if that matters at all, but I'm guessing that some inspect tests may be looking at those comments for some reason or another?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lincolnj1

lincolnj1 commented Mar 12, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

In Lib/test/test_inspect/inspect_fodder.py, the last class has a bunch of comments where the comment includes the specific line number, i.e. # line 95 is line 95 of the original file. The updates in this PR include adding lines to that file and so those comments are no longer on the lines that they reference.

I'm having trouble getting the test suite working locally to get a sense of if that matters at all, but I'm guessing that some inspect tests may be looking at those comments for some reason or another?

I believe I fully reverted that in my later commits. There should no longer be any changes to inspect_fodder.py. Which commit have you pulled?

Those comments are there to test the comment extractor part of inspect.py. After a bit of testing I felt adding new stuff to the fodder file required changing more hard coded numbers than I thought was worthwhile, so I made a new fodder file.

I'm also having trouble getting the test suite working after my last pull from main. Are you getting the same "cannot import name 'Union' from '_typing' (unknown location)" error?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@scotscotmcc

scotscotmcc commented Mar 12, 2025

Copy link
Copy Markdown
Contributor

ope, my bad, was looking at a commit and spent so long trying to get the devcontainer to work you had made new updates since then that I didn't look at.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

auvipy
auvipy suggested changes Mar 13, 2025
View reviewed changes

@auvipy auvipy left a comment

Copy link
Copy Markdown

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 please check the failing tests here please, Tests / Hypothesis tests on Ubuntu (pull_request) ? if they are relevant failures?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Mar 13, 2025
@lincolnj1
Merge branch 'python:main' into getdoc-fix
5f0d6d4
@lincolnj1

lincolnj1 commented Mar 13, 2025

Copy link
Copy Markdown
Contributor Author

can you please check the failing tests here please, Tests / Hypothesis tests on Ubuntu (pull_request) ? if they are relevant failures?

Syncing with main this afternoon seems to have fixed it. I'm not sure why it was failing in the first place, since it seems to test code that I didn't modify at all. The tests ran fine on my Ubuntu terminal before and after I submitted so that was a bit baffling to me.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Mar 13, 2025

Copy link
Copy Markdown
Member

The test is flaky I think (we've seen similar failures elsewhere)

lincolnj1 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lincolnj1

lincolnj1 commented Mar 24, 2025

Copy link
Copy Markdown
Contributor Author

@picnixz Do you have any thoughts on this pr? It's been a couple weeks and I'm hoping for core dev to look at it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Mar 24, 2025

Copy link
Copy Markdown
Member

I don't have time until the end of the week so I'm requesting myself a review

lincolnj1 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz
picnixz self-requested a review March 24, 2025 23:23
picnixz
picnixz reviewed Mar 30, 2025
View reviewed changes
Comment thread Lib/test/test_inspect/inspect_fodder3.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

picnixz
picnixz reviewed Mar 30, 2025
View reviewed changes
Comment thread Lib/test/test_inspect/test_inspect.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2025-03-12-18-57-10.gh-issue-131116.uTpwXZ.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@lincolnj1
Added more tests and versionchanged to Doc
d61ab34
picnixz
picnixz reviewed Apr 1, 2025
View reviewed changes
Comment thread Doc/library/inspect.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_inspect/inspect_fodder3.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_inspect/inspect_fodder3.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_inspect/inspect_fodder3.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_inspect/inspect_fodder3.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

lincolnj1 and others added 4 commits April 1, 2025 10:55
@lincolnj1 @picnixz
Update Doc/library/inspect.rst
4b74d4e
Co-authored-by: Bénédikt Tran <[email protected]>
@lincolnj1 @picnixz
Update Lib/test/test_inspect/inspect_fodder3.py
cfe3e99
Co-authored-by: Bénédikt Tran <[email protected]>
@lincolnj1 @picnixz
Update Lib/test/test_inspect/inspect_fodder3.py
fa92ad3
Co-authored-by: Bénédikt Tran <[email protected]>
@lincolnj1 @picnixz
Update Lib/test/test_inspect/inspect_fodder3.py
d83205c
Co-authored-by: Bénédikt Tran <[email protected]>
@lincolnj1 @picnixz
Update Lib/test/test_inspect/inspect_fodder3.py
c4764ff
Co-authored-by: Bénédikt Tran <[email protected]>
@picnixz
picnixz self-requested a review April 6, 2025 14:38
picnixz
picnixz reviewed Apr 6, 2025
View reviewed changes
Comment thread Lib/test/test_inspect/inspect_fodder3.py Outdated
def foo(self):
pass

# Redine foo as something other than cached_property

@picnixz picnixz Apr 6, 2025

Copy link
Copy Markdown
Member

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.

Suggested change
# Redine foo as something other than cached_property
# redefine foo as something other than cached_property

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_inspect/inspect_fodder3.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_inspect/inspect_fodder3.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_inspect/inspect_fodder3.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@lincolnj1
Fixed inspect_fodder3 comments and added new test cases
1b0471f
picnixz
picnixz approved these changes Apr 12, 2025
View reviewed changes

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

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.

Just some cosmetic nits.

@serhiy-storchaka is there something we need to be careful about here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_inspect/inspect_fodder3.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_inspect/inspect_fodder3.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_inspect/test_inspect.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_inspect/test_inspect.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_inspect/inspect_fodder3.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2025-03-12-18-57-10.gh-issue-131116.uTpwXZ.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Apr 12, 2025
@serhiy-storchaka
serhiy-storchaka self-requested a review April 12, 2025 16:35
@python-cla-bot

python-cla-bot Bot commented Apr 18, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Apr 18, 2025

Copy link
Copy Markdown
Member

I haven't looked at this PR yet, but note that there is a duplicate of this function in the pydoc module.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Apr 18, 2025

Copy link
Copy Markdown
Member

but note that there is a duplicate of this function in the pydoc module.

Good to know then. @lincolnj1 can you check also the pydoc module please?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka approved these changes Apr 18, 2025
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

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.

It seems to me that you went overboard with the tests (the simplest test would have been enough), but overall LGTM.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@picnixz

picnixz commented Apr 18, 2025

Copy link
Copy Markdown
Member

It seems to me that you went overboard with the tests (the simplest test would have been enough),

This is a trauma I had from maintaining Sphinx. There were so many edge cases because of inheritance. It also serves as a robust way to detect future possible regressions (but I agree that it's a bit of an overkill).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lincolnj1

lincolnj1 commented Apr 18, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

but note that there is a duplicate of this function in the pydoc module.

Good to know then. @lincolnj1 can you check also the pydoc module please?

Yes, it seems that one is also broken in the same way (see below). I'll take a look at fixing it. Should I open a new PR to fix that or keep working in this one?

Edit: I used the private _getdoc method here when I should've used the public getdoc, but the behavior is consistent using either method.

>>> import pydoc
>>> import inspect
>>> pydoc._getdoc(int)
"int([x]) -> integer\nint(x, base=10) -> integer\n\nConvert a number or string to an integer, or return 0 if no arguments\nare given.  If x is a number, return x.__int__().  For floating-point\nnumbers, this truncates towards zero.\n\nIf x is not a number or if base is given, then x must be a string,\nbytes, or bytearray instance representing an integer literal in the\ngiven base.  The literal can be preceded by '+' or '-' and be surrounded\nby whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.\nBase 0 means to interpret the base from the string as an integer literal.\n>>> int('0b100', base=0)\n4"
>>> inspect.getdoc(int)
"int([x]) -> integer\nint(x, base=10) -> integer\n\nConvert a number or string to an integer, or return 0 if no arguments\nare given.  If x is a number, return x.__int__().  For floating-point\nnumbers, this truncates towards zero.\n\nIf x is not a number or if base is given, then x must be a string,\nbytes, or bytearray instance representing an integer literal in the\ngiven base.  The literal can be preceded by '+' or '-' and be surrounded\nby whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.\nBase 0 means to interpret the base from the string as an integer literal.\n>>> int('0b100', base=0)\n4"
>>> inspect.getdoc(int) == pydoc._getdoc(int)
True
>>>
>>> from functools import cached_property
>>> class A:
...     @cached_property
...     def foo(self):
...         "this is the docstring for foo"
...         return 1
...
>>> class B(A):
...     @cached_property
...     def foo(self): ...
...
>>> pydoc._getdoc(B.foo)
>>> pydoc._getdoc(A.foo)
'this is the docstring for foo'
>>> pydoc._getdoc(B.foo) == pydoc._getdoc(A.foo)
False
>>>
>>>
>>>
>>> class MethodBase:
...     def foo(self):
...             "my method foo"
...
>>> class Child(MethodBase):
...     def foo(self): ...
...
>>> pydoc._getdoc(MethodBase.foo) == pydoc._getdoc(Child.foo) #method docstrings are inherited properly!
True
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Apr 18, 2025

Copy link
Copy Markdown
Member

If there are plans to backport this change, you need to fix the pydoc version too (it is just a copy). Otherwise, you may wait until #132686 has been resolved.

lincolnj1 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka and others added 4 commits November 12, 2025 11:34
@serhiy-storchaka
Merge branch 'main' into getdoc-fix
d4e37c3
@serhiy-storchaka @picnixz
Apply suggestions from code review
1e0cb4b
Co-authored-by: Bénédikt Tran <[email protected]>
@serhiy-storchaka @picnixz
Update Lib/test/test_inspect/test_inspect.py
a669d23
Co-authored-by: Bénédikt Tran <[email protected]>
@serhiy-storchaka @picnixz
Apply suggestions from code review
c510524
Co-authored-by: Bénédikt Tran <[email protected]>
@serhiy-storchaka
serhiy-storchaka enabled auto-merge (squash) November 12, 2025 09:42
@serhiy-storchaka
serhiy-storchaka merged commit 70748bd into python:main Nov 12, 2025
46 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Nov 12, 2025
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
@lincolnj1 @StanFromIreland
pythongh-131116: Fix inspect.getdoc() to work with cached_property ob…
1656941
…jects (pythonGH-131165)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes
@picnixz picnixz picnixz approved these changes
+1 more reviewer
@auvipy auvipy auvipy requested changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@lincolnj1 @scotscotmcc @picnixz @serhiy-storchaka @auvipy
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.