Skip to content

bpo-38603: Inherit docstrings in dynamically generated subclasses if possible#16957

Open
anntzer wants to merge 1 commit intopython:mainfrom
anntzer:getdoc-dynamic-subclass
Open

bpo-38603: Inherit docstrings in dynamically generated subclasses if possible#16957
anntzer wants to merge 1 commit intopython:mainfrom
anntzer:getdoc-dynamic-subclass

Conversation

@anntzer
Copy link
Copy Markdown
Contributor

@anntzer anntzer commented Oct 27, 2019

Currently, inspect.getdoc() fails to inherit docstrings in dynamically
generated subclasses, such as

class Base:
    def method(self): "some docstring"

def make_subclass():
    class subclass(Base):
        def method(self): return super().method()
    return subclass

subclass = make_subclass()
inspect.getdoc(subclass.method)  # => returns None

because inspect._findclass() tries to find the base
class by parsing subclass.method.__qualname__ which is
"make_subclass.<locals>.subclass.method" and chokes over
.<locals>..

In the case where the method does rely on super(), there is another
way we can go back to the "owning" class of the method: by looking up
the contents of the __class__ cell (which is set up to make 0-arg
super()). This approach is implemented by this PR.

Perhaps a __class__ cell could even be set up (in a separate patch)
for all methods defined in dynamically created subclasses (i.e. whose
__qualname__ includes .<locals>.), to help with introspection?

https://bugs.python.org/issue38603

Copy link
Copy Markdown
Member

@isidentical isidentical 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 add a news entry

@anntzer anntzer force-pushed the getdoc-dynamic-subclass branch from 404ab04 to 58daf31 Compare November 24, 2019 20:58
@anntzer
Copy link
Copy Markdown
Contributor Author

anntzer commented Nov 24, 2019

done

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 7, 2026

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 7, 2026
…possible.

Currently, `inspect.getdoc()` fails to inherit docstrings in dynamically
generated subclasses, such as
```
class Base:
    def method(self): "some docstring"

def make_subclass():
    class subclass(Base):
        def method(self): return super().method()
    return subclass

subclass = make_subclass()
inspect.getdoc(subclass.method)  # => returns None
```
because `inspect._findclass()` tries to find the base
class by parsing `subclass.method.__qualname__` which is
`"make_subclass.<locals>.subclass.method"` and chokes over
`.<locals>.`.

In the case where the method does rely on `super()`, there is another
way we can go back to the "owning" class of the method: by looking up
the contents of the `__class__` cell (which is set up to make 0-arg
super()).  This approach is implemented by this PR.

Perhaps a `__class__` cell could even be set up (in a separate patch)
for *all* methods defined in dynamically created subclasses (i.e. whose
`__qualname__` includes `.<locals>.`), to help with introspection?
@anntzer anntzer force-pushed the getdoc-dynamic-subclass branch from 58daf31 to ce92190 Compare May 5, 2026 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants