Skip to content

Commit

Permalink
Fix typing of Union._serialize parameter
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
dairiki committed Sep 23, 2022
1 parent 1518691 commit 65cd843
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions marshmallow_dataclass/union_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 65cd843

Please # to comment.