Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,10 @@ def __init__(self, func):
self.func = func
self.attrname = None
self.__doc__ = func.__doc__
try:
self.__isabstractmethod__ = func.__isabstractmethod__
except AttributeError:
pass
self.lock = RLock()

def __set_name__(self, owner, name):
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,17 @@ def test_access_from_class(self):
def test_doc(self):
self.assertEqual(CachedCostItem.cost.__doc__, "The cost of the item.")

def test_isabstractmethod(self):
class AbstractExpensiveCalculator(abc.ABC):
@functools.cached_property
@abc.abstractmethod
def calculate(self):
pass

self.assertTrue(getattr(AbstractExpensiveCalculator.calculate, '__isabstractmethod__', False))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.assertTrue(AbstractExpensiveCalculator.calculate.isabstractmethod) is enough.

with self.assertRaises(TypeError):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to (also) directly check the isabstractmethod attribute?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, added an assertion for that

AbstractExpensiveCalculator()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test on a method which doesn't have the __isabstractmethod__ attribute? I expect such test:

self.assertFalse(hasattr(AbstractExpensiveCalculator.calculate, '__isabstractmethod__'))


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Maintain wrapped method's __isabstractmethod__ in functools.cached_property

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest:

:func:`functools.cached_property` now also copies the wrapped method's ``__isabstractmethod__`` attribute.