|
15 | 15 | from typing import List, Optional, Union |
16 | 16 |
|
17 | 17 | from marshmallow import RAISE, fields |
18 | | -from marshmallow.exceptions import ValidationError, FieldInstanceResolutionError |
| 18 | +from marshmallow.exceptions import ValidationError, MarshmallowError |
19 | 19 | from marshmallow.fields import Field, Nested |
20 | 20 |
|
| 21 | +def resolve_field_instance(cls_or_instance): |
| 22 | + """Resolve a field class or instance to a field instance. |
| 23 | + |
| 24 | + This replaces the marshmallow resolve_field_instance utility which |
| 25 | + was removed in marshmallow 4.x. |
| 26 | + |
| 27 | + :param cls_or_instance: Field class or instance |
| 28 | + :return: Field instance |
| 29 | + """ |
| 30 | + if isinstance(cls_or_instance, Field): |
| 31 | + return cls_or_instance |
| 32 | + elif isinstance(cls_or_instance, type) and issubclass(cls_or_instance, Field): |
| 33 | + return cls_or_instance() |
| 34 | + else: |
| 35 | + raise MarshmallowError(f"Expected Field class or instance, got {type(cls_or_instance)}") |
| 36 | + |
| 37 | + |
21 | 38 | try: |
22 | | - # marshmallow 4.x |
23 | | - from marshmallow.class_registry import resolve_field_instance |
| 39 | + # marshmallow 3.x fallback - try to import if available |
| 40 | + from marshmallow.utils import resolve_field_instance as _original_resolve_field_instance |
| 41 | + # If import succeeds, use the original function |
| 42 | + resolve_field_instance = _original_resolve_field_instance |
24 | 43 | except ImportError: |
25 | | - # marshmallow 3.x |
26 | | - from marshmallow.utils import resolve_field_instance |
| 44 | + # marshmallow 4.x or function not available - use our implementation above |
| 45 | + pass |
27 | 46 |
|
28 | 47 | from ..._utils._arm_id_utils import ( |
29 | 48 | AMLVersionedArmId, |
@@ -481,7 +500,7 @@ def __init__(self, union_fields: List[fields.Field], is_strict=False, **kwargs): |
481 | 500 | self._union_fields = [resolve_field_instance(cls_or_instance) for cls_or_instance in union_fields] |
482 | 501 | # TODO: make serialization/de-serialization work in the same way as json schema when is_strict is True |
483 | 502 | self.is_strict = is_strict # S\When True, combine fields with oneOf instead of anyOf at schema generation |
484 | | - except FieldInstanceResolutionError as error: |
| 503 | + except MarshmallowError as error: |
485 | 504 | raise ValueError( |
486 | 505 | 'Elements of "union_fields" must be subclasses or instances of marshmallow fields.' |
487 | 506 | ) from error |
@@ -924,7 +943,7 @@ def __init__(self, experimental_field: fields.Field, **kwargs): |
924 | 943 | try: |
925 | 944 | self._experimental_field = resolve_field_instance(experimental_field) |
926 | 945 | self.required = experimental_field.required |
927 | | - except FieldInstanceResolutionError as error: |
| 946 | + except MarshmallowError as error: |
928 | 947 | raise ValueError('"experimental_field" must be subclasses or instances of marshmallow fields.') from error |
929 | 948 |
|
930 | 949 | @property |
|
0 commit comments