def _mapping_to_instance(mapping: Mapping, destination_type: Type[T], instantiator: Callable = None, expand_kwargs=True) -> T: """Convert a dictionary-like object into an instance of the given type This conversion is performed recursively. If the passed type class contains child structures, then the corresponding child keys of the dictionary will be mapped. If `instantiator` is provided, it will be called rather than simply calling `destination_type`. If `expand_kwargs` is True then the the parameters will be expended (i.e. **) when the `destination_type` / `instantiator` is called. """ import lightbus.config.structure hints = get_type_hints(destination_type, None, lightbus.config.structure.__dict__) parameters = {} if mapping is None: return None # Iterate through each key/type-hint pairing in the destination type for key, hint in hints.items(): value = mapping.get(key) if key not in mapping: # This attribute has not been provided by in the mapping. Skip it continue hint_type, hint_args = parse_hint(hint) # Is this an Optional[] hint (which looks like Union[Thing, None]) if is_optional(hint) and value is not None: # Yep, it's an Optional. So unwrap it. hint = hint_args[0] if issubclass_safe(hint_type, Mapping) and hint_args and len(hint_args) == 2: parameters[key] = dict() for k, v in value.items(): parameters[key][k] = cast_to_hint(v, hint_args[1]) else: parameters[key] = cast_to_hint(value, hint) instantiator = instantiator or destination_type if expand_kwargs: return instantiator(**parameters) else: return instantiator(parameters)
def python_type_to_json_schemas(type_): """Convert a python type hint to its JSON schema representations Note that a type hint may actually have several possible representations, which is why this function returns a list of schemas. An example of this is the `Union` type hint. These are later combined via `wrap_with_any_of()` """ # pylint: disable=too-many-return-statements type_, hint_args = parse_hint(type_) if type_ == Union: return list( itertools.chain(*map(python_type_to_json_schemas, hint_args))) if type_ == empty: return [{}] elif type_ in (Any, ...): return [{}] elif hasattr(type_, "__to_bus__"): return python_type_to_json_schemas( inspect.signature(type_.__to_bus__).return_annotation) elif issubclass_safe(type_, (str, bytes, complex, UUID)): return [{"type": "string"}] elif issubclass_safe(type_, Decimal): return [{"type": "string", "pattern": r"^-?\d+(\.\d+)?$"}] elif issubclass_safe(type_, (bool, )): return [{"type": "boolean"}] elif issubclass_safe(type_, (float, )): return [{"type": "number"}] elif issubclass_safe(type_, (int, )): return [{"type": "integer"}] elif issubclass_safe(type_, (Mapping, )) and hint_args and hint_args[0] == str: # Mapping with strings as keys return [{ "type": "object", "additionalProperties": wrap_with_any_of(python_type_to_json_schemas(hint_args[1])), }] elif issubclass_safe(type_, (dict, Mapping)): return [{"type": "object"}] elif issubclass_safe(type_, tuple) and hasattr(type_, "_fields"): # Named tuple return [make_custom_object_schema(type_, property_names=type_._fields)] elif issubclass_safe(type_, Enum) and type_.__members__: # Enum enum_first_value = list(type_.__members__.values())[0].value schema = {} try: schema["type"] = python_type_to_json_schemas( type(enum_first_value))[0]["type"] schema["enum"] = [v.value for v in type_.__members__.values()] except KeyError: logger.warning( f"Could not determine type for values in enum: {type_}") return [schema] elif issubclass_safe(type_, (Tuple, )) and hint_args: return [{ "type": "array", "maxItems": len(hint_args), "minItems": len(hint_args), "items": [ wrap_with_any_of(python_type_to_json_schemas(sub_type)) for sub_type in hint_args ], }] elif issubclass_safe(type_, (list, tuple, set)): schema = {"type": "array"} if hint_args: schema["items"] = wrap_with_any_of( python_type_to_json_schemas(hint_args[0])) return [schema] elif issubclass_safe(type_, NoneType) or type_ is None: return [{"type": "null"}] elif issubclass_safe(type_, (datetime.datetime)): return [{"type": "string", "format": "date-time"}] elif issubclass_safe(type_, (datetime.date)): return [{"type": "string", "format": "date"}] elif issubclass_safe(type_, (datetime.time)): return [{"type": "string", "format": "time"}] elif getattr(type_, "__annotations__", None): # Custom class return [make_custom_object_schema(type_)] else: logger.warning( f"Could not convert python type to json schema type: {type_}. If it is a class, " "ensure it's class-level variables have type hints.") return [{}]
def cast_to_hint(value: V, hint: H) -> Union[V, H]: """Cast a value into a given type hint If a value cannot be cast then the original value will be returned and a warning emitted """ # pylint: disable=too-many-return-statements if value is None: return None elif hint in (Any, ...): return value optional_hint = is_optional(hint) if optional_hint and value is not None: hint = optional_hint hint_type, hint_args = parse_hint(hint) is_class = inspect.isclass(hint_type) if hint_type == Union: # We don't attempt to deal with unions for now return value elif hint == inspect.Parameter.empty: # Empty annotation return value elif isinstance_safe(value, hint): # Already correct type return value elif hasattr(hint, "__from_bus__"): # Hint supports custom deserializing. return _mapping_to_instance( mapping=value, destination_type=hint, instantiator=hint.__from_bus__, expand_kwargs=False, ) elif issubclass_safe(hint, bytes): return b64decode(value.encode("utf8")) elif type_is_namedtuple(hint) and isinstance_safe(value, Mapping): return _mapping_to_instance(mapping=value, destination_type=hint) elif type_is_dataclass(hint) and isinstance_safe(value, Mapping): return _mapping_to_instance(mapping=value, destination_type=hint) elif is_class and issubclass_safe( hint_type, datetime.datetime) and isinstance_safe(value, str): # Datetime as a string return dateutil.parser.parse(value) elif is_class and issubclass_safe( hint_type, datetime.date) and isinstance_safe(value, str): # Date as a string return cast_or_warning(lambda v: dateutil.parser.parse(v).date(), value) elif is_class and issubclass_safe(hint_type, list): # Lists if hint_args and hasattr(value, "__iter__"): value = [cast_to_hint(i, hint_args[0]) for i in value] return cast_or_warning(list, value) elif is_class and issubclass_safe(hint_type, tuple): # Tuples if hint_args and hasattr(value, "__iter__"): value = [ cast_to_hint(h, hint_args[i]) for i, h in enumerate(value) ] return cast_or_warning(tuple, value) elif is_class and issubclass_safe(hint_type, set): # Sets if hint_args and hasattr(value, "__iter__"): value = [cast_to_hint(i, hint_args[0]) for i in value] return cast_or_warning(set, value) elif (inspect.isclass(hint) and hasattr(hint, "__annotations__") and not issubclass_safe(hint_type, Enum)): logger.warning( f"Cannot cast to arbitrary class {hint}, using un-casted value. " f"If you want to receive custom objects you can 1) " f"use a NamedTuple, 2) use a dataclass, or 3) specify the " f"__from_bus__() and __to_bus__() magic methods.") return value else: return cast_or_warning(hint, value)