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

Union with generic types is broken

mvanderlee opened this issue · comments

from typing import Union
import marshmallow.fields as mf
from marshmallow_dataclass import NewType, dataclass
from webargs.fields import DelimitedList

DelimitedListInt = NewType('DelimitedListInt', list[int], field=lambda *args, **kwargs: DelimitedList(mf.Integer(), *args, **kwargs))

@dataclass
class Test:
  myint: Union[DelimitedListInt, list[int]]


Test.Schema().load({'myint': '1,2,3'})

Raises:

TypeError: isinstance() argument 2 cannot be a parameterized generic

The issue is that typeguard does a check of isinstance([1, 2, 3], list[int]) which is not allowed.
Updating to typeguard 4.0.0 fixes the issue. I've also verified that the latest version, 4.1.5 works.

Thanks! Can you open a pr ?

@jasle Can you pls submit a PR?

typeguard 4.0.0 and newer does drop the support for python older than 3.7.4, so it is not possible to fix this for all python versions currently supported by python-marshmallow.