def test_normal(self): """ Transforms every `_CountingAttr` and leaves others (a) be. """ C = make_tc() _transform_attrs(C, None) assert ["z", "y", "x"] == [a.name for a in C.__attrs_attrs__]
def test_transforms_to_attribute(self, attribute): """ All `_CountingAttr`s are transformed into `Attribute`s. """ C = make_tc() _transform_attrs(C, None) assert isinstance(getattr(C, attribute), Attribute)
def test_no_modifications(self): """ Doesn't attach __attrs_attrs__ to the class anymore. """ C = make_tc() _transform_attrs(C, None, False) assert None is getattr(C, "__attrs_attrs__", None)
def test_no_modifications(self): """ Does not attach __attrs_attrs__ to the class. """ C = make_tc() _transform_attrs(C, None, False, False) assert None is getattr(C, "__attrs_attrs__", None)
def test_these(self): """ If these is passed, use it and ignore body. """ class C(object): y = attr() _transform_attrs(C, {"x": attr()}) assert (simple_attr("x"), ) == C.__attrs_attrs__ assert isinstance(C.y, _CountingAttr)
def test_empty(self): """ No attributes works as expected. """ @attributes class C(object): pass _transform_attrs(C, None) assert () == C.__attrs_attrs__
def test_these(self): """ If these is passed, use it and ignore body. """ class C(object): y = attr() _transform_attrs(C, {"x": attr()}) assert ( simple_attr("x"), ) == C.__attrs_attrs__ assert isinstance(C.y, _CountingAttr)
def test_recurse(self): """ Collect attributes from all sub-classes. """ class A(object): a = None class B(A): b = attr() _transform_attrs(B, None) class C(B): c = attr() _transform_attrs(C, None) class D(C): d = attr() _transform_attrs(D, None) class E(D): e = attr() _transform_attrs(E, None) assert ( simple_attr("b"), simple_attr("c"), simple_attr("d"), simple_attr("e"), ) == E.__attrs_attrs__
def test_conflicting_defaults(self): """ Raises `ValueError` if attributes with defaults are followed by mandatory attributes. """ class C(object): x = attr.ib(default=None) y = attr.ib() with pytest.raises(ValueError) as e: _transform_attrs(C, None, False) assert ("No mandatory attributes allowed after an attribute with a " "default value or factory. Attribute in question: Attribute" "(name='y', default=NOTHING, validator=None, repr=True, " "cmp=True, hash=None, init=True, convert=None, " "metadata=mappingproxy({}), type=None)", ) == e.value.args
def test_conflicting_defaults(self): """ Raises `ValueError` if attributes with defaults are followed by mandatory attributes. """ class C(object): x = attr(default=None) y = attr() with pytest.raises(ValueError) as e: _transform_attrs(C) assert ( "No mandatory attributes allowed after an atribute with a " "default value or factory. Attribute in question: Attribute" "(name='y', default=NOTHING, validator=None, no_repr=False, " "no_cmp=False, no_hash=False, no_init=False)", ) == e.value.args
def test_kw_only(self): """ Converts all attributes, including base class' attributes, if `kw_only` is provided. Therefore, `kw_only` allows attributes with defaults to preceed mandatory attributes. Updates in the subclass *don't* affect the base class attributes. """ @attr.s class B(object): b = attr.ib() for b_a in B.__attrs_attrs__: assert b_a.kw_only is False class C(B): x = attr.ib(default=None) y = attr.ib() attrs, base_attrs, _ = _transform_attrs(C, None, False, True) assert len(attrs) == 3 assert len(base_attrs) == 1 for a in attrs: assert a.kw_only is True for b_a in B.__attrs_attrs__: assert b_a.kw_only is False
def test_conflicting_defaults(self): """ Raises `ValueError` if attributes with defaults are followed by mandatory attributes. """ class C(object): x = attr(default=None) y = attr() with pytest.raises(ValueError) as e: _transform_attrs(C, None) assert ( "No mandatory attributes allowed after an attribute with a " "default value or factory. Attribute in question: Attribute" "(name='y', default=NOTHING, validator=None, repr=True, " "cmp=True, hash=True, init=True)", ) == e.value.args
def test_conflicting_keyword_only_attributes(self): """ Raises `ValueError` if keyword-only attributes are followed by regular (non keyword-only) attributes. """ class C(object): x = attr.ib(kw_only=True) y = attr.ib() with pytest.raises(ValueError) as e: _transform_attrs(C, None, False, False) assert ("Non keyword-only attributes are not allowed after a " "keyword-only attribute. Attribute in question: Attribute" "(name='y', default=NOTHING, validator=None, repr=True, " "cmp=True, hash=None, init=True, metadata=mappingproxy({}), " "type=None, converter=None, kw_only=False)", ) == e.value.args
def test_empty(self): """ No attributes works as expected. """ @attr.s class C(object): pass assert _Attributes(((), [], {})) == _transform_attrs(C, None, False)
def test_empty(self): """ No attributes works as expected. """ @attr.s class C(object): pass assert _Attributes(((), [])) == _transform_attrs(C, None, False)
def test_transforms_to_attribute(self): """ All `_CountingAttr`s are transformed into `Attribute`s. """ C = make_tc() attrs, base_attrs, _ = _transform_attrs(C, None, False, False) assert [] == base_attrs assert 3 == len(attrs) assert all(isinstance(a, Attribute) for a in attrs)
def test_transforms_to_attribute(self): """ All `_CountingAttr`s are transformed into `Attribute`s. """ C = make_tc() attrs, super_attrs, _ = _transform_attrs(C, None, False) assert [] == super_attrs assert 3 == len(attrs) assert all(isinstance(a, Attribute) for a in attrs)
def test_recurse(self): """ Collect attributes from all sub-classes. """ class A(object): pass class C(A): x = attr() _transform_attrs(C, None) class D(C): y = attr() _transform_attrs(D, None) assert ( simple_attr("x"), simple_attr("y"), ) == D.__attrs_attrs__
def test_these(self): """ If these is passed, use it and ignore body and super classes. """ class Base(object): z = attr.ib() class C(Base): y = attr.ib() attrs, super_attrs = _transform_attrs(C, {"x": attr.ib()}, False) assert [] == super_attrs assert (simple_attr("x"), ) == attrs
def test_these(self): """ If these is passed, use it and ignore body and super classes. """ class Base(object): z = attr.ib() class C(Base): y = attr.ib() attrs, super_attrs, _ = _transform_attrs(C, {"x": attr.ib()}, False) assert [] == super_attrs assert ( simple_attr("x"), ) == attrs