当前位置: 首页>>代码示例>>Python>>正文


Python validators.instance_of方法代码示例

本文整理汇总了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" 
开发者ID:python-attrs,项目名称:attrs,代码行数:19,代码来源:test_dunders.py

示例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) 
开发者ID:COALAIP,项目名称:pycoalaip,代码行数:15,代码来源:test_models.py

示例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 
开发者ID:COALAIP,项目名称:pycoalaip,代码行数:14,代码来源:test_models.py

示例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) 
开发者ID:COALAIP,项目名称:pycoalaip,代码行数:15,代码来源:test_models.py

示例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) 
开发者ID:genomoncology,项目名称:related,代码行数:17,代码来源:_init_fields.py

示例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)) 
开发者ID:python-attrs,项目名称:attrs,代码行数:16,代码来源:test_funcs.py


注:本文中的attr.validators.instance_of方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。