Skip to content

Commit 1127332

Browse files
committed
Fix for typeguard version 3
As of version 3 of typeguard, check_type raises typeguard.TypeCheckError instead of TypeError.
1 parent cddce9a commit 1127332

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# marshmallow\_dataclass change log
22

3+
## v8.5.12 (unreleased)
4+
5+
- Fix to work with typeguard 3.x
6+
37
## v8.5.11 (2023-01-08)
48

59
- Replace the use of `marshmallow-enum` with the native

marshmallow_dataclass/union_field.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import typeguard
55
from marshmallow import fields, Schema, ValidationError
66

7+
try:
8+
from typeguard import TypeCheckError # type: ignore[attr-defined]
9+
except ImportError:
10+
# typeguard < 3
11+
TypeCheckError = TypeError
12+
713

814
class Union(fields.Field):
915
"""A union field, composed other `Field` classes or instances.
@@ -44,7 +50,7 @@ def _serialize(self, value: Any, attr: Optional[str], obj, **kwargs) -> Any:
4450
value=value, expected_type=typ, argname=attr or "anonymous"
4551
)
4652
return field._serialize(value, attr, obj, **kwargs)
47-
except TypeError as e:
53+
except TypeCheckError as e:
4854
errors.append(e)
4955
raise TypeError(
5056
f"Unable to serialize value with any of the fields in the union: {errors}"
@@ -59,7 +65,7 @@ def _deserialize(self, value: Any, attr: Optional[str], data, **kwargs) -> Any:
5965
value=result, expected_type=typ, argname=attr or "anonymous"
6066
)
6167
return result
62-
except (TypeError, ValidationError) as e:
68+
except (TypeCheckError, ValidationError) as e:
6369
errors.append(e)
6470

6571
raise ValidationError(errors)

0 commit comments

Comments
 (0)