lovasoa / marshmallow_dataclass

Automatic generation of marshmallow schemas from dataclasses.

Home Page:https://lovasoa.github.io/marshmallow_dataclass/html/marshmallow_dataclass.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AttributeError: 'types.UnionType' object has no attribute '__name__'

LostInDarkMath opened this issue · comments

Hi there! It seems like your library does not support Union types.

Working

@dataclass(frozen=True)
class Bar:
    foo: Optional[str] = None

Not working

@dataclass(frozen=True)
class Bar:
    foo: str | None = None

Error message:

/home/wsontopski/Documents/Gitlab/maddox/backend/src/shared/nats/serialization.py:102: in _get_marshmallow_schema_of_dataclass
    return marshmallow_dataclass.class_schema(clazz=class_obj)
/home/wsontopski/.cache/pypoetry/virtualenvs/maddox-E1xGxQ_g-py3.10/lib/python3.10/site-packages/marshmallow_dataclass/__init__.py:356: in class_schema
    return _internal_class_schema(clazz, base_schema, clazz_frame)
/home/wsontopski/.cache/pypoetry/virtualenvs/maddox-E1xGxQ_g-py3.10/lib/python3.10/site-packages/marshmallow_dataclass/__init__.py:402: in _internal_class_schema
    attributes.update(
/home/wsontopski/.cache/pypoetry/virtualenvs/maddox-E1xGxQ_g-py3.10/lib/python3.10/site-packages/marshmallow_dataclass/__init__.py:405: in <genexpr>
    field_for_schema(
/home/wsontopski/.cache/pypoetry/virtualenvs/maddox-E1xGxQ_g-py3.10/lib/python3.10/site-packages/marshmallow_dataclass/__init__.py:729: in field_for_schema
    or _internal_class_schema(typ, base_schema, typ_frame)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

clazz = str | None, base_schema = None
clazz_frame = <frame at 0x7fc340fc2c20, file '/home/wsontopski/Documents/Gitlab/maddox/backend/src/shared/nats/serialization.py', line 102, code _get_marshmallow_schema_of_dataclass>

    @lru_cache(maxsize=MAX_CLASS_SCHEMA_CACHE_SIZE)
    def _internal_class_schema(
        clazz: type,
        base_schema: Optional[Type[marshmallow.Schema]] = None,
        clazz_frame: types.FrameType = None,
    ) -> Type[marshmallow.Schema]:
>       _RECURSION_GUARD.seen_classes[clazz] = clazz.__name__
E       AttributeError: 'types.UnionType' object has no attribute '__name__'

/home/wsontopski/.cache/pypoetry/virtualenvs/maddox-E1xGxQ_g-py3.10/lib/python3.10/site-packages/marshmallow_dataclass/__init__.py:367: AttributeError

Context

Python 3.10.3
Marshmallow-Dataclass 8.5.8

Duplicate of #193

commented

is it ? It look like this is different as here the issue is the | annotation, ie, PEP-604.

@lovasoa do you know of any recent changes to support this new type annotation ?

I'm facing the same issue. Looks like it doesn't support the new syntax for union types