Ejemplo n.º 1
0
def test_copy_exclude():
    @datacls
    class Foo:
        a: Optional[int]
        b: Optional[str]
        c: Optional[float]

    foo = Foo(a=1, b="a", c=2.0)
    exclude = {"a"}
    bar = copy_data(foo, Foo, exclude=exclude)
    for name in bar.__annotations__:
        assert getattr(
            bar, name) == (getattr(foo, name) if name not in exclude else None)
Ejemplo n.º 2
0
def test_copy_dc_to_td_exclude():
    fields = (("a", Optional[str]), ("b", Optional[int]))
    DC = make_datacls("DC", fields)
    TD = TypedDict("TD", fields)
    dc = DC(a="a", b=1)
    assert copy_data(dc, TD, exclude={"a"}) == {"b": 1}
Ejemplo n.º 3
0
def test_copy_dc_to_td():
    fields = (("a", str), ("b", int))
    DC = make_datacls("DC", fields)
    TD = TypedDict("TD", fields)
    dc = DC(a="a", b=1)
    assert copy_data(dc, TD) == asdict(dc)
Ejemplo n.º 4
0
def test_copy_td_to_dc_exclude():
    fields = (("a", Optional[str]), ("b", Optional[int]))
    DC = make_datacls("DC", fields)
    TD = TypedDict("TD", fields)
    td = TD(a="a", b=1)
    assert copy_data(td, DC, exclude={"a"}) == DC(b=1)
Ejemplo n.º 5
0
def test_copy_td_to_dc():
    fields = (("a", str), ("b", int))
    DC = make_datacls("DC", fields)
    TD = TypedDict("TD", fields)
    td = TD(a="a", b=1)
    assert copy_data(td, DC) == DC(**td)
Ejemplo n.º 6
0
def test_copy_all():
    Foo = make_datacls("Foo", (("a", Optional[int]), ("b", Optional[str]),
                               ("c", Optional[float])))
    foo = Foo(a=1, b="a", c=2.0)
    bar = copy_data(foo, Foo)
    assert foo == bar