mirror of
https://github.com/python/cpython.git
synced 2025-08-27 20:25:18 +00:00
bpo-41905: Add abc.update_abstractmethods() (GH-22485)
This function recomputes `cls.__abstractmethods__`. Also update `@dataclass` to use it.
This commit is contained in:
parent
a8bf44d049
commit
bef7d299eb
7 changed files with 256 additions and 5 deletions
|
@ -4,6 +4,7 @@
|
|||
|
||||
from dataclasses import *
|
||||
|
||||
import abc
|
||||
import pickle
|
||||
import inspect
|
||||
import builtins
|
||||
|
@ -3332,6 +3333,42 @@ class TestReplace(unittest.TestCase):
|
|||
|
||||
## replace(c, x=5)
|
||||
|
||||
class TestAbstract(unittest.TestCase):
|
||||
def test_abc_implementation(self):
|
||||
class Ordered(abc.ABC):
|
||||
@abc.abstractmethod
|
||||
def __lt__(self, other):
|
||||
pass
|
||||
|
||||
@abc.abstractmethod
|
||||
def __le__(self, other):
|
||||
pass
|
||||
|
||||
@dataclass(order=True)
|
||||
class Date(Ordered):
|
||||
year: int
|
||||
month: 'Month'
|
||||
day: 'int'
|
||||
|
||||
self.assertFalse(inspect.isabstract(Date))
|
||||
self.assertGreater(Date(2020,12,25), Date(2020,8,31))
|
||||
|
||||
def test_maintain_abc(self):
|
||||
class A(abc.ABC):
|
||||
@abc.abstractmethod
|
||||
def foo(self):
|
||||
pass
|
||||
|
||||
@dataclass
|
||||
class Date(A):
|
||||
year: int
|
||||
month: 'Month'
|
||||
day: 'int'
|
||||
|
||||
self.assertTrue(inspect.isabstract(Date))
|
||||
msg = 'class Date with abstract method foo'
|
||||
self.assertRaisesRegex(TypeError, msg, Date)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue