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
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]]
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)
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)
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)
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)
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)
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])
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)
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]
def step(self, i: SerdeInst, t: Any, ctx: SerdeStepContext) -> SerdeNode: return i.step(i.norm(t, ctx), ctx)
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]
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)
def norm(self, i: SerdeInst, t: Any, ctx: SerdeStepContext) -> Any: st = tuple(i.norm(x, ctx) for x in t.__args__) return Union[st]
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)