lovasoa / marshmallow_dataclass

Automatic generation of marshmallow schemas from dataclasses.
https://lovasoa.github.io/marshmallow_dataclass/html/marshmallow_dataclass.html
MIT License
456 stars 78 forks source link

Union with generic types is broken #250

Closed mvanderlee closed 4 months ago

mvanderlee commented 11 months ago
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.

lovasoa commented 11 months ago

Thanks! Can you open a pr ?

LostInDarkMath commented 4 months ago

@jasle Can you pls submit a PR?

jasle commented 4 months ago

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.