def _sequence_from_input(self, cls: type, fieldpath: str, anntype: Any, value: Any, seqtype: type, ioattrs: Optional[IOAttrs]) -> Any: # Because we are json-centric, we expect a list for all sequences. if type(value) is not list: raise TypeError(f'Invalid input value for "{fieldpath}";' f' expected a list, got a {type(value).__name__}') childanntypes = typing.get_args(anntype) # 'Any' type children; make sure they are valid json values # and then just grab them. if len(childanntypes) == 0 or childanntypes[0] is typing.Any: for i, child in enumerate(value): if not _is_valid_for_codec(child, self._codec): raise TypeError(f'Item {i} of {fieldpath} contains' f' data type(s) not supported by json.') return value if type(value) is seqtype else seqtype(value) # We contain elements of some specified type. assert len(childanntypes) == 1 childanntype = childanntypes[0] return seqtype( self._value_from_input(cls, fieldpath, childanntype, i, ioattrs) for i in value)
def _process_dataclass(self, cls: type, obj: Any, fieldpath: str) -> Any: # pylint: disable=too-many-locals # pylint: disable=too-many-branches prep = PrepSession(explicit=False).prep_dataclass(type(obj), recursion_level=0) assert prep is not None fields = dataclasses.fields(obj) out: Optional[dict[str, Any]] = {} if self._create else None for field in fields: fieldname = field.name if fieldpath: subfieldpath = f'{fieldpath}.{fieldname}' else: subfieldpath = fieldname anntype = prep.annotations[fieldname] value = getattr(obj, fieldname) anntype, ioattrs = _parse_annotated(anntype) # If we're not storing default values for this fella, # we can skip all output processing if we've got a default value. if ioattrs is not None and not ioattrs.store_default: default_factory: Any = field.default_factory if default_factory is not dataclasses.MISSING: if default_factory() == value: continue elif field.default is not dataclasses.MISSING: if field.default == value: continue else: raise RuntimeError( f'Field {fieldname} of {cls.__name__} has' f' neither a default nor a default_factory;' f' store_default=False cannot be set for it.' f' (AND THIS SHOULD HAVE BEEN CAUGHT IN PREP!)') outvalue = self._process_value(cls, subfieldpath, anntype, value, ioattrs) if self._create: assert out is not None storagename = (fieldname if (ioattrs is None or ioattrs.storagename is None) else ioattrs.storagename) out[storagename] = outvalue # If there's extra-attrs stored on us, check/include them. extra_attrs = getattr(obj, EXTRA_ATTRS_ATTR, None) if isinstance(extra_attrs, dict): if not _is_valid_for_codec(extra_attrs, self._codec): raise TypeError( f'Extra attrs on {fieldpath} contains data type(s)' f' not supported by json.') if self._create: assert out is not None out.update(extra_attrs) return out
def _tuple_from_input(self, cls: type, fieldpath: str, anntype: Any, value: Any, ioattrs: Optional[IOAttrs]) -> Any: out: list = [] # Because we are json-centric, we expect a list for all sequences. if type(value) is not list: raise TypeError(f'Invalid input value for "{fieldpath}";' f' expected a list, got a {type(value).__name__}') childanntypes = typing.get_args(anntype) # We should have verified this to be non-zero at prep-time. assert childanntypes if len(value) != len(childanntypes): raise ValueError(f'Invalid tuple input for "{fieldpath}";' f' expected {len(childanntypes)} values,' f' found {len(value)}.') for i, childanntype in enumerate(childanntypes): childval = value[i] # 'Any' type children; make sure they are valid json values # and then just grab them. if childanntype is typing.Any: if not _is_valid_for_codec(childval, self._codec): raise TypeError(f'Item {i} of {fieldpath} contains' f' data type(s) not supported by json.') out.append(childval) else: out.append( self._value_from_input(cls, fieldpath, childanntype, childval, ioattrs)) assert len(out) == len(childanntypes) return tuple(out)
def _value_from_input(self, cls: type, fieldpath: str, anntype: Any, value: Any, ioattrs: Optional[IOAttrs]) -> Any: """Convert an assigned value to what a dataclass field expects.""" # pylint: disable=too-many-return-statements # pylint: disable=too-many-branches origin = _get_origin(anntype) if origin is typing.Any: if not _is_valid_for_codec(value, self._codec): raise TypeError(f'Invalid value type for \'{fieldpath}\';' f' \'Any\' typed values must contain only' f' types directly supported by the specified' f' codec ({self._codec.name}); found' f' \'{type(value).__name__}\' which is not.') return value if origin is typing.Union or origin is types.UnionType: # Currently, the only unions we support are None/Value # (translated from Optional), which we verified on prep. # So let's treat this as a simple optional case. if value is None: return None childanntypes_l = [ c for c in typing.get_args(anntype) if c is not type(None) ] # noqa (pycodestyle complains about *is* with type) assert len(childanntypes_l) == 1 return self._value_from_input(cls, fieldpath, childanntypes_l[0], value, ioattrs) # Everything below this point assumes the annotation type resolves # to a concrete type. (This should have been verified at prep time). assert isinstance(origin, type) if origin in SIMPLE_TYPES: if type(value) is not origin: # Special case: if they want to coerce ints to floats, do so. if (self._coerce_to_float and origin is float and type(value) is int): return float(value) _raise_type_error(fieldpath, type(value), (origin, )) return value if origin in {list, set}: return self._sequence_from_input(cls, fieldpath, anntype, value, origin, ioattrs) if origin is tuple: return self._tuple_from_input(cls, fieldpath, anntype, value, ioattrs) if origin is dict: return self._dict_from_input(cls, fieldpath, anntype, value, ioattrs) if dataclasses.is_dataclass(origin): return self._dataclass_from_input(origin, fieldpath, value) if issubclass(origin, Enum): return enum_by_value(origin, value) if issubclass(origin, datetime.datetime): return self._datetime_from_input(cls, fieldpath, value, ioattrs) if origin is bytes: return self._bytes_from_input(origin, fieldpath, value) raise TypeError( f"Field '{fieldpath}' of type '{anntype}' is unsupported here.")
def _dict_from_input(self, cls: type, fieldpath: str, anntype: Any, value: Any, ioattrs: Optional[IOAttrs]) -> Any: # pylint: disable=too-many-branches # pylint: disable=too-many-locals if not isinstance(value, dict): raise TypeError( f'Expected a dict for \'{fieldpath}\' on {cls.__name__};' f' got a {type(value)}.') childtypes = typing.get_args(anntype) assert len(childtypes) in (0, 2) out: dict # We treat 'Any' dicts simply as json; we don't do any translating. if not childtypes or childtypes[0] is typing.Any: if not isinstance(value, dict) or not _is_valid_for_codec( value, self._codec): raise TypeError(f'Got invalid value for Dict[Any, Any]' f' at \'{fieldpath}\' on {cls.__name__};' f' all keys and values must be' f' compatible with the specified codec' f' ({self._codec.name}).') out = value else: out = {} keyanntype, valanntype = childtypes # Ok; we've got definite key/value types (which we verified as # valid during prep). Run all keys/values through it. # str keys we just take directly since that's supported by json. if keyanntype is str: for key, val in value.items(): if not isinstance(key, str): raise TypeError( f'Got invalid key type {type(key)} for' f' dict key at \'{fieldpath}\' on {cls.__name__};' f' expected a str.') out[key] = self._value_from_input(cls, fieldpath, valanntype, val, ioattrs) # int keys are stored in json as str versions of themselves. elif keyanntype is int: for key, val in value.items(): if not isinstance(key, str): raise TypeError( f'Got invalid key type {type(key)} for' f' dict key at \'{fieldpath}\' on {cls.__name__};' f' expected a str.') try: keyint = int(key) except ValueError as exc: raise TypeError( f'Got invalid key value {key} for' f' dict key at \'{fieldpath}\' on {cls.__name__};' f' expected an int in string form.') from exc out[keyint] = self._value_from_input( cls, fieldpath, valanntype, val, ioattrs) elif issubclass(keyanntype, Enum): # In prep, we verified that all these enums' values have # the same type, so we can just look at the first to see if # this is a string enum or an int enum. enumvaltype = type(next(iter(keyanntype)).value) assert enumvaltype in (int, str) if enumvaltype is str: for key, val in value.items(): try: enumval = enum_by_value(keyanntype, key) except ValueError as exc: raise ValueError( f'Got invalid key value {key} for' f' dict key at \'{fieldpath}\'' f' on {cls.__name__};' f' expected a value corresponding to' f' a {keyanntype}.') from exc out[enumval] = self._value_from_input( cls, fieldpath, valanntype, val, ioattrs) else: for key, val in value.items(): try: enumval = enum_by_value(keyanntype, int(key)) except (ValueError, TypeError) as exc: raise ValueError( f'Got invalid key value {key} for' f' dict key at \'{fieldpath}\'' f' on {cls.__name__};' f' expected {keyanntype} value (though' f' in string form).') from exc out[enumval] = self._value_from_input( cls, fieldpath, valanntype, val, ioattrs) else: raise RuntimeError(f'Unhandled dict in-key-type {keyanntype}') return out
def _dataclass_from_input(self, cls: type, fieldpath: str, values: dict) -> Any: """Given a dict, instantiates a dataclass of the given type. The dict must be in the json-friendly format as emitted from dataclass_to_dict. This means that sequence values such as tuples or sets should be passed as lists, enums should be passed as their associated values, and nested dataclasses should be passed as dicts. """ # pylint: disable=too-many-locals # pylint: disable=too-many-branches if not isinstance(values, dict): raise TypeError( f'Expected a dict for {fieldpath} on {cls.__name__};' f' got a {type(values)}.') prep = PrepSession(explicit=False).prep_dataclass(cls, recursion_level=0) assert prep is not None extra_attrs = {} # noinspection PyDataclass fields = dataclasses.fields(cls) fields_by_name = {f.name: f for f in fields} # Preprocess all fields to convert Annotated[] to contained types # and IOAttrs. parsed_field_annotations = { f.name: _parse_annotated(prep.annotations[f.name]) for f in fields } # Go through all data in the input, converting it to either dataclass # args or extra data. args: dict[str, Any] = {} for rawkey, value in values.items(): key = prep.storage_names_to_attr_names.get(rawkey, rawkey) field = fields_by_name.get(key) # Store unknown attrs off to the side (or error if desired). if field is None: if self._allow_unknown_attrs: if self._discard_unknown_attrs: continue # Treat this like 'Any' data; ensure that it is valid # raw json. if not _is_valid_for_codec(value, self._codec): raise TypeError( f'Unknown attr \'{key}\'' f' on {fieldpath} contains data type(s)' f' not supported by the specified codec' f' ({self._codec.name}).') extra_attrs[key] = value else: raise AttributeError( f"'{cls.__name__}' has no '{key}' field.") else: fieldname = field.name anntype, ioattrs = parsed_field_annotations[fieldname] subfieldpath = (f'{fieldpath}.{fieldname}' if fieldpath else fieldname) args[key] = self._value_from_input(cls, subfieldpath, anntype, value, ioattrs) # Go through all fields looking for any not yet present in our data. # If we find any such fields with a soft-default value or factory # defined, inject that soft value into our args. for key, aparsed in parsed_field_annotations.items(): if key in args: continue ioattrs = aparsed[1] if (ioattrs is not None and (ioattrs.soft_default is not ioattrs.MISSING or ioattrs.soft_default_factory is not ioattrs.MISSING)): if ioattrs.soft_default is not ioattrs.MISSING: soft_default = ioattrs.soft_default else: assert callable(ioattrs.soft_default_factory) soft_default = ioattrs.soft_default_factory() args[key] = soft_default # Make sure these values are valid since we didn't run # them through our normal input type checking. self._type_check_soft_default( value=soft_default, anntype=aparsed[0], fieldpath=(f'{fieldpath}.{key}' if fieldpath else key)) try: out = cls(**args) except Exception as exc: raise ValueError(f'Error instantiating class {cls.__name__}' f' at {fieldpath}: {exc}') from exc if extra_attrs: setattr(out, EXTRA_ATTRS_ATTR, extra_attrs) return out
def _dataclass_from_input(self, cls: type, fieldpath: str, values: dict) -> Any: """Given a dict, instantiates a dataclass of the given type. The dict must be in the json-friendly format as emitted from dataclass_to_dict. This means that sequence values such as tuples or sets should be passed as lists, enums should be passed as their associated values, and nested dataclasses should be passed as dicts. """ # pylint: disable=too-many-locals if not isinstance(values, dict): raise TypeError( f'Expected a dict for {fieldpath} on {cls.__name__};' f' got a {type(values)}.') prep = PrepSession(explicit=False).prep_dataclass(cls, recursion_level=0) assert prep is not None extra_attrs = {} # noinspection PyDataclass fields = dataclasses.fields(cls) fields_by_name = {f.name: f for f in fields} args: dict[str, Any] = {} for rawkey, value in values.items(): key = prep.storage_names_to_attr_names.get(rawkey, rawkey) field = fields_by_name.get(key) # Store unknown attrs off to the side (or error if desired). if field is None: if self._allow_unknown_attrs: if self._discard_unknown_attrs: continue # Treat this like 'Any' data; ensure that it is valid # raw json. if not _is_valid_for_codec(value, self._codec): raise TypeError( f'Unknown attr \'{key}\'' f' on {fieldpath} contains data type(s)' f' not supported by the specified codec' f' ({self._codec.name}).') extra_attrs[key] = value else: raise AttributeError( f"'{cls.__name__}' has no '{key}' field.") else: fieldname = field.name anntype = prep.annotations[fieldname] anntype, ioattrs = _parse_annotated(anntype) subfieldpath = (f'{fieldpath}.{fieldname}' if fieldpath else fieldname) args[key] = self._value_from_input(cls, subfieldpath, anntype, value, ioattrs) try: out = cls(**args) except Exception as exc: raise RuntimeError(f'Error instantiating class {cls.__name__}' f' at {fieldpath}: {exc}') from exc if extra_attrs: setattr(out, EXTRA_ATTRS_ATTR, extra_attrs) return out
def _process_dict(self, cls: type, fieldpath: str, anntype: Any, value: dict, ioattrs: Optional[IOAttrs]) -> Any: # pylint: disable=too-many-branches if not isinstance(value, dict): raise TypeError(f'Expected a dict for {fieldpath};' f' found a {type(value)}.') childtypes = typing.get_args(anntype) assert len(childtypes) in (0, 2) # We treat 'Any' dicts simply as json; we don't do any translating. if not childtypes or childtypes[0] is typing.Any: if not isinstance(value, dict) or not _is_valid_for_codec( value, self._codec): raise TypeError( f'Invalid value for Dict[Any, Any]' f' at \'{fieldpath}\' on {cls.__name__};' f' all keys and values must be directly compatible' f' with the specified codec ({self._codec.name})' f' when dict type is Any.') return value if self._create else None # Ok; we've got a definite key type (which we verified as valid # during prep). Make sure all keys match it. out: Optional[dict] = {} if self._create else None keyanntype, valanntype = childtypes # str keys we just export directly since that's supported by json. if keyanntype is str: for key, val in value.items(): if not isinstance(key, str): raise TypeError( f'Got invalid key type {type(key)} for' f' dict key at \'{fieldpath}\' on {cls.__name__};' f' expected {keyanntype}.') outval = self._process_value(cls, fieldpath, valanntype, val, ioattrs) if self._create: assert out is not None out[key] = outval # int keys are stored as str versions of themselves. elif keyanntype is int: for key, val in value.items(): if not isinstance(key, int): raise TypeError( f'Got invalid key type {type(key)} for' f' dict key at \'{fieldpath}\' on {cls.__name__};' f' expected an int.') outval = self._process_value(cls, fieldpath, valanntype, val, ioattrs) if self._create: assert out is not None out[str(key)] = outval elif issubclass(keyanntype, Enum): for key, val in value.items(): if not isinstance(key, keyanntype): raise TypeError( f'Got invalid key type {type(key)} for' f' dict key at \'{fieldpath}\' on {cls.__name__};' f' expected a {keyanntype}.') outval = self._process_value(cls, fieldpath, valanntype, val, ioattrs) if self._create: assert out is not None out[str(key.value)] = outval else: raise RuntimeError(f'Unhandled dict out-key-type {keyanntype}') return out
def _process_value(self, cls: type, fieldpath: str, anntype: Any, value: Any, ioattrs: Optional[IOAttrs]) -> Any: # pylint: disable=too-many-return-statements # pylint: disable=too-many-branches # pylint: disable=too-many-statements origin = _get_origin(anntype) if origin is typing.Any: if not _is_valid_for_codec(value, self._codec): raise TypeError( f'Invalid value type for \'{fieldpath}\';' f" 'Any' typed values must contain types directly" f' supported by the specified codec ({self._codec.name});' f' found \'{type(value).__name__}\' which is not.') return value if self._create else None if origin is typing.Union or origin is types.UnionType: # Currently, the only unions we support are None/Value # (translated from Optional), which we verified on prep. # So let's treat this as a simple optional case. if value is None: return None childanntypes_l = [ c for c in typing.get_args(anntype) if c is not type(None) ] # noqa (pycodestyle complains about *is* with type) assert len(childanntypes_l) == 1 return self._process_value(cls, fieldpath, childanntypes_l[0], value, ioattrs) # Everything below this point assumes the annotation type resolves # to a concrete type. (This should have been verified at prep time). assert isinstance(origin, type) # For simple flat types, look for exact matches: if origin in SIMPLE_TYPES: if type(value) is not origin: # Special case: if they want to coerce ints to floats, do so. if (self._coerce_to_float and origin is float and type(value) is int): return float(value) if self._create else None _raise_type_error(fieldpath, type(value), (origin, )) return value if self._create else None if origin is tuple: if not isinstance(value, tuple): raise TypeError(f'Expected a tuple for {fieldpath};' f' found a {type(value)}') childanntypes = typing.get_args(anntype) # We should have verified this was non-zero at prep-time assert childanntypes if len(value) != len(childanntypes): raise TypeError(f'Tuple at {fieldpath} contains' f' {len(value)} values; type specifies' f' {len(childanntypes)}.') if self._create: return [ self._process_value(cls, fieldpath, childanntypes[i], x, ioattrs) for i, x in enumerate(value) ] for i, x in enumerate(value): self._process_value(cls, fieldpath, childanntypes[i], x, ioattrs) return None if origin is list: if not isinstance(value, list): raise TypeError(f'Expected a list for {fieldpath};' f' found a {type(value)}') childanntypes = typing.get_args(anntype) # 'Any' type children; make sure they are valid values for # the specified codec. if len(childanntypes) == 0 or childanntypes[0] is typing.Any: for i, child in enumerate(value): if not _is_valid_for_codec(child, self._codec): raise TypeError( f'Item {i} of {fieldpath} contains' f' data type(s) not supported by the specified' f' codec ({self._codec.name}).') # Hmm; should we do a copy here? return value if self._create else None # We contain elements of some specified type. assert len(childanntypes) == 1 if self._create: return [ self._process_value(cls, fieldpath, childanntypes[0], x, ioattrs) for x in value ] for x in value: self._process_value(cls, fieldpath, childanntypes[0], x, ioattrs) return None if origin is set: if not isinstance(value, set): raise TypeError(f'Expected a set for {fieldpath};' f' found a {type(value)}') childanntypes = typing.get_args(anntype) # 'Any' type children; make sure they are valid Any values. if len(childanntypes) == 0 or childanntypes[0] is typing.Any: for child in value: if not _is_valid_for_codec(child, self._codec): raise TypeError( f'Set at {fieldpath} contains' f' data type(s) not supported by the' f' specified codec ({self._codec.name}).') return list(value) if self._create else None # We contain elements of some specified type. assert len(childanntypes) == 1 if self._create: # Note: we output json-friendly values so this becomes # a list. return [ self._process_value(cls, fieldpath, childanntypes[0], x, ioattrs) for x in value ] for x in value: self._process_value(cls, fieldpath, childanntypes[0], x, ioattrs) return None if origin is dict: return self._process_dict(cls, fieldpath, anntype, value, ioattrs) if dataclasses.is_dataclass(origin): if not isinstance(value, origin): raise TypeError(f'Expected a {origin} for {fieldpath};' f' found a {type(value)}.') return self._process_dataclass(cls, value, fieldpath) if issubclass(origin, Enum): if not isinstance(value, origin): raise TypeError(f'Expected a {origin} for {fieldpath};' f' found a {type(value)}.') # At prep-time we verified that these enums had valid value # types, so we can blindly return it here. return value.value if self._create else None if issubclass(origin, datetime.datetime): if not isinstance(value, origin): raise TypeError(f'Expected a {origin} for {fieldpath};' f' found a {type(value)}.') check_utc(value) if ioattrs is not None: ioattrs.validate_datetime(value, fieldpath) if self._codec is Codec.FIRESTORE: return value assert self._codec is Codec.JSON return [ value.year, value.month, value.day, value.hour, value.minute, value.second, value.microsecond ] if self._create else None if origin is bytes: return self._process_bytes(cls, fieldpath, value) raise TypeError( f"Field '{fieldpath}' of type '{anntype}' is unsupported here.")