bpo-41905: Add abc.update_abstractmethods() (GH-22485)

This function recomputes `cls.__abstractmethods__`.
Also update `@dataclass` to use it.
This commit is contained in:
Ben Avrahami 2020-10-06 20:40:50 +03:00 committed by GitHub
parent a8bf44d049
commit bef7d299eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 256 additions and 5 deletions

View file

@ -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()