示例#1
0
    def test_from_attr_value(self):
        a = Attr.from_attr_value("attr", "value", compare=False)
        assert a.name == "attr"
        assert a.default == "value"
        assert a.compare is False

        a = Attr.from_attr_value("attr", a)
        assert a.name == "attr"
        assert a.default == "value"
        assert a.compare is False

        f = dataclasses.field(
            default="value",
            init=False,
            repr=False,
            compare=False,
            hash=False,
            metadata={"foo": "bar"},
        )
        a = Attr.from_attr_value("attr", f)
        assert a.name == "attr"
        for attr in {
                "default",
                "default_factory",
                "init",
                "repr",
                "compare",
                "hash",
                "metadata",
        }:
            a_value = getattr(a, attr)
            f_value = getattr(f, attr)
            assert (a_value == f_value
                    or a_value is MISSING and f_value is dataclasses.MISSING)
示例#2
0
        class Spec:
            attr: str = Attr(default="Hello World")
            unmanaged_attr = "Hi"
            invalidated_attr: str = Attr(
                default="Invalidated",
                repr=False,
                invalidated_by=["attr", "unmanaged_attr"],
            )

            @spec_property(cache=True, invalidated_by=["unmanaged_attr"])
            def invalidated_property(self):
                return self.unmanaged_attr
示例#3
0
 def test_constructor(self):
     with pytest.raises(
             ValueError,
             match=re.escape(
                 "Only one of `default` and `default_factory` can be specified."
             ),
     ):
         Attr(default="Hi", default_factory=lambda: "Hi")
示例#4
0
        class MyClass:
            attr: List[str] = Attr()

            @attr.preparer
            def _(self, attr):
                return list(attr)

            @attr.item_preparer
            def _(self, attr_item):
                return str(attr_item)
示例#5
0
    def test_default_helpers(self):
        assert Attr(default="Hi").has_default
        assert Attr(default_factory=lambda: "Hi").has_default
        assert not Attr().has_default

        assert Attr(default="Hi").default_value == "Hi"
        assert Attr(default_factory=lambda: "Hi").default_value == "Hi"
        assert Attr().default_value is MISSING
 def attr_spec2(self):
     return Attr.from_attr_value("attr", "value", type=List[Spec])
 def attr_spec(self):
     return Attr.from_attr_value("attr",
                                 "value",
                                 type=List[str],
                                 prepare_item=lambda self, x: str(x))
示例#8
0
 def attr_spec2(self):
     return Attr.from_attr_value("attr", "value", type=KeyedSet[Spec, str])
示例#9
0
 class MyClass:
     attr1: MySpec = Attr()
     attr2: List[MySpec] = Attr(default_factory=[])
示例#10
0
 class MyClass:
     a: int = Attr(default=Attr())
示例#11
0
 def attr_spec2(self):
     return Attr.from_attr_value("attr", "value", type=Dict[str, Spec])
示例#12
0
 class SubSpec(Spec):
     sub_invalidated_attr: str = Attr(
         default="Also Invalidated",
         repr=False,
         invalidated_by=["invalidated_attr"],
     )
示例#13
0
 class Spec:
     attr: str = Attr(default="Hello World")
     hidden_attr: str = Attr(
         default="Hidden", init=False, repr=False, compare=False
     )