New submission from Antony Lee <[email protected]>:
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()).
Perhaps a `__class__` cell could even be set up for *all* methods defined in
dynamically created subclasses (i.e. whose `__qualname__` includes
`.<locals>.`), to help with introspection?
----------
components: Library (Lib)
messages: 355472
nosy: Antony.Lee
priority: normal
severity: normal
status: open
title: inspect.getdoc could examine the __class__ cell for dynamically
generated subclasses
versions: Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue38603>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com