def _build_typedtuple_des( self, func: gen.Block, anno_name: str, annotation: "Annotation", namespace: Type = None, ): with func.b(f"if issubclass({self.VTYPE}, Mapping):", Mapping=abc.Mapping) as b: if annotation.serde.fields: self._build_typeddict_des(b, anno_name, annotation, namespace=namespace) else: b.l(f"{self.VNAME} = {anno_name}(**{self.VNAME})", ) with func.b( f"elif isinstance({self.VNAME}, (list, set, frozenset, tuple)):" ) as b: if annotation.serde.fields: b.l( f"{self.VNAME} = __bind({anno_name}, *{self.VNAME}).eval()", __bind=self.resolver.bind, ) else: b.l(f"{self.VNAME} = {anno_name}(*{self.VNAME})", ) with func.b("else:") as b: b.l( f"{self.VNAME} = translate({self.VNAME}, {anno_name})", translate=self.resolver.translate, )
def _build_typeddict_des( self, func: gen.Block, anno_name: str, annotation: "Annotation", *, total: bool = True, namespace: Type = None, ): with func.b(f"if issubclass({self.VTYPE}, Mapping):", Mapping=abc.Mapping) as b: fields_deser = { x: self.resolver._resolve_from_annotation( y, _namespace=namespace).transmute for x, y in annotation.serde.fields.items() } x = "fields_in[x]" y = (f"fields_deser[x]({self.VNAME}[x])" if fields_deser else f"{self.VNAME}[x]") line = f"{{{x}: {y} for x in fields_in.keys()" tail = "}" if total else f"& {self.VNAME}.keys()}}" b.l(f"{self.VNAME} = {anno_name}(**{line}{tail})", fields_deser=fields_deser) with func.b("else:") as b: b.l( f"{self.VNAME} = translate({self.VNAME}, {anno_name})", translate=self.resolver.translate, )
def _build_timedelta_des(self, func: gen.Block, anno_name: str, annotation: "Annotation"): # From an int with func.b(f"if isinstance({self.VNAME}, (int, float)):") as b: b.l(f"{self.VNAME} = {anno_name}(int({self.VNAME}))") # From a string with func.b(f"elif isinstance({self.VNAME}, (str, bytes)):") as b: line = f"{self.VNAME} = dateparse({self.VNAME}, exact=True)" b.l(line, dateparse=dateparse)
def _build_validator(self, func: gen.Block, context: ContextT, assertions: AssertionsT) -> ContextT: if (self.max_digits, self.decimal_places) == (None, None): context = NumberConstraints._build_validator(self, func, context=context, assertions=assertions) context.update(decimal=decimal, Decimal=decimal.Decimal, _get_digits=_get_digits) return context # Update the global namespace for the validator # Add setup/sanity checks for decimals. func.l(f"{self.VALUE} = decimal.Decimal({self.VALUE})") with func.b( f"if {self.VALUE}.is_infinite():", ConstraintValueError=ConstraintValueError, ) as b: b.l("raise ConstraintValueError('Cannot validate infinite values.')" ) func.l(f"tup = {self.VALUE}.as_tuple()") func.l( "whole, digits, decimals = _get_digits(tup)", _get_digits=_get_digits, ) context = NumberConstraints._build_validator(self, func, context=context, assertions=assertions) context.update(decimal=decimal, Decimal=decimal.Decimal, _get_digits=_get_digits) return context
def _build_validator(self, func: gen.Block, context: Dict[str, Any], assertions: AssertionsT) -> ContextT: if self.key_dependencies: self._get_key_dependencies(assertions, context) _lazy_repr = (util.collectionrepr if issubclass(self.type, Mapping) else util.joinedrepr) context.update(Mapping=Mapping, _lazy_repr=_lazy_repr) if self.required_keys: context["required"] = self.required_keys defined_keys = (self.required_keys or set()) | (self.items or {}).keys() if defined_keys: context["defined"] = frozenset(defined_keys) if not issubclass(self.type, Mapping): with func.b(f"if not isinstance({self.VALUE}, Mapping):") as b: b.l(f"return False, {self.VALUE}") func.l(f"valkeys = {{*{self.VALUE}}}") context = BaseConstraints._build_validator(self, func=func, context=context, assertions=assertions) items_context = self._build_item_validator(func) if items_context: context.update(items_context) return context
def _set_item_validator_pattern_constraints(self, loop: gen.Block, func_name: str): # Item constraints based upon key-pattern pattern_constr_name = f"{func_name}_pattern_constraints" if self.patterns: loop.l( f"{self.RETY} = " f"validate_pattern_constraints" f"({pattern_constr_name}, {self.X}, {self.Y})", level=None, **{ "validate_pattern_constraints": validate_pattern_constraints, pattern_constr_name: self.patterns, }, ) # Required key pattern if self.key_pattern: key_pattern_name = f"{func_name}_key_pattern" loop.l( f"valid = bool({key_pattern_name}.match({self.X}))", level=None, **{key_pattern_name: self.key_pattern}, ) with loop.b("if not valid:") as b: b.l("break")
def _build_uuid_des(self, func: gen.Block, anno_name: str, annotation: "Annotation"): self._add_type_check(func, anno_name) with func.b(f"if issubclass({self.VTYPE}, UUID):", UUID=uuid.UUID) as b: b.l(f"{self.VNAME} = {anno_name}(int={self.VNAME}.int)") with func.b(f"elif isinstance({self.VNAME}, str):") as b: b.l(f"{self.VNAME} = {anno_name}({self.VNAME})") with func.b(f"elif isinstance({self.VNAME}, bytes):") as b: b.l(f"{self.VNAME} = {anno_name}(bytes={self.VNAME})") with func.b(f"elif isinstance({self.VNAME}, int):") as b: b.l(f"{self.VNAME} = {anno_name}(int={self.VNAME})") with func.b(f"elif isinstance({self.VNAME}, tuple):") as b: b.l(f"{self.VNAME} = {anno_name}(fields={self.VNAME})")
def _build_text_des( self, func: gen.Block, anno_name: str, annotation: "Annotation", ): origin = annotation.resolved_origin # Encode for bytes if issubclass(origin, bytes): with func.b(f"if isinstance({self.VNAME}, str):") as b: b.l(f"{self.VNAME} = {anno_name}(" f"{self.VNAME}, encoding={DEFAULT_ENCODING!r})") # Decode for str elif issubclass(origin, str): with func.b( f"if isinstance({self.VNAME}, (bytes, bytearray)):") as b: b.l(f"{self.VNAME} = {self.VNAME}.decode({DEFAULT_ENCODING!r})" ) func.l(f"{self.VNAME} = {anno_name}({self.VNAME})")
def _build_item_validator(self, func: gen.Block) -> Optional[ContextT]: if any(( self.items, self.patterns, self.key_pattern, self.keys, self.values, )): with func.b(f"{self.VALUE} = {{") as loop: item_context = self._set_item_validator_loop_line( loop, func.name) loop.l(f"for {self.X}, {self.Y} in {self.VALUE}.items()") func.l("}") if self.key_pattern: key_pattern_name = f"{func.name}_key_pattern" with func.b(f"if any((not {key_pattern_name}.match({self.X}) " f"for {self.X} in {self.VALUE})):") as b: b.l(f"return False, {self.VALUE}") item_context[key_pattern_name] = self.key_pattern return item_context return None
def _build_union_des(self, func: gen.Block, annotation: "Annotation", namespace): # Get all types which we may coerce to. args = (*(a for a in annotation.args if a not in {None, Ellipsis, type(None)}), ) # Get all custom types, which may have discriminators targets = (*(a for a in args if not checks.isstdlibtype(a)), ) # We can only build a tagged union deserializer if all args are valid if args and args == targets: # Try to collect the field which will be the discriminator. # First, get a mapping of Type -> Proto & Type -> Fields tagged = get_tag_for_types(targets) # Just bail out if we can't find a key. if not tagged: func.l("# No-op, couldn't locate a discriminator key.") return # If we got a key, re-map the protocols to the value for each type. deserializers = { value: self.resolver.resolve(t, namespace=namespace) for value, t in tagged.types_by_values } # Finally, build the deserializer func.namespace.update( tag=tagged.tag, desers=deserializers, empty=_empty, ) with func.b(f"if issubclass({self.VTYPE}, Mapping):", Mapping=abc.Mapping) as b: b.l(f"tag_value = {self.VNAME}.get(tag, empty)") with func.b("else:") as b: b.l(f"tag_value = getattr({self.VNAME}, tag, empty)") with func.b("if tag_value in desers:") as b: b.l(f"{self.VNAME} = desers[tag_value].transmute({self.VNAME})" ) with func.b("else:") as b: b.l("raise ValueError(" 'f"Value is missing field {tag!r} with one of ' '{(*desers,)}: {val!r}"' ")")
def _build_date_des(self, func: gen.Block, anno_name: str, annotation: "Annotation"): origin = annotation.resolved_origin # From an int with func.b(f"if isinstance({self.VNAME}, (int, float)):") as b: b.l(f"{self.VNAME} = {anno_name}.fromtimestamp({self.VNAME})") # From a string with func.b(f"elif isinstance({self.VNAME}, (str, bytes)):") as b: line = f"{self.VNAME} = dateparse({self.VNAME})" b.l(line, dateparse=dateparse) if issubclass(origin, datetime.datetime): with func.b(f"if isinstance({self.VNAME}, datetime):", datetime=datetime.datetime) as b: # Use pendulum's helper if possible. if origin is DateTime: b.l(f"{self.VNAME} = instance({self.VNAME})", instance=instance) else: b.l( f"{self.VNAME} = " f"{anno_name}(" f"{self.VNAME}.year, " f"{self.VNAME}.month, " f"{self.VNAME}.day, " f"{self.VNAME}.hour, " f"{self.VNAME}.minute, " f"{self.VNAME}.second, " f"{self.VNAME}.microsecond, " f"{self.VNAME}.tzinfo" f")", ) with func.b(f"elif isinstance({self.VNAME}, date):", date=datetime.date) as b: b.l( f"{self.VNAME} = " f"{anno_name}(" f"{self.VNAME}.year, " f"{self.VNAME}.month, " f"{self.VNAME}.day" f")", ) elif issubclass(origin, datetime.date): with func.b(f"if isinstance({self.VNAME}, datetime):", datetime=datetime.datetime) as b: b.l(f"{self.VNAME} = {self.VNAME}.date()") with func.b(f"elif isinstance({self.VNAME}, (int, float)):") as b: b.l(f"{self.VNAME} = {anno_name}.fromtimestamp({self.VNAME})") with func.b(f"elif isinstance({self.VNAME}, (str, bytes)):") as b: line = f"{self.VNAME} = dateparse({self.VNAME}, exact=True)" b.l(line, dateparse=dateparse)
def _set_checks(self, func: gen.Block, anno_name: str, annotation: Annotation): _ctx = {} # run a safe eval if input is text and anno isn't if inspect.isclass(annotation.resolved_origin) and (issubclass( annotation.resolved_origin, (str, bytes)) or checks.isdecimaltype(annotation.resolved_origin)): self._add_vtype(func) else: self._add_eval(func) # Equality checks for defaults and optionals custom_equality = hasattr(annotation.resolved_origin, "equals") if custom_equality and (annotation.optional or annotation.has_default): func.l(f"custom_equality = hasattr({self.VNAME}, 'equals')") null = "" if annotation.optional: null = f"{self.VNAME} in {self.resolver.OPTIONALS}" if custom_equality: null = ( f"(any({self.VNAME}.equals(o) for o in {self.resolver.OPTIONALS}) " "if custom_equality " f"else {null})") eq = "" if (annotation.has_default and annotation.parameter.default not in self.resolver.OPTIONALS): eq = f"{self.VNAME} == __default" if custom_equality: if hasattr(annotation.parameter.default, "equals"): eq = f"__default.equals({self.VNAME})" eq = f"{self.VNAME}.equals(__default) if custom_equality else {eq}" _ctx["__default"] = annotation.parameter.default if eq or null: # Add a type-check for anything that isn't a builtin. if eq and not checks.isbuiltintype(annotation.resolved_origin): eq = f"{self.VTYPE} is {anno_name} and {eq}" check = " or ".join(c for c in (null, eq) if c) with func.b(f"if {check}:", **_ctx) as b: # type: ignore b.l(f"return {self.VNAME}")
def _build_assertions(self, func: gen.Block, assertions: AssertionsT): check = " and ".join(assertions) with func.b(f"if not ({check}):") as b: b.l(f"return False, {self.VALUE}")
def _add_type_check(self, func: gen.Block, anno_name: str): with func.b(f"if {self.VTYPE} is {anno_name}:") as b: b.l(f"{gen.Keyword.RET} {self.VNAME}")
def _build_generic_des( self, func: gen.Block, anno_name: str, annotation: "Annotation", namespace: Type = None, ): serde = annotation.serde resolved = annotation.resolved self._add_type_check(func, anno_name) # Main branch - we have a mapping for a user-defined class. # This is where the serde configuration comes in. # WINDY PATH AHEAD func.l("# Happy path - deserialize a mapping into the object.") with func.b(f"if issubclass({self.VTYPE}, Mapping):", Mapping=abc.Mapping) as b: # Universal line - transform input to known keys/values. # Specific values may change. def mainline(k, v): return f"{{{k}: {v} for x in fields_in.keys() & {self.VNAME}.keys()}}" # The "happy path" - e.g., no guesswork needed. def happypath(k, v, **ns): b.l(f"{self.VNAME} = {anno_name}(**{mainline(k, v)})", **ns) # Default X - translate given `x` to known input `x` x = "fields_in[x]" # No field name translation needs to happen. if {*serde.fields_in.keys()} == {*serde.fields_in.values()}: x = "x" # Default Y - get the given `y` with the given `x` y = f"{self.VNAME}[x]" # Get the intersection of known input fields and annotations. matched = {*serde.fields_in.values()} & serde.fields.keys() # Happy path! This is a `@typic.al` wrapped class. if self.resolver.known(resolved) or self.resolver.delayed( resolved): happypath(x, y) # Secondary happy path! We know how to deserialize already. else: fields_in = serde.fields_in if serde.fields and len(matched) == len(serde.fields_in): desers = { f: self.resolver._resolve_from_annotation( serde.fields[f], _namespace=namespace).transmute for f in matched } else: protocols = self.resolver.protocols( annotation.resolved_origin) fields_in = {x: x for x in protocols} desers = {f: p.transmute for f, p in protocols.items()} y = f"desers[{x}]({self.VNAME}[x])" happypath(x, y, desers=desers, fields_in=fields_in) # Secondary branch - we have some other input for a user-defined class func.l("# Unknown path, just try casting it directly.") with func.b( f"elif isbuiltinsubtype({self.VTYPE}):", isbuiltinsubtype=checks.isbuiltinsubtype, ) as b: b.l(f"{self.VNAME} = {anno_name}({self.VNAME})") # Final branch - user-defined class for another user-defined class func.l("# Two user-defined types, " "try to translate the input into the desired output.") with func.b("else:") as b: b.l( f"{self.VNAME} = translate({self.VNAME}, {anno_name})", translate=self.resolver.translate, )