|
7 | 7 | from typing import Generic |
8 | 8 | from typing import Optional |
9 | 9 | from typing import TypeVar |
10 | | -from typing import Union |
11 | 10 | from typing import get_args |
12 | 11 | from typing import get_origin |
13 | 12 |
|
|
32 | 31 | from scim2_models.utils import normalize_attribute_name |
33 | 32 | from scim2_models.utils import to_camel |
34 | 33 |
|
35 | | -try: |
36 | | - from types import UnionType |
37 | | - |
38 | | - UNION_TYPES = [Union, UnionType] |
39 | | -except ImportError: |
40 | | - # Python 3.9 has no UnionType |
41 | | - UNION_TYPES = [Union] |
| 34 | +from .utils import UNION_TYPES |
42 | 35 |
|
43 | 36 | ReferenceTypes = TypeVar("ReferenceTypes") |
44 | 37 | URIReference = NewType("URIReference", str) |
@@ -456,6 +449,18 @@ def get_field_root_type(cls, attribute_name: str) -> type: |
456 | 449 |
|
457 | 450 | return attribute_type |
458 | 451 |
|
| 452 | + @classmethod |
| 453 | + def get_field_multiplicity(cls, attribute_name: str) -> bool: |
| 454 | + """Indicate whether a field holds multiple values.""" |
| 455 | + attribute_type = cls.model_fields[attribute_name].annotation |
| 456 | + |
| 457 | + # extract 'x' from 'Optional[x]' |
| 458 | + if get_origin(attribute_type) in UNION_TYPES: |
| 459 | + attribute_type = get_args(attribute_type)[0] |
| 460 | + |
| 461 | + origin = get_origin(attribute_type) |
| 462 | + return isinstance(origin, type) and issubclass(origin, list) |
| 463 | + |
459 | 464 | @field_validator("*") |
460 | 465 | @classmethod |
461 | 466 | def check_request_attributes_mutability( |
|
0 commit comments