Commit ce92190
committed
bpo-38603: Inherit docstrings in dynamically generated subclasses if 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?1 parent ffb543d commit ce92190
4 files changed
Lines changed: 31 additions & 3 deletions
File tree
- Lib
- test/test_inspect
- Misc/NEWS.d/next/Library
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
697 | 697 | | |
698 | 698 | | |
699 | 699 | | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
700 | 706 | | |
701 | 707 | | |
702 | 708 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
629 | 629 | | |
630 | 630 | | |
631 | 631 | | |
632 | | - | |
| 632 | + | |
| 633 | + | |
633 | 634 | | |
634 | 635 | | |
635 | 636 | | |
| |||
649 | 650 | | |
650 | 651 | | |
651 | 652 | | |
652 | | - | |
| 653 | + | |
| 654 | + | |
653 | 655 | | |
654 | 656 | | |
655 | 657 | | |
| |||
662 | 664 | | |
663 | 665 | | |
664 | 666 | | |
665 | | - | |
| 667 | + | |
| 668 | + | |
666 | 669 | | |
667 | 670 | | |
668 | 671 | | |
| |||
697 | 700 | | |
698 | 701 | | |
699 | 702 | | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
700 | 707 | | |
701 | 708 | | |
702 | 709 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
0 commit comments