Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,28 @@ def test_access_from_class(self):
def test_doc(self):
self.assertEqual(CachedCostItem.cost.__doc__, "The cost of the item.")

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

method = AbstractExpensiveCalculator.calculate
self.assertTrue(method.__isabstractmethod__)
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__'))

def test_missing_isabstractmethod_not_copied(self):
class ConcreteExpensiveCalculator(abc.ABC):
@functools.cached_property
def calculate(self):
return 42

method = ConcreteExpensiveCalculator.calculate
self.assertFalse(getattr(method, '__isabstractmethod__', False))
ConcreteExpensiveCalculator()


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:func:`functools.cached_property` now also copies the wrapped method's ``__isabstractmethod__`` attribute.