def test_attribute_descriptor1(): assert not hasattr(attribute_descriptor('name', default=10), "check") class Foo: name = attribute_descriptor("name", default=10, content_type=int, nullable=False, constraint=lambda x: x % 10 == 0) x = Foo() assert x.name == 10 x.name = 30 assert x.name == 30 with pytest.raises(ValueError): x.name = 3 with pytest.raises(TypeError): x.name = "asdas" with pytest.raises(TypeError): x.name = 3.4 with pytest.raises(ValueError): x.name = None del x.name assert x.name == 10 # back to the default value
def test_attribute_descriptor1(): assert not hasattr(attribute_descriptor('name', default=10), "check") class Foo: name = attribute_descriptor("name", default=10, content_type=int, nullable=False, constraint=lambda x: x%10==0) x = Foo() assert x.name == 10 x.name = 30 assert x.name == 30 with pytest.raises(ValueError): x.name = 3 with pytest.raises(TypeError): x.name = "asdas" with pytest.raises(TypeError): x.name = 3.4 with pytest.raises(ValueError): x.name = None del x.name assert x.name==10 # back to the default value
class Foo: a = attribute_descriptor('a', content_type=int, nullable=False)
class Foo: x = attribute_descriptor("x", default=10, content_type=(int, float), nullable=True)
class Foo: a = attribute_descriptor("name", default=10, content_type=int, nullable=True, constraint=lambda x: x % 10 == 0)