|
13 | 13 | import jinja2 |
14 | 14 | from collections.abc import Callable, Sequence, Iterable |
15 | 15 | from beartype import beartype, BeartypeConf |
| 16 | +from beartype.door import is_bearable |
16 | 17 | from beartype.roar import BeartypeCallHintParamViolation |
17 | 18 | from dataclasses import dataclass, is_dataclass |
18 | 19 | from typing import overload, TypeVar, Generic, Any, Optional, Union, Literal |
@@ -263,6 +264,7 @@ def wrap(cls: type[T]) -> type[T]: |
263 | 264 | g["_get_by_aliases"] = _get_by_aliases |
264 | 265 | g["class_deserializers"] = class_deserializers |
265 | 266 | g["BeartypeCallHintParamViolation"] = BeartypeCallHintParamViolation |
| 267 | + g["is_bearable"] = is_bearable |
266 | 268 | if deserializer: |
267 | 269 | g["serde_legacy_custom_class_deserializer"] = functools.partial( |
268 | 270 | serde_legacy_custom_class_deserializer, custom=deserializer |
@@ -1158,9 +1160,11 @@ def {{func}}(cls=cls, maybe_generic=None, maybe_generic_type_vars=None, data=Non |
1158 | 1160 | if not isinstance(fake_dict["fake_key"], {{typename(t)}}): |
1159 | 1161 | raise Exception("Not a type of {{typename(t)}}") |
1160 | 1162 | {% endif %} |
1161 | | - return {{rvalue(arg(t))}} |
| 1163 | + res = {{rvalue(arg(t))}} |
| 1164 | + ensure(is_bearable(res, {{typename(t)}}), "object is not of type '{{typename(t)}}'") |
| 1165 | + return res |
1162 | 1166 | except Exception as e: |
1163 | | - errors.append(f' Failed to deserialize into {{typename(t)}}: {e}') |
| 1167 | + errors.append(f" Failed to deserialize into {{typename(t)}}: {e}") |
1164 | 1168 | {% endfor %} |
1165 | 1169 | raise SerdeError("Can not deserialize " + repr(data) + " of type " + \ |
1166 | 1170 | typename(type(data)) + " into {{union_name}}.\\nReasons:\\n" + "\\n".join(errors)) |
|
0 commit comments