Skip to content

Commit 5ad82de

Browse files
committed
refactor: implement BaseModel.get_field_multiplicity
1 parent 5607f9c commit 5ad82de

4 files changed

Lines changed: 161 additions & 152 deletions

File tree

scim2_models/base.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from typing import Generic
88
from typing import Optional
99
from typing import TypeVar
10-
from typing import Union
1110
from typing import get_args
1211
from typing import get_origin
1312

@@ -32,13 +31,7 @@
3231
from scim2_models.utils import normalize_attribute_name
3332
from scim2_models.utils import to_camel
3433

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
4235

4336
ReferenceTypes = TypeVar("ReferenceTypes")
4437
URIReference = NewType("URIReference", str)
@@ -456,6 +449,18 @@ def get_field_root_type(cls, attribute_name: str) -> type:
456449

457450
return attribute_type
458451

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+
459464
@field_validator("*")
460465
@classmethod
461466
def check_request_attributes_mutability(

scim2_models/rfc7643/resource.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,6 @@ def from_schema(cls, schema) -> "Resource":
245245
AnyResource = TypeVar("AnyResource", bound="Resource")
246246

247247

248-
def is_multiple(field):
249-
return "list" in str(field.annotation).lower()
250-
251-
252248
def dedicated_attributes(model):
253249
"""Return attributes that are not members of parent classes."""
254250

@@ -332,7 +328,7 @@ def model_attribute_to_attribute(model, attribute_name):
332328
return Attribute(
333329
name=field_info.serialization_alias or attribute_name,
334330
type=attribute_type,
335-
multi_valued=is_multiple(field_info),
331+
multi_valued=model.get_field_multiplicity(attribute_name),
336332
description=field_info.description,
337333
canonical_values=field_info.examples,
338334
required=model.get_field_annotation(attribute_name, Required),

scim2_models/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import re
22
from typing import Optional
3+
from typing import Union
34

45
from pydantic.alias_generators import to_snake
56

7+
try:
8+
from types import UnionType
9+
10+
UNION_TYPES = [Union, UnionType]
11+
except ImportError:
12+
# Python 3.9 has no UnionType
13+
UNION_TYPES = [Union]
14+
615

716
def int_to_str(status: Optional[int]) -> Optional[str]:
817
return None if status is None else str(status)

0 commit comments

Comments
 (0)