This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: inspect.getdoc could examine the __class__ cell for dynamically generated subclasses
Type: Stage: patch review
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Antony.Lee, yselivanov
Priority: normal Keywords: patch

Created on 2019-10-27 15:24 by Antony.Lee, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 16957 open Antony.Lee, 2019-10-27 15:28
Messages (1)
msg355472 - (view) Author: Antony Lee (Antony.Lee) * Date: 2019-10-27 15:24
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?
History
Date User Action Args
2022-04-11 14:59:22adminsetgithub: 82784
2019-11-24 20:44:00BTaskayasetnosy: + yselivanov
2019-10-27 15:28:37Antony.Leesetkeywords: + patch
stage: patch review
pull_requests: + pull_request16485
2019-10-27 15:24:34Antony.Leecreate