Skip to content

Commit 2a9d7a7

Browse files
committed
refactor: use isinstance instead of hasattr
1 parent 3d78052 commit 2a9d7a7

4 files changed

Lines changed: 34 additions & 23 deletions

File tree

scim2_models/base.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,21 +368,21 @@ def set_complex_attribute_urns(self) -> None:
368368
369369
'attribute_urn' will later be used by 'get_attribute_urn'.
370370
"""
371-
from scim2_models.rfc7643.resource import Resource
372-
371+
from .attributes import ComplexAttribute
373372
from .attributes import is_complex_attribute
374373

375374
for field_name in self.__class__.model_fields:
376375
attr_type = self.get_field_root_type(field_name)
377376
if not attr_type or not is_complex_attribute(attr_type):
378377
continue
379378

380-
main_schema = (
381-
getattr(self, "attribute_urn", None)
382-
or self.__class__.model_fields["schemas"].default[0]
383-
)
379+
if isinstance(self, ComplexAttribute):
380+
main_schema = self.attribute_urn
381+
separator = "."
382+
else:
383+
main_schema = self.__class__.model_fields["schemas"].default[0]
384+
separator = ":"
384385

385-
separator = ":" if isinstance(self, Resource) else "."
386386
schema = f"{main_schema}{separator}{field_name}"
387387

388388
if attr_value := getattr(self, field_name):

scim2_models/rfc7643/resource.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
from typing import get_args
1010
from typing import get_origin
1111

12-
if TYPE_CHECKING:
13-
from scim2_models.rfc7643.schema import Attribute
14-
from scim2_models.rfc7643.schema import Schema
15-
1612
from pydantic import Field
1713
from pydantic import WrapSerializer
1814
from pydantic import field_serializer
@@ -30,9 +26,15 @@
3026
from ..base import BaseModel
3127
from ..base import BaseModelType
3228
from ..reference import Reference
29+
from ..scim_object import SchemaObject
30+
from ..scim_object import ScimObject
3331
from ..utils import UNION_TYPES
3432
from ..utils import normalize_attribute_name
3533

34+
if TYPE_CHECKING:
35+
from .schema import Attribute
36+
from .schema import Schema
37+
3638

3739
class Meta(ComplexAttribute):
3840
"""All "meta" sub-attributes are assigned by the service provider (have a "mutability" of "readOnly"), and all of these sub-attributes have a "returned" characteristic of "default".
@@ -85,7 +87,7 @@ class Meta(ComplexAttribute):
8587
"""
8688

8789

88-
class Extension(BaseModel):
90+
class Extension(SchemaObject):
8991
@classmethod
9092
def to_schema(cls) -> "Schema":
9193
"""Build a :class:`~scim2_models.Schema` from the current extension class."""
@@ -145,13 +147,7 @@ def __new__(cls, name: str, bases: tuple, attrs: dict, **kwargs: Any) -> type:
145147
return klass
146148

147149

148-
class Resource(BaseModel, Generic[AnyExtension], metaclass=ResourceMetaclass):
149-
schemas: Annotated[list[str], Required.true]
150-
"""The "schemas" attribute is a REQUIRED attribute and is an array of
151-
Strings containing URIs that are used to indicate the namespaces of the
152-
SCIM schemas that define the attributes present in the current JSON
153-
structure."""
154-
150+
class Resource(ScimObject, Generic[AnyExtension], metaclass=ResourceMetaclass):
155151
# Common attributes as defined by
156152
# https://www.rfc-editor.org/rfc/rfc7643#section-3.1
157153

scim2_models/rfc7644/message.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111

1212
from ..base import BaseModel
1313
from ..base import BaseModelType
14-
from ..base import Required
14+
from ..scim_object import ScimObject
1515
from ..utils import UNION_TYPES
1616

1717

18-
class Message(BaseModel):
18+
class Message(ScimObject):
1919
"""SCIM protocol messages as defined by :rfc:`RFC7644 §3.1 <7644#section-3.1>`."""
2020

21-
schemas: Annotated[list[str], Required.true]
22-
2321

2422
def create_schema_discriminator(
2523
resource_types_schemas: list[str],

scim2_models/scim_object.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Base SCIM object classes with schema identification."""
2+
3+
from typing import Annotated
4+
5+
from .annotations import Required
6+
from .base import BaseModel
7+
8+
9+
class SchemaObject(BaseModel):
10+
schemas: Annotated[list[str], Required.true]
11+
"""The "schemas" attribute is a REQUIRED attribute and is an array of
12+
Strings containing URIs that are used to indicate the namespaces of the
13+
SCIM schemas that define the attributes present in the current JSON
14+
structure."""
15+
16+
17+
class ScimObject(SchemaObject): ...

0 commit comments

Comments
 (0)