def test_descriptors_with_abstractmethod(self):
class C(metaclass=abc.ABCMeta):
@property
@abc.abstractmethod
def foo(self): return 3
@foo.setter
@abc.abstractmethod
def foo(self, val): pass
self.assertRaises(TypeError, C)
class D(C):
@C.foo.getter
def foo(self): return super().foo
self.assertRaises(TypeError, D)
class E(D):
@D.foo.setter
def foo(self, val): pass
self.assertEqual(E().foo, 3)
# check that the property's __isabstractmethod__ descriptor does the
# right thing when presented with a value that fails truth testing:
class NotBool(object):
def __nonzero__(self):
raise ValueError()
__len__ = __nonzero__
with self.assertRaises(ValueError):
class F(C):
def bar(self):
pass
bar.__isabstractmethod__ = NotBool()
foo = property(bar)
评论列表
文章目录