From 65cd843f3c618d0029cb083aa94be7312fac7c0f Mon Sep 17 00:00:00 2001 From: Jeff Dairiki Date: Fri, 23 Sep 2022 11:37:45 -0700 Subject: [PATCH] Fix typing of Union._serialize parameter This tracks a change in type of the `attr` parameter to `Field._serialize` made in marshmallow 3.18.0. Here we also switch to using keyword args to typeguard.check_type in an attempt to protect against upcoming [changes in its signature](https://typeguard.readthedocs.io/en/latest/api.html#typeguard.check_type). --- marshmallow_dataclass/union_field.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/marshmallow_dataclass/union_field.py b/marshmallow_dataclass/union_field.py index 91464576..4b1b42a7 100644 --- a/marshmallow_dataclass/union_field.py +++ b/marshmallow_dataclass/union_field.py @@ -34,13 +34,15 @@ def _bind_to_schema(self, field_name: str, schema: Schema) -> None: self.union_fields = new_union_fields - def _serialize(self, value: Any, attr: str, obj, **kwargs) -> Any: + def _serialize(self, value: Any, attr: Optional[str], obj, **kwargs) -> Any: errors = [] if value is None: return value for typ, field in self.union_fields: try: - typeguard.check_type(attr, value, typ) + typeguard.check_type( + value=value, expected_type=typ, argname=attr or "anonymous" + ) return field._serialize(value, attr, obj, **kwargs) except TypeError as e: errors.append(e) @@ -53,7 +55,9 @@ def _deserialize(self, value: Any, attr: Optional[str], data, **kwargs) -> Any: for typ, field in self.union_fields: try: result = field.deserialize(value, **kwargs) - typeguard.check_type(attr or "anonymous", result, typ) + typeguard.check_type( + value=result, expected_type=typ, argname=attr or "anonymous" + ) return result except (TypeError, ValidationError) as e: errors.append(e)