Fatal1ty / mashumaro

Fast and well tested serialization library
Apache License 2.0
774 stars 45 forks source link

collections.OrderedDict is not hashable when using inside typing_extensions.Annotated on Python < 3.9 #138

Closed Fatal1ty closed 1 year ago

Fatal1ty commented 1 year ago

Description

TypeError is raised when using collections.OrderedDict object as annotation in typing_extensions.Annotated on python<3.9:

TypeError: unhashable type: 'collections.OrderedDict'

Originally reported by @hhcs9527: https://github.com/Fatal1ty/mashumaro/pull/134#issuecomment-1679931209 Related to:

What I Did

import collections
import typing
from dataclasses import dataclass
from typing import OrderedDict, Type

from typing_extensions import Annotated

from mashumaro.mixins.json import DataClassJSONMixin

def kwtypes(**kwargs) -> OrderedDict[str, Type]:
    """
    This is a small helper function to convert the keyword arguments to an OrderedDict of types.

    .. code-block:: python

        kwtypes(a=int, b=str)
    """
    d = collections.OrderedDict()
    for k, v in kwargs.items():
        d[k] = v
    return d

@dataclass
class test(DataClassJSONMixin):
    b: typing.List[Annotated[int, "parquet"]]
    c: typing.Dict[str, Annotated[int, kwtypes(Name=str, Age=int)]]