Esempio n. 1
0
def test_inherited_serde_flags():
    @typic.klass(serde=typic.flags(omit=(1, )))
    class Foo:
        a: str
        b: str = typic.field(exclude=True)

    @typic.klass(serde=typic.flags(omit=(2, )))
    class Bar(Foo):
        c: int

    assert Bar.__serde_flags__.fields.keys() == {"a", "b", "c"}
    assert Bar.__serde_flags__.exclude == {"b"}
    assert Bar.__serde_flags__.omit == (1, 2)
Esempio n. 2
0
def test_klass_iterate_exclude():
    @typic.klass(serde=typic.flags(exclude=("excluded", )))
    class Foo:
        bar: str = None
        excluded: str = None

    assert dict(Foo().iterate()) == {"bar": None}
Esempio n. 3
0
def test_protocol_iterate_exclude():
    @dataclasses.dataclass
    class Foo:
        bar: str = None
        excluded: str = None

    proto = typic.protocol(Foo, flags=typic.flags(exclude=("excluded", )))

    assert dict(proto.iterate(Foo())) == {"bar": None}
Esempio n. 4
0
def test_custom_decode():
    def decode(o):
        return o.decode("utf-8-sig")

    @dataclasses.dataclass
    class Foo:
        bar: str = None

    proto = typic.protocol(Foo, flags=typic.flags(decoder=decode))
    inp = '{"bar":null}'.encode("utf-8-sig")
    dec = proto.decode(inp)
    assert dec == Foo()
Esempio n. 5
0
def test_custom_encode():
    def encode(o):
        return ujson.encode(o).encode("utf-8-sig")

    @dataclasses.dataclass
    class Foo:
        bar: str = None

    proto = typic.protocol(Foo, flags=typic.flags(encoder=encode))
    enc = proto.encode(Foo())
    assert isinstance(enc, bytes)
    assert enc.decode("utf-8-sig") == '{"bar":null}'
Esempio n. 6
0
def test_klass_custom_encdec():
    def encode(o):
        return ujson.encode(o).encode("utf-8-sig")

    def decode(o):
        return o.decode("utf-8-sig")

    @typic.klass(serde=typic.flags(encoder=encode, decoder=decode))
    class Foo:
        bar: str = None

    enc = Foo().encode()
    dec = Foo.decode(enc)
    assert isinstance(enc, bytes)
    assert enc.decode("utf-8-sig") == '{"bar":null}'
    assert dec == Foo()
Esempio n. 7
0
 class Foo:
     __serde_flags__ = typic.flags(exclude=("excluded", ))
     bar: str = None
     excluded: bool = True
Esempio n. 8
0
class SerDict(Dict):
    __serde_flags__ = typic.flags(fields=("foo_bar", ),
                                  case=typic.common.Case.CAMEL)
Esempio n. 9
0
class GenDict(Generic[_KT, _VT], Dict):
    __serde_flags__ = typic.flags(fields=("foo_bar", ),
                                  case=typic.common.Case.CAMEL)
Esempio n. 10
0
import typic.ext.json
from tests import objects


@typic.klass
class FieldMapp:
    foo_bar: str = typic.field(default="bar", name="foo")


@typic.klass(serde=typic.flags(case=typic.common.Case.CAMEL))
class Camel:

    foo_bar: str = "bar"


@typic.klass(serde=typic.flags(signature_only=True))
class SigOnly:

    foo: ClassVar[str] = "foo"
    foo_bar: str = "bar"


@typic.klass(serde=typic.flags(omit=("bar", )))
class Omit:

    bar: str = "foo"
    foo: str = "bar"


@typic.klass
class ClassVarEnum: