23
23
from scim2_models .annotations import Required
24
24
from scim2_models .annotations import Returned
25
25
from scim2_models .context import Context
26
- from scim2_models .utils import UNION_TYPES
27
- from scim2_models .utils import find_field_name
28
- from scim2_models .utils import normalize_attribute_name
29
- from scim2_models .utils import to_camel
26
+ from scim2_models .utils import _UNION_TYPES
27
+ from scim2_models .utils import _find_field_name
28
+ from scim2_models .utils import _normalize_attribute_name
29
+ from scim2_models .utils import _to_camel
30
30
31
31
32
- def contains_attribute_or_subattributes (
32
+ def _contains_attribute_or_subattributes (
33
33
attribute_urns : list [str ], attribute_urn : str
34
34
) -> bool :
35
35
return attribute_urn in attribute_urns or any (
@@ -43,8 +43,8 @@ class BaseModel(PydanticBaseModel):
43
43
44
44
model_config = ConfigDict (
45
45
alias_generator = AliasGenerator (
46
- validation_alias = normalize_attribute_name ,
47
- serialization_alias = to_camel ,
46
+ validation_alias = _normalize_attribute_name ,
47
+ serialization_alias = _to_camel ,
48
48
),
49
49
validate_assignment = True ,
50
50
populate_by_name = True ,
@@ -77,7 +77,7 @@ def get_field_root_type(cls, attribute_name: str) -> Optional[type]:
77
77
attribute_type = cls .model_fields [attribute_name ].annotation
78
78
79
79
# extract 'x' from 'Optional[x]'
80
- if get_origin (attribute_type ) in UNION_TYPES :
80
+ if get_origin (attribute_type ) in _UNION_TYPES :
81
81
attribute_type = get_args (attribute_type )[0 ]
82
82
83
83
# extract 'x' from 'List[x]'
@@ -93,7 +93,7 @@ def get_field_multiplicity(cls, attribute_name: str) -> bool:
93
93
attribute_type = cls .model_fields [attribute_name ].annotation
94
94
95
95
# extract 'x' from 'Optional[x]'
96
- if get_origin (attribute_type ) in UNION_TYPES :
96
+ if get_origin (attribute_type ) in _UNION_TYPES :
97
97
attribute_type = get_args (attribute_type )[0 ]
98
98
99
99
origin = get_origin (attribute_type )
@@ -159,7 +159,7 @@ def normalize_dict_keys(
159
159
result = {}
160
160
161
161
for key , val in input_dict .items ():
162
- field_name = find_field_name (model_class , key )
162
+ field_name = _find_field_name (model_class , key )
163
163
field_type = (
164
164
model_class .get_field_root_type (field_name ) if field_name else None
165
165
)
@@ -170,7 +170,7 @@ def normalize_dict_keys(
170
170
if field_name and field_type == Any :
171
171
result [key ] = normalize_value (val )
172
172
else :
173
- result [normalize_attribute_name (key )] = normalize_value (
173
+ result [_normalize_attribute_name (key )] = normalize_value (
174
174
val , field_type
175
175
)
176
176
@@ -285,11 +285,11 @@ def check_replacement_request_mutability(
285
285
and issubclass (cls , Resource )
286
286
and original is not None
287
287
):
288
- cls .check_mutability_issues (original , obj )
288
+ cls ._check_mutability_issues (original , obj )
289
289
return obj
290
290
291
291
@classmethod
292
- def check_mutability_issues (
292
+ def _check_mutability_issues (
293
293
cls , original : "BaseModel" , replacement : "BaseModel"
294
294
) -> None :
295
295
"""Compare two instances, and check for differences of values on the fields marked as immutable."""
@@ -316,9 +316,9 @@ def check_mutability_issues(
316
316
original_val = getattr (original , field_name )
317
317
replacement_value = getattr (replacement , field_name )
318
318
if original_val is not None and replacement_value is not None :
319
- cls .check_mutability_issues (original_val , replacement_value )
319
+ cls ._check_mutability_issues (original_val , replacement_value )
320
320
321
- def set_complex_attribute_urns (self ) -> None :
321
+ def _set_complex_attribute_urns (self ) -> None :
322
322
"""Navigate through attributes and sub-attributes of type ComplexAttribute, and mark them with a '_attribute_urn' attribute.
323
323
324
324
'_attribute_urn' will later be used by 'get_attribute_urn'.
@@ -359,14 +359,14 @@ def scim_serializer(
359
359
scim_ctx = info .context .get ("scim" ) if info .context else None
360
360
361
361
if scim_ctx and Context .is_request (scim_ctx ):
362
- value = self .scim_request_serializer (value , info )
362
+ value = self ._scim_request_serializer (value , info )
363
363
364
364
if scim_ctx and Context .is_response (scim_ctx ):
365
- value = self .scim_response_serializer (value , info )
365
+ value = self ._scim_response_serializer (value , info )
366
366
367
367
return value
368
368
369
- def scim_request_serializer (self , value : Any , info : FieldSerializationInfo ) -> Any :
369
+ def _scim_request_serializer (self , value : Any , info : FieldSerializationInfo ) -> Any :
370
370
"""Serialize the fields according to mutability indications passed in the serialization context."""
371
371
mutability = self .get_field_annotation (info .field_name , Mutability )
372
372
scim_ctx = info .context .get ("scim" ) if info .context else None
@@ -390,7 +390,9 @@ def scim_request_serializer(self, value: Any, info: FieldSerializationInfo) -> A
390
390
391
391
return value
392
392
393
- def scim_response_serializer (self , value : Any , info : FieldSerializationInfo ) -> Any :
393
+ def _scim_response_serializer (
394
+ self , value : Any , info : FieldSerializationInfo
395
+ ) -> Any :
394
396
"""Serialize the fields according to returnability indications passed in the serialization context."""
395
397
returnability = self .get_field_annotation (info .field_name , Returned )
396
398
attribute_urn = self .get_attribute_urn (info .field_name )
@@ -399,17 +401,17 @@ def scim_response_serializer(self, value: Any, info: FieldSerializationInfo) ->
399
401
info .context .get ("scim_excluded_attributes" , []) if info .context else []
400
402
)
401
403
402
- attribute_urn = normalize_attribute_name (attribute_urn )
403
- included_urns = [normalize_attribute_name (urn ) for urn in included_urns ]
404
- excluded_urns = [normalize_attribute_name (urn ) for urn in excluded_urns ]
404
+ attribute_urn = _normalize_attribute_name (attribute_urn )
405
+ included_urns = [_normalize_attribute_name (urn ) for urn in included_urns ]
406
+ excluded_urns = [_normalize_attribute_name (urn ) for urn in excluded_urns ]
405
407
406
408
if returnability == Returned .never :
407
409
return None
408
410
409
411
if returnability == Returned .default and (
410
412
(
411
413
included_urns
412
- and not contains_attribute_or_subattributes (
414
+ and not _contains_attribute_or_subattributes (
413
415
included_urns , attribute_urn
414
416
)
415
417
)
@@ -427,7 +429,7 @@ def model_serializer_exclude_none(
427
429
self , handler : SerializerFunctionWrapHandler , info : SerializationInfo
428
430
) -> dict [str , Any ]:
429
431
"""Remove `None` values inserted by the :meth:`~scim2_models.base.BaseModel.scim_serializer`."""
430
- self .set_complex_attribute_urns ()
432
+ self ._set_complex_attribute_urns ()
431
433
result = handler (self )
432
434
return {key : value for key , value in result .items () if value is not None }
433
435
0 commit comments