本文整理汇总了Python中attr.validators.instance_of方法的典型用法代码示例。如果您正苦于以下问题:Python validators.instance_of方法的具体用法?Python validators.instance_of怎么用?Python validators.instance_of使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类attr.validators
的用法示例。
在下文中一共展示了validators.instance_of方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_validator_others
# 需要导入模块: from attr import validators [as 别名]
# 或者: from attr.validators import instance_of [as 别名]
def test_validator_others(self, slots):
"""
Does not interfere when setting non-attrs attributes.
"""
C = make_class(
"C", {"a": attr.ib("a", validator=instance_of(int))}, slots=slots
)
i = C(1)
assert 1 == i.a
if not slots:
i.b = "foo"
assert "foo" == i.b
else:
with pytest.raises(AttributeError):
i.b = "foo"
示例2: test_model_immutable
# 需要导入模块: from attr import validators [as 别名]
# 或者: from attr.validators import instance_of [as 别名]
def test_model_immutable(model_data, model_type):
from attr import validators
from attr.exceptions import FrozenInstanceError
from coalaip.models import Model
model = Model(data=model_data, ld_type=model_type)
with raises(FrozenInstanceError):
model.data = {'other': 'other'}
with raises(FrozenInstanceError):
model.ld_type = 'other_type'
with raises(FrozenInstanceError):
model.ld_context = 'other_context'
with raises(FrozenInstanceError):
model.validator = validators.instance_of(str)
示例3: test_lazy_model_init
# 需要导入模块: from attr import validators [as 别名]
# 或者: from attr.validators import instance_of [as 别名]
def test_lazy_model_init(model_type):
from attr import validators
from coalaip.models import LazyLoadableModel
ld_context = 'ld_context'
validator = validators.instance_of(dict)
model = LazyLoadableModel(ld_type=model_type, ld_context=ld_context,
validator=validator)
assert model.loaded_model is None
assert model.ld_type == model_type
assert model.ld_context == ld_context
assert model.validator == validator
示例4: test_lazy_model_immutable
# 需要导入模块: from attr import validators [as 别名]
# 或者: from attr.validators import instance_of [as 别名]
def test_lazy_model_immutable(model_data, model_type):
from attr import validators
from attr.exceptions import FrozenInstanceError
from coalaip.models import Model, LazyLoadableModel
model = LazyLoadableModel(data=model_data, ld_type=model_type)
with raises(FrozenInstanceError):
model.loaded_model = Model(data={'other': 'other'}, ld_type='other_type')
with raises(FrozenInstanceError):
model.ld_type = 'other_type'
with raises(FrozenInstanceError):
model.ld_context = 'other_context'
with raises(FrozenInstanceError):
model.validator = validators.instance_of(str)
示例5: init_validator
# 需要导入模块: from attr import validators [as 别名]
# 或者: from attr.validators import instance_of [as 别名]
def init_validator(required, cls, *additional_validators):
"""
Create an attrs validator based on the cls provided and required setting.
:param bool required: whether the field is required in a given model.
:param cls: the expected class type of object value.
:return: attrs validator chained correctly (e.g. optional(instance_of))
"""
validator = validators.instance_of(cls)
if additional_validators:
additional_validators = list(additional_validators)
additional_validators.append(validator)
validator = composite(*additional_validators)
return validator if required else validators.optional(validator)
示例6: test_validator_failure
# 需要导入模块: from attr import validators [as 别名]
# 或者: from attr.validators import instance_of [as 别名]
def test_validator_failure(self):
"""
TypeError isn't swallowed when validation fails within evolve.
"""
@attr.s
class C(object):
a = attr.ib(validator=instance_of(int))
with pytest.raises(TypeError) as e:
evolve(C(a=1), a="some string")
m = e.value.args[0]
assert m.startswith("'a' must be <{type} 'int'>".format(type=TYPE))