| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f587f4d commit 1656941
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -639,6 +639,9 @@ Retrieving source code | |||
| 639 | 639 | .. versionchanged:: next | |
| 640 | 640 | Added parameters *inherit_class_doc* and *fallback_to_class_doc*. | |
| 641 | 641 | ||
| 642 | + Documentation strings on :class:`~functools.cached_property` | ||
| 643 | + objects are now inherited if not overriden. | ||
| 644 | + | ||
| 642 | 645 | ||
| 643 | 646 | .. function:: getcomments(object) | |
| 644 | 647 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -747,6 +747,12 @@ def _finddoc(obj, *, search_in_class=True): | |||
| 747 | 747 | cls = _findclass(obj.fget) | |
| 748 | 748 | if cls is None or getattr(cls, name) is not obj: | |
| 749 | 749 | return None | |
| 750 | + # Should be tested before ismethoddescriptor() | ||
| 751 | + elif isinstance(obj, functools.cached_property): | ||
| 752 | + name = obj.attrname | ||
| 753 | + cls = _findclass(obj.func) | ||
| 754 | + if cls is None or getattr(cls, name) is not obj: | ||
| 755 | + return None | ||
| 750 | 756 | elif ismethoddescriptor(obj) or isdatadescriptor(obj): | |
| 751 | 757 | name = obj.__name__ | |
| 752 | 758 | cls = obj.__objclass__ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,39 @@ | |||
| 1 | + from functools import cached_property | ||
| 2 | + | ||
| 3 | + # docstring in parent, inherited in child | ||
| 4 | + class ParentInheritDoc: | ||
| 5 | + @cached_property | ||
| 6 | + def foo(self): | ||
| 7 | + """docstring for foo defined in parent""" | ||
| 8 | + | ||
| 9 | + class ChildInheritDoc(ParentInheritDoc): | ||
| 10 | + pass | ||
| 11 | + | ||
| 12 | + class ChildInheritDefineDoc(ParentInheritDoc): | ||
| 13 | + @cached_property | ||
| 14 | + def foo(self): | ||
| 15 | + pass | ||
| 16 | + | ||
| 17 | + # Redefine foo as something other than cached_property | ||
| 18 | + class ChildPropertyFoo(ParentInheritDoc): | ||
| 19 | + @property | ||
| 20 | + def foo(self): | ||
| 21 | + """docstring for the property foo""" | ||
| 22 | + | ||
| 23 | + class ChildMethodFoo(ParentInheritDoc): | ||
| 24 | + def foo(self): | ||
| 25 | + """docstring for the method foo""" | ||
| 26 | + | ||
| 27 | + # docstring in child but not parent | ||
| 28 | + class ParentNoDoc: | ||
| 29 | + @cached_property | ||
| 30 | + def foo(self): | ||
| 31 | + pass | ||
| 32 | + | ||
| 33 | + class ChildNoDoc(ParentNoDoc): | ||
| 34 | + pass | ||
| 35 | + | ||
| 36 | + class ChildDefineDoc(ParentNoDoc): | ||
| 37 | + @cached_property | ||
| 38 | + def foo(self): | ||
| 39 | + """docstring for foo defined in child""" | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,6 +46,7 @@ | |||
| 46 | 46 | ||
| 47 | 47 | from test.test_inspect import inspect_fodder as mod | |
| 48 | 48 | from test.test_inspect import inspect_fodder2 as mod2 | |
| 49 | + from test.test_inspect import inspect_fodder3 as mod3 | ||
| 49 | 50 | from test.test_inspect import inspect_stringized_annotations | |
| 50 | 51 | from test.test_inspect import inspect_deferred_annotations | |
| 51 | 52 | ||
@@ -714,6 +715,25 @@ class B(A): | |||
| 714 | 715 | b.__doc__ = 'Instance' | |
| 715 | 716 | self.assertEqual(inspect.getdoc(b, fallback_to_class_doc=False), 'Instance') | |
| 716 | 717 | ||
| 718 | + def test_getdoc_inherited_cached_property(self): | ||
| 719 | + doc = inspect.getdoc(mod3.ParentInheritDoc.foo) | ||
| 720 | + self.assertEqual(doc, 'docstring for foo defined in parent') | ||
| 721 | + self.assertEqual(inspect.getdoc(mod3.ChildInheritDoc.foo), doc) | ||
| 722 | + self.assertEqual(inspect.getdoc(mod3.ChildInheritDefineDoc.foo), doc) | ||
| 723 | + | ||
| 724 | + def test_getdoc_redefine_cached_property_as_other(self): | ||
| 725 | + self.assertEqual(inspect.getdoc(mod3.ChildPropertyFoo.foo), | ||
| 726 | + 'docstring for the property foo') | ||
| 727 | + self.assertEqual(inspect.getdoc(mod3.ChildMethodFoo.foo), | ||
| 728 | + 'docstring for the method foo') | ||
| 729 | + | ||
| 730 | + def test_getdoc_define_cached_property(self): | ||
| 731 | + self.assertEqual(inspect.getdoc(mod3.ChildDefineDoc.foo), | ||
| 732 | + 'docstring for foo defined in child') | ||
| 733 | + | ||
| 734 | + def test_getdoc_nodoc_inherited(self): | ||
| 735 | + self.assertIsNone(inspect.getdoc(mod3.ChildNoDoc.foo)) | ||
| 736 | + | ||
| 717 | 737 | @unittest.skipIf(MISSING_C_DOCSTRINGS, "test requires docstrings") | |
| 718 | 738 | def test_finddoc(self): | |
| 719 | 739 | finddoc = inspect._finddoc | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + :func:`inspect.getdoc` now correctly returns an inherited docstring on | ||
| 2 | + :class:`~functools.cached_property` objects if none is given in a subclass. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments