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)
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}
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}
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()
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}'
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()
class Foo: __serde_flags__ = typic.flags(exclude=("excluded", )) bar: str = None excluded: bool = True
class SerDict(Dict): __serde_flags__ = typic.flags(fields=("foo_bar", ), case=typic.common.Case.CAMEL)
class GenDict(Generic[_KT, _VT], Dict): __serde_flags__ = typic.flags(fields=("foo_bar", ), case=typic.common.Case.CAMEL)
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: