mirror of https://github.com/python/cpython.git
Tests for @abstractproperty by Jeffrey Yasskin.
(The previous changes to abc.py were also by him). Put back a comment about using super() for properties (I didn't realize this worked).
This commit is contained in:
parent
46334cdae8
commit
70d2b890de
|
@ -30,6 +30,8 @@ class abstractproperty(property):
|
||||||
Requires that the metaclass is ABCMeta or derived from it. A
|
Requires that the metaclass is ABCMeta or derived from it. A
|
||||||
class that has a metaclass derived from ABCMeta cannot be
|
class that has a metaclass derived from ABCMeta cannot be
|
||||||
instantiated unless all of its abstract properties are overridden.
|
instantiated unless all of its abstract properties are overridden.
|
||||||
|
The abstract properties can be called using any of the the normal
|
||||||
|
'super' call mechanisms.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
|
|
|
@ -19,9 +19,25 @@ def foo(self): pass
|
||||||
def bar(self): pass
|
def bar(self): pass
|
||||||
self.assertEqual(hasattr(bar, "__isabstractmethod__"), False)
|
self.assertEqual(hasattr(bar, "__isabstractmethod__"), False)
|
||||||
|
|
||||||
def test_abstractmethod_integration(self):
|
def test_abstractproperty_basics(self):
|
||||||
|
@abc.abstractproperty
|
||||||
|
def foo(self): pass
|
||||||
|
self.assertEqual(foo.__isabstractmethod__, True)
|
||||||
|
def bar(self): pass
|
||||||
|
self.assertEqual(hasattr(bar, "__isabstractmethod__"), False)
|
||||||
|
|
||||||
class C(metaclass=abc.ABCMeta):
|
class C(metaclass=abc.ABCMeta):
|
||||||
@abc.abstractmethod
|
@abc.abstractproperty
|
||||||
|
def foo(self): return 3
|
||||||
|
class D(C):
|
||||||
|
@property
|
||||||
|
def foo(self): return super().foo
|
||||||
|
self.assertEqual(D().foo, 3)
|
||||||
|
|
||||||
|
def test_abstractmethod_integration(self):
|
||||||
|
for abstractthing in [abc.abstractmethod, abc.abstractproperty]:
|
||||||
|
class C(metaclass=abc.ABCMeta):
|
||||||
|
@abstractthing
|
||||||
def foo(self): pass # abstract
|
def foo(self): pass # abstract
|
||||||
def bar(self): pass # concrete
|
def bar(self): pass # concrete
|
||||||
self.assertEqual(C.__abstractmethods__, {"foo"})
|
self.assertEqual(C.__abstractmethods__, {"foo"})
|
||||||
|
@ -35,7 +51,7 @@ def foo(self): pass
|
||||||
self.assertEqual(E.__abstractmethods__, set())
|
self.assertEqual(E.__abstractmethods__, set())
|
||||||
E() # now foo is concrete, too
|
E() # now foo is concrete, too
|
||||||
class F(E):
|
class F(E):
|
||||||
@abc.abstractmethod
|
@abstractthing
|
||||||
def bar(self): pass # abstract override of concrete
|
def bar(self): pass # abstract override of concrete
|
||||||
self.assertEqual(F.__abstractmethods__, {"bar"})
|
self.assertEqual(F.__abstractmethods__, {"bar"})
|
||||||
self.assertRaises(TypeError, F) # because bar is abstract now
|
self.assertRaises(TypeError, F) # because bar is abstract now
|
||||||
|
|
Loading…
Reference in New Issue