Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
update_abstractmethods now ignores non-abstract classes
  • Loading branch information
ben avrahami committed Oct 3, 2020
commit 4f3d8469c2afec3bfcae11b0e6502f8d3c44bf46
2 changes: 1 addition & 1 deletion Doc/library/abc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ The :mod:`abc` module also provides the following functions:

If *cls* has any subclasses, raises a :exc:`RuntimeError`.

If *cls* is not an instance of ABCMeta, raises a :exc:`TypeError`.
If *cls* is not an instance of ABCMeta, does nothing.

.. versionadded:: 3.10

Expand Down
6 changes: 3 additions & 3 deletions Lib/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ class after this function is called.

If cls is has any subclasses, raises a RuntimeError.

If cls is not an instance of ABCMeta, raises a TypeError.
If cls is not an instance of ABCMeta, does nothing.
"""
if not hasattr(cls, '__abstractmethods__'):
# We check for __abstractmethods__ here because cls might by a C
# implementation or a python implementation (especially during
# testing), and we want to handle both cases.
raise TypeError('cls must be an abstract class or subclass of an abstract'
' class')
return cls

if cls.__subclasses__():
raise RuntimeError("cannot update abstract methods of class after"
" subclassing")
Expand Down
4 changes: 1 addition & 3 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,9 +993,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
cls.__doc__ = (cls.__name__ +
str(inspect.signature(cls)).replace(' -> None', ''))

# Update the abstract methods of the class, if it is abstract.
if isinstance(cls, abc.ABCMeta):
abc.update_abstractmethods(cls)
abc.update_abstractmethods(cls)

return cls

Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,9 @@ def updated_foo(self):
pass

A.foo = updated_foo
self.assertRaises(TypeError, abc.update_abstractmethods, A)
abc.update_abstractmethods(A)
A()
self.assertFalse(hasattr(A, '__abstractmethods__'))


class TestABCWithInitSubclass(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
A new function in abc: *update_abstractmethods* to re-calculate an abstract class's abstract status. In addition, *dataclass* and *total_ordering* have been changed to call this function. Finally, *total_ordering* was patched so that it would override abstract methods.
A new function in abc: *update_abstractmethods* to re-calculate an abstract class's abstract status. In addition, *dataclass* and *total_ordering* have been changed to call this function. Finally, *total_ordering* was patched so that it would override abstract methods defined in superclasses.