@@ -55,7 +55,7 @@ def validate_model_attribute(model: type["BaseModel"], attribute_base: str) -> N
55
55
if sub_attribute_base :
56
56
attribute_type = model .get_field_root_type (attribute_name )
57
57
58
- if not issubclass (attribute_type , BaseModel ):
58
+ if not attribute_type or not issubclass (attribute_type , BaseModel ):
59
59
raise ValueError (
60
60
f"Attribute '{ attribute_name } ' is not a complex attribute, and cannot have a '{ sub_attribute_base } ' sub-attribute"
61
61
)
@@ -429,7 +429,7 @@ def annotation_type_filter(item):
429
429
return field_annotation
430
430
431
431
@classmethod
432
- def get_field_root_type (cls , attribute_name : str ) -> type :
432
+ def get_field_root_type (cls , attribute_name : str ) -> type | None :
433
433
"""Extract the root type from a model field.
434
434
435
435
For example, return 'GroupMember' for
@@ -442,9 +442,8 @@ def get_field_root_type(cls, attribute_name: str) -> type:
442
442
attribute_type = get_args (attribute_type )[0 ]
443
443
444
444
# extract 'x' from 'List[x]'
445
- if isclass (get_origin (attribute_type )) and issubclass (
446
- get_origin (attribute_type ), list
447
- ):
445
+ origin = get_origin (attribute_type )
446
+ if origin and isclass (origin ) and issubclass (origin , list ):
448
447
attribute_type = get_args (attribute_type )[0 ]
449
448
450
449
return attribute_type
@@ -637,28 +636,29 @@ def scim_serializer(
637
636
) -> Any :
638
637
"""Serialize the fields according to mutability indications passed in the serialization context."""
639
638
value = handler (value )
639
+ scim_ctx = info .context .get ("scim" ) if info .context else None
640
640
641
- if info . context . get ( "scim" ) and Context .is_request (info . context [ "scim" ] ):
641
+ if scim_ctx and Context .is_request (scim_ctx ):
642
642
value = self .scim_request_serializer (value , info )
643
643
644
- if info . context . get ( "scim" ) and Context .is_response (info . context [ "scim" ] ):
644
+ if scim_ctx and Context .is_response (scim_ctx ):
645
645
value = self .scim_response_serializer (value , info )
646
646
647
647
return value
648
648
649
649
def scim_request_serializer (self , value : Any , info : SerializationInfo ) -> Any :
650
650
"""Serialize the fields according to mutability indications passed in the serialization context."""
651
651
mutability = self .get_field_annotation (info .field_name , Mutability )
652
- context = info .context .get ("scim" )
652
+ scim_ctx = info .context .get ("scim" ) if info . context else None
653
653
654
654
if (
655
- context == Context .RESOURCE_CREATION_REQUEST
655
+ scim_ctx == Context .RESOURCE_CREATION_REQUEST
656
656
and mutability == Mutability .read_only
657
657
):
658
658
return None
659
659
660
660
if (
661
- context
661
+ scim_ctx
662
662
in (
663
663
Context .RESOURCE_QUERY_REQUEST ,
664
664
Context .SEARCH_REQUEST ,
@@ -667,7 +667,7 @@ def scim_request_serializer(self, value: Any, info: SerializationInfo) -> Any:
667
667
):
668
668
return None
669
669
670
- if context == Context .RESOURCE_REPLACEMENT_REQUEST and mutability in (
670
+ if scim_ctx == Context .RESOURCE_REPLACEMENT_REQUEST and mutability in (
671
671
Mutability .immutable ,
672
672
Mutability .read_only ,
673
673
):
@@ -679,8 +679,10 @@ def scim_response_serializer(self, value: Any, info: SerializationInfo) -> Any:
679
679
"""Serialize the fields according to returnability indications passed in the serialization context."""
680
680
returnability = self .get_field_annotation (info .field_name , Returned )
681
681
attribute_urn = self .get_attribute_urn (info .field_name )
682
- included_urns = info .context .get ("scim_attributes" , [])
683
- excluded_urns = info .context .get ("scim_excluded_attributes" , [])
682
+ included_urns = info .context .get ("scim_attributes" , []) if info .context else []
683
+ excluded_urns = (
684
+ info .context .get ("scim_excluded_attributes" , []) if info .context else []
685
+ )
684
686
685
687
attribute_urn = normalize_attribute_name (attribute_urn )
686
688
included_urns = [normalize_attribute_name (urn ) for urn in included_urns ]
0 commit comments