|
15 | 15 | from typing import List, Optional, Union |
16 | 16 |
|
17 | 17 | from marshmallow import RAISE, fields |
18 | | -from marshmallow.exceptions import ValidationError |
| 18 | +from marshmallow.exceptions import ValidationError, FieldInstanceResolutionError |
19 | 19 | from marshmallow.fields import Field, Nested |
20 | | -from marshmallow.utils import FieldInstanceResolutionError, from_iso_datetime, resolve_field_instance |
| 20 | + |
| 21 | +try: |
| 22 | + # marshmallow 4.x |
| 23 | + from marshmallow.class_registry import resolve_field_instance |
| 24 | +except ImportError: |
| 25 | + # marshmallow 3.x |
| 26 | + from marshmallow.utils import resolve_field_instance |
| 27 | + |
| 28 | +# Custom implementation for from_iso_datetime compatibility |
| 29 | +def from_iso_datetime(value): |
| 30 | + """Parse an ISO8601 datetime string, handling the 'Z' suffix.""" |
| 31 | + from datetime import datetime |
| 32 | + if isinstance(value, str): |
| 33 | + # Replace 'Z' with '+00:00' for compatibility with datetime.fromisoformat |
| 34 | + if value.endswith('Z'): |
| 35 | + value = value[:-1] + '+00:00' |
| 36 | + return datetime.fromisoformat(value) |
| 37 | + return value |
21 | 38 |
|
22 | 39 | from ..._utils._arm_id_utils import AMLVersionedArmId, is_ARM_id_for_resource, parse_name_label, parse_name_version |
23 | 40 | from ..._utils._experimental import _is_warning_cached |
@@ -435,13 +452,13 @@ def __init__(self, union_fields: List[fields.Field], is_strict=False, **kwargs): |
435 | 452 | super().__init__(**kwargs) |
436 | 453 | try: |
437 | 454 | # add the validation and make sure union_fields must be subclasses or instances of |
438 | | - # marshmallow.base.FieldABC |
| 455 | + # marshmallow fields |
439 | 456 | self._union_fields = [resolve_field_instance(cls_or_instance) for cls_or_instance in union_fields] |
440 | 457 | # TODO: make serialization/de-serialization work in the same way as json schema when is_strict is True |
441 | 458 | self.is_strict = is_strict # S\When True, combine fields with oneOf instead of anyOf at schema generation |
442 | 459 | except FieldInstanceResolutionError as error: |
443 | 460 | raise ValueError( |
444 | | - 'Elements of "union_fields" must be subclasses or instances of marshmallow.base.FieldABC.' |
| 461 | + 'Elements of "union_fields" must be subclasses or instances of marshmallow fields.' |
445 | 462 | ) from error |
446 | 463 |
|
447 | 464 | @property |
@@ -878,7 +895,7 @@ def __init__(self, experimental_field: fields.Field, **kwargs): |
878 | 895 | self.required = experimental_field.required |
879 | 896 | except FieldInstanceResolutionError as error: |
880 | 897 | raise ValueError( |
881 | | - '"experimental_field" must be subclasses or instances of marshmallow.base.FieldABC.' |
| 898 | + '"experimental_field" must be subclasses or instances of marshmallow fields.' |
882 | 899 | ) from error |
883 | 900 |
|
884 | 901 | @property |
|
0 commit comments