bpo-38603: Inherit docstrings in dynamically generated subclasses if possible#16957
Open
anntzer wants to merge 1 commit intopython:mainfrom
Open
bpo-38603: Inherit docstrings in dynamically generated subclasses if possible#16957anntzer wants to merge 1 commit intopython:mainfrom
anntzer wants to merge 1 commit intopython:mainfrom
Conversation
404ab04 to
58daf31
Compare
Contributor
Author
|
done |
|
This PR is stale because it has been open for 30 days with no activity. |
…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?
58daf31 to
ce92190
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Currently,
inspect.getdoc()fails to inherit docstrings in dynamicallygenerated subclasses, such as
because
inspect._findclass()tries to find the baseclass 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 anotherway 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-argsuper()). 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