Python attr.validators.instance_of() Examples

The following are 6 code examples of attr.validators.instance_of(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module attr.validators , or try the search function .
Example #1
Source File: test_dunders.py    From attrs with MIT License 6 votes vote down vote up
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" 
Example #2
Source File: test_models.py    From pycoalaip with Apache License 2.0 5 votes vote down vote up
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) 
Example #3
Source File: test_models.py    From pycoalaip with Apache License 2.0 5 votes vote down vote up
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 
Example #4
Source File: test_models.py    From pycoalaip with Apache License 2.0 5 votes vote down vote up
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) 
Example #5
Source File: _init_fields.py    From related with MIT License 5 votes vote down vote up
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) 
Example #6
Source File: test_funcs.py    From attrs with MIT License 5 votes vote down vote up
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))