marshmallow_dataclass icon indicating copy to clipboard operation
marshmallow_dataclass copied to clipboard

Union with generic types is broken

Open mvanderlee opened this issue 1 year ago • 1 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.

mvanderlee avatar Sep 18 '23 16:09 mvanderlee

Thanks! Can you open a pr ?

lovasoa avatar Sep 18 '23 17:09 lovasoa

@jasle Can you pls submit a PR?

LostInDarkMath avatar May 08 '24 11:05 LostInDarkMath

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.

jasle avatar May 08 '24 21:05 jasle