Kaydet (Commit) 70d2b890 authored tarafından Guido van Rossum's avatar Guido van Rossum

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).
üst 46334cda
...@@ -30,6 +30,8 @@ class abstractproperty(property): ...@@ -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,26 +19,42 @@ class TestABC(unittest.TestCase): ...@@ -19,26 +19,42 @@ class TestABC(unittest.TestCase):
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): pass # abstract def foo(self): return 3
def bar(self): pass # concrete
self.assertEqual(C.__abstractmethods__, {"foo"})
self.assertRaises(TypeError, C) # because foo is abstract
class D(C): class D(C):
def bar(self): pass # concrete override of concrete @property
self.assertEqual(D.__abstractmethods__, {"foo"}) def foo(self): return super().foo
self.assertRaises(TypeError, D) # because foo is still abstract self.assertEqual(D().foo, 3)
class E(D):
def foo(self): pass def test_abstractmethod_integration(self):
self.assertEqual(E.__abstractmethods__, set()) for abstractthing in [abc.abstractmethod, abc.abstractproperty]:
E() # now foo is concrete, too class C(metaclass=abc.ABCMeta):
class F(E): @abstractthing
@abc.abstractmethod def foo(self): pass # abstract
def bar(self): pass # abstract override of concrete def bar(self): pass # concrete
self.assertEqual(F.__abstractmethods__, {"bar"}) self.assertEqual(C.__abstractmethods__, {"foo"})
self.assertRaises(TypeError, F) # because bar is abstract now self.assertRaises(TypeError, C) # because foo is abstract
class D(C):
def bar(self): pass # concrete override of concrete
self.assertEqual(D.__abstractmethods__, {"foo"})
self.assertRaises(TypeError, D) # because foo is still abstract
class E(D):
def foo(self): pass
self.assertEqual(E.__abstractmethods__, set())
E() # now foo is concrete, too
class F(E):
@abstractthing
def bar(self): pass # abstract override of concrete
self.assertEqual(F.__abstractmethods__, {"bar"})
self.assertRaises(TypeError, F) # because bar is abstract now
def test_registration_basics(self): def test_registration_basics(self):
class A(metaclass=abc.ABCMeta): class A(metaclass=abc.ABCMeta):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment