Beispiel #1
0
 def norm(self, i: SerdeInst, t: FR, ctx: SerdeStepContext):
     if sys.version_info >= (3, 7):
         t = t._evaluate(ctx.mod.__dict__, ctx.mod.__dict__)
     else:
         t = t._eval_type(ctx.mod.__dict__, ctx.mod.__dict__)
     t = i.norm(t, ctx)
     return t
Beispiel #2
0
    def norm(self, i: SerdeInst, t: Any, ctx: SerdeStepContext) -> Any:
        *args, _ = t.__args__
        args = tuple(i.norm(x, ctx) for x in args)

        if len(args) > 1:
            return Optional[Union[args]]
        else:
            return Optional[args[0]]
Beispiel #3
0
    def step(self, i: SerdeInst, t: Any, ctx: SerdeStepContext) -> SerdeNode:
        # todo: here, we may need to instantiate the generic items
        t, ctx = _build_generic_context(t, ctx)

        xt = t

        if sys.version_info >= (3, 7):
            is_gen = hasattr(t, '__origin__')

            if is_gen:
                xt = t.__origin__

        logging.getLogger(__name__ + '.DataclassSerde.step').debug(
            '%s %s', t, ctx)

        return SerdeNode(i.norm(t, ctx), [
            i.norm(f.type, ctx)
            for f in sorted(fields(xt), key=lambda x: x.name)
        ], ctx)
Beispiel #4
0
    def step(self, i: SerdeInst, t: CallableArgsWrapper,
             ctx: SerdeStepContext) -> SerdeNode:
        if t.cls:
            ctx = build_generic_context(t.cls, ctx)
            ctx = ctx.merge(SerdeStepContext(mod=build_obj_module(t.cls)))

        types = sorted(self._build_args(t).items(), key=lambda x: x[0])

        types = [i.norm(x, ctx) for _, x in types]

        return SerdeNode(t, types, ctx)
Beispiel #5
0
    def step(self, i: SerdeInst, t: Any, ctx: SerdeStepContext) -> SerdeNode:
        xt = t

        if sys.version_info >= (3, 7):
            is_gen = hasattr(t, '__origin__')

            if is_gen:
                xt = t.__origin__

        ctx = SerdeStepContext(mod=build_obj_module(t))
        return SerdeNode(
            t, [i.norm(st, ctx) for f, st in sorted(xt._field_types.items())],
            ctx=ctx)
Beispiel #6
0
    def step(self, i: SerdeInst, t: Any, ctx: SerdeStepContext) -> SerdeNode:
        *args, _ = t.__args__

        args = tuple(i.norm(x, ctx) for x in args)

        deps = []

        if len(args) > 1:
            deps = [Union[args]]
        else:
            deps = [args[0]]

        return SerdeNode(self.norm(i, t, ctx), deps, ctx)
Beispiel #7
0
    def step(self, i: SerdeInst, t: CallableRetWrapper,
             ctx: SerdeStepContext) -> SerdeNode:
        if t.cls:
            ctx = build_generic_context(t.cls, ctx)
            ctx = ctx.merge(SerdeStepContext(mod=build_obj_module(t.cls)))

        RET = 'return'

        dt = None

        if RET in t.spec.annotations:
            dt = i.norm(t.spec.annotations[RET], ctx)
        return SerdeNode(t, [dt], ctx)
    def test_caller(self):
        i = SerdeInst(CALL_TYPES)

        class Simpleton(NamedTuple):
            x: int

        def a(a: int, b: str = 'abc', *cs: Dict[str, str], g: str, **kwargs: int):
            pass

        class A:
            def a(self, a: int, c: Simpleton, b: str = 'abc', *cs: Dict[str, str], g: str, **kwargs: int):
                pass

        obj = A()

        wrapper = CallableArgsWrapper.from_func(a)
        wrapper2 = CallableArgsWrapper.from_func_cls(A, A.a)

        x1 = SerdeSet.walk(i, wrapper)
        x2 = SerdeSet.walk(i, wrapper2)

        x = x1.merge(x2)


        y = x.struct(i)


        z = y.deserialize(wrapper, [[5, 'asd', {'a': 'a'}, {'b': 'c'}], {'g': 'abc', 'd': 5}])


        args, kwargs = z

        a(*args, **kwargs)

        #

        z = y.deserialize(wrapper2, [[5, {'x': 5}, 'asd', {'a': 'a'}, {'b': 'c'}], {'g': 'abc', 'd': 5}])

        zb = y.serialize(wrapper2, z)

        args, kwargs = z

        obj.a(*args, **kwargs)
Beispiel #9
0
 def step(self, i: SerdeInst, t: Any, ctx: SerdeStepContext) -> SerdeNode:
     kt, vt = t.__args__
     kt, vt = i.norm(kt, ctx), i.norm(vt, ctx)
     return SerdeNode(Dict[kt, vt], [kt, vt])
Beispiel #10
0
 def step(self, i: SerdeInst, t: Any, ctx: SerdeStepContext) -> SerdeNode:
     t = i.norm(t, ctx)
     return SerdeNode(t, [x for x in t.__args__], ctx=ctx)
Beispiel #11
0
    def norm(self, i: SerdeInst, t: Any, ctx: SerdeStepContext) -> Any:
        _, ctx = _build_generic_context(t, ctx)
        args = tuple(i.norm(st, ctx) for st in t.__args__)

        return Tuple[args]
Beispiel #12
0
 def step(self, i: SerdeInst, t: Any, ctx: SerdeStepContext) -> SerdeNode:
     return i.step(i.norm(t, ctx), ctx)
Beispiel #13
0
    def norm(self, i: SerdeInst, t: Any, ctx: SerdeStepContext) -> Any:
        _, ctx = _build_generic_context(t, ctx)
        st, = t.__args__
        st = i.norm(st, ctx)

        return List[st]
Beispiel #14
0
    def step(self, i: SerdeInst, t: Any, ctx: SerdeStepContext) -> SerdeNode:
        ordered = self.extract_ordered(Union[tuple(
            i.norm(x, ctx) for x in t.__args__)])

        return SerdeNode(Union[tuple(x for _, x in ordered)],
                         [t for _, t in ordered], ctx)
Beispiel #15
0
 def norm(self, i: SerdeInst, t: Any, ctx: SerdeStepContext) -> Any:
     st = tuple(i.norm(x, ctx) for x in t.__args__)
     return Union[st]
Beispiel #16
0
from xrpc.serde import types
from xrpc.serde.abstract import SerdeInst

SERVER_SERDE_ITEMS = [
    types.NoneSerde(),
    types.TypeVarSerde(),
    types.CallableArgsSerde(),
    types.CallableRetSerde(),
    types.ForwardRefSerde(),
    types.OptionalSerde(),
    types.UnionSerde(),
    types.BytesSerde(),
    types.DateTimeSerde(),
    types.TimeDeltaSerde(),
    types.DateSerde(),
    types.AtomSerde(),
    types.UUIDSerde(),
    types.ListSerde(),
    types.DictSerde(),
    types.EnumSerde(),
    types.DataclassSerde(),
    types.NamedTupleSerde(),
    types.TupleSerde(),
]
SERVER_SERDE_INST = SerdeInst(SERVER_SERDE_ITEMS)