How to use the mashumaro.exceptions.UnserializableField function in mashumaro

To help you get started, we’ve selected a few mashumaro examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Fatal1ty / mashumaro / tests / test_data_types.py View on Github external
def test_unsupported_generic_field_types(x_type, generic_type):
    with pytest.raises(UnserializableField):
        @dataclass
        class _(DataClassDictMixin):
            # noinspection PyTypeChecker
            x: generic_type[x_type]
github Fatal1ty / mashumaro / mashumaro / serializer / base / metaprogramming.py View on Github external
if is_generic(ftype):
                    return f'[{inner_expr()} for value in {value_name}]'
                elif ftype is list:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.List[T] instead')
                elif ftype is collections.deque:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Deque[T] instead')
                elif ftype is tuple:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Tuple[T] instead')
                elif ftype is set:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Set[T] instead')
                elif ftype is frozenset:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.FrozenSet[T] instead')
            elif issubclass(origin_type, typing.ChainMap):
                if ftype is collections.ChainMap:
                    raise UnserializableField(
                        fname, ftype, parent,
                        'Use typing.ChainMap[KT,VT] instead'
                    )
                elif is_generic(ftype):
                    if is_dataclass(args[0]):
                        raise UnserializableDataError(
                            'ChainMaps with dataclasses as keys '
                            'are not supported by mashumaro')
                    else:
                        return f'[{{{inner_expr(0,"key")}:{inner_expr(1)} ' \
                               f'for key,value in m.items()}} ' \
                               f'for m in value.maps]'
github Fatal1ty / mashumaro / mashumaro / serializer / base / metaprogramming.py View on Github external
return f'tuple([{inner_expr()} for value in {value_name}])'
                elif ftype is tuple:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Tuple[T] instead')
            elif issubclass(origin_type, typing.FrozenSet):
                if is_generic(ftype):
                    return f'frozenset([{inner_expr()} ' \
                           f'for value in {value_name}])'
                elif ftype is frozenset:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.FrozenSet[T] instead')
            elif issubclass(origin_type, typing.AbstractSet):
                if is_generic(ftype):
                    return f'set([{inner_expr()} for value in {value_name}])'
                elif ftype is set:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Set[T] instead')
            elif issubclass(origin_type, typing.ChainMap):
                if ftype is collections.ChainMap:
                    raise UnserializableField(
                        fname, ftype, parent,
                        'Use typing.ChainMap[KT,VT] instead'
                    )
                elif is_generic(ftype):
                    if is_dataclass(args[0]):
                        raise UnserializableDataError(
                            'ChainMaps with dataclasses as keys '
                            'are not supported by mashumaro')
                    else:
                        return f'collections.ChainMap(' \
                               f'*[{{{inner_expr(0,"key")}:{inner_expr(1)} ' \
                               f'for key, value in m.items()}} ' \
github Fatal1ty / mashumaro / mashumaro / serializer / base / metaprogramming.py View on Github external
return self._pack_value(fname, args[arg_num], parent, v_name)

            if issubclass(origin_type, (typing.List,
                                        typing.Deque,
                                        typing.Tuple,
                                        typing.AbstractSet)):
                if is_generic(ftype):
                    return f'[{inner_expr()} for value in {value_name}]'
                elif ftype is list:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.List[T] instead')
                elif ftype is collections.deque:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Deque[T] instead')
                elif ftype is tuple:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Tuple[T] instead')
                elif ftype is set:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Set[T] instead')
                elif ftype is frozenset:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.FrozenSet[T] instead')
            elif issubclass(origin_type, typing.ChainMap):
                if ftype is collections.ChainMap:
                    raise UnserializableField(
                        fname, ftype, parent,
                        'Use typing.ChainMap[KT,VT] instead'
                    )
                elif is_generic(ftype):
                    if is_dataclass(args[0]):
                        raise UnserializableDataError(
github Fatal1ty / mashumaro / mashumaro / serializer / base / metaprogramming.py View on Github external
return f'[{inner_expr()} for value in {value_name}]'
                elif ftype is list:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.List[T] instead')
            elif issubclass(origin_type, typing.Deque):
                if is_generic(ftype):
                    return f'collections.deque([{inner_expr()} ' \
                           f'for value in {value_name}])'
                elif ftype is collections.deque:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Deque[T] instead')
            elif issubclass(origin_type, typing.Tuple):
                if is_generic(ftype):
                    return f'tuple([{inner_expr()} for value in {value_name}])'
                elif ftype is tuple:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Tuple[T] instead')
            elif issubclass(origin_type, typing.FrozenSet):
                if is_generic(ftype):
                    return f'frozenset([{inner_expr()} ' \
                           f'for value in {value_name}])'
                elif ftype is frozenset:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.FrozenSet[T] instead')
            elif issubclass(origin_type, typing.AbstractSet):
                if is_generic(ftype):
                    return f'set([{inner_expr()} for value in {value_name}])'
                elif ftype is set:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Set[T] instead')
            elif issubclass(origin_type, typing.ChainMap):
                if ftype is collections.ChainMap:
github Fatal1ty / mashumaro / mashumaro / serializer / base / metaprogramming.py View on Github external
raise UnserializableField(
                        fname, ftype, parent,
                        'Use typing.ChainMap[KT,VT] instead'
                    )
                elif is_generic(ftype):
                    if is_dataclass(args[0]):
                        raise UnserializableDataError(
                            'ChainMaps with dataclasses as keys '
                            'are not supported by mashumaro')
                    else:
                        return f'[{{{inner_expr(0,"key")}:{inner_expr(1)} ' \
                               f'for key,value in m.items()}} ' \
                               f'for m in value.maps]'
            elif issubclass(origin_type, typing.Mapping):
                if ftype is dict:
                    raise UnserializableField(
                        fname, ftype, parent,
                        'Use typing.Dict[KT,VT] or Mapping[KT,VT] instead'
                    )
                elif is_generic(ftype):
                    if is_dataclass(args[0]):
                        raise UnserializableDataError(
                            'Mappings with dataclasses as keys '
                            'are not supported by mashumaro')
                    else:
                        return f'{{{inner_expr(0,"key")}: {inner_expr(1)} ' \
                               f'for key, value in {value_name}.items()}}'
            elif issubclass(origin_type, typing.ByteString):
                return f'{value_name} if use_bytes else ' \
                       f'encodebytes({value_name}).decode()'
            elif issubclass(origin_type, str):
                return value_name
github Fatal1ty / mashumaro / mashumaro / serializer / base / metaprogramming.py View on Github external
'TypeVars are not supported by mashumaro')
            else:
                raise UnserializableDataError(
                    f'{ftype} as a field type is not supported by mashumaro')
        elif issubclass(origin_type, typing.Collection):
            args = getattr(ftype, '__args__', ())

            def inner_expr(arg_num=0, v_name='value'):
                return self._unpack_field_value(
                    fname, args[arg_num], parent, v_name)

            if issubclass(origin_type, typing.List):
                if is_generic(ftype):
                    return f'[{inner_expr()} for value in {value_name}]'
                elif ftype is list:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.List[T] instead')
            elif issubclass(origin_type, typing.Deque):
                if is_generic(ftype):
                    return f'collections.deque([{inner_expr()} ' \
                           f'for value in {value_name}])'
                elif ftype is collections.deque:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Deque[T] instead')
            elif issubclass(origin_type, typing.Tuple):
                if is_generic(ftype):
                    return f'tuple([{inner_expr()} for value in {value_name}])'
                elif ftype is tuple:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Tuple[T] instead')
            elif issubclass(origin_type, typing.FrozenSet):
                if is_generic(ftype):
github Fatal1ty / mashumaro / mashumaro / serializer / base / metaprogramming.py View on Github external
elif origin_type in (bool, NoneType):
            return value_name
        elif origin_type in (datetime.datetime, datetime.date, datetime.time):
            return f'{value_name} if use_datetime else {value_name}.isoformat()'
        elif origin_type is datetime.timedelta:
            return f'{value_name}.total_seconds()'
        elif origin_type is datetime.timezone:
            return f'{value_name}.tzname(None)'
        elif origin_type is uuid.UUID:
            return f'str({value_name})'
        elif origin_type is Decimal:
            return f'str({value_name})'
        elif origin_type is Fraction:
            return f'str({value_name})'

        raise UnserializableField(fname, ftype, parent)
github Fatal1ty / mashumaro / mashumaro / serializer / base / metaprogramming.py View on Github external
raise UnserializableDataError(
                    f'{ftype} as a field type is not supported by mashumaro')
        elif issubclass(origin_type, typing.Collection):
            args = getattr(ftype, '__args__', ())

            def inner_expr(arg_num=0, v_name='value'):
                return self._pack_value(fname, args[arg_num], parent, v_name)

            if issubclass(origin_type, (typing.List,
                                        typing.Deque,
                                        typing.Tuple,
                                        typing.AbstractSet)):
                if is_generic(ftype):
                    return f'[{inner_expr()} for value in {value_name}]'
                elif ftype is list:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.List[T] instead')
                elif ftype is collections.deque:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Deque[T] instead')
                elif ftype is tuple:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Tuple[T] instead')
                elif ftype is set:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.Set[T] instead')
                elif ftype is frozenset:
                    raise UnserializableField(
                        fname, ftype, parent, 'Use typing.FrozenSet[T] instead')
            elif issubclass(origin_type, typing.ChainMap):
                if ftype is collections.ChainMap:
                    raise UnserializableField(
github Fatal1ty / mashumaro / mashumaro / serializer / base / metaprogramming.py View on Github external
fname, ftype, parent,
                        'Use typing.ChainMap[KT,VT] instead'
                    )
                elif is_generic(ftype):
                    if is_dataclass(args[0]):
                        raise UnserializableDataError(
                            'ChainMaps with dataclasses as keys '
                            'are not supported by mashumaro')
                    else:
                        return f'collections.ChainMap(' \
                               f'*[{{{inner_expr(0,"key")}:{inner_expr(1)} ' \
                               f'for key, value in m.items()}} ' \
                               f'for m in {value_name}])'
            elif issubclass(origin_type, typing.Mapping):
                if ftype is dict:
                    raise UnserializableField(
                        fname, ftype, parent,
                        'Use typing.Dict[KT,VT] or Mapping[KT,VT] instead'
                    )
                elif is_generic(ftype):
                    if is_dataclass(args[0]):
                        raise UnserializableDataError(
                            'Mappings with dataclasses as keys '
                            'are not supported by mashumaro')
                    else:
                        return f'{{{inner_expr(0,"key")}: {inner_expr(1)} ' \
                               f'for key, value in {value_name}.items()}}'
            elif issubclass(origin_type, typing.ByteString):
                if origin_type is bytes:
                    return f'{value_name} if use_bytes else ' \
                           f'decodebytes({value_name}.encode())'
                elif origin_type is bytearray: