Skip to content

Commit 3d78052

Browse files
committed
fix: make_python_model typing
1 parent 37336ad commit 3d78052

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

scim2_models/rfc7643/resource.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def to_schema(cls) -> "Schema":
9292
return model_to_schema(cls)
9393

9494
@classmethod
95-
def from_schema(cls, schema: "Schema") -> "Extension":
95+
def from_schema(cls, schema: "Schema") -> type["Extension"]:
9696
"""Build a :class:`~scim2_models.Extension` subclass from the schema definition."""
9797
from .schema import make_python_model
9898

@@ -259,7 +259,7 @@ def to_schema(cls) -> "Schema":
259259
return model_to_schema(cls)
260260

261261
@classmethod
262-
def from_schema(cls, schema: "Schema") -> "Resource":
262+
def from_schema(cls, schema: "Schema") -> type["Resource"]:
263263
"""Build a :class:`scim2_models.Resource` subclass from the schema definition."""
264264
from .schema import make_python_model
265265

scim2_models/rfc7643/schema.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import List # noqa : UP005,UP035
77
from typing import Literal
88
from typing import Optional
9+
from typing import TypeVar
910
from typing import Union
1011
from typing import get_origin
1112

@@ -31,9 +32,10 @@
3132
from ..reference import URIReference
3233
from ..utils import Base64Bytes
3334
from ..utils import normalize_attribute_name
34-
from .resource import Extension
3535
from .resource import Resource
3636

37+
T = TypeVar("T", bound=BaseModel)
38+
3739

3840
def make_python_identifier(identifier: str) -> str:
3941
"""Sanitize string to be a suitable Python/Pydantic class attribute name."""
@@ -46,17 +48,16 @@ def make_python_identifier(identifier: str) -> str:
4648

4749
def make_python_model(
4850
obj: Union["Schema", "Attribute"],
49-
base: Optional[type[BaseModel]] = None,
51+
base: type[T],
5052
multiple: bool = False,
51-
) -> Union[Resource, Extension]:
53+
) -> type[T]:
5254
"""Build a Python model from a Schema or an Attribute object."""
5355
if isinstance(obj, Attribute):
5456
pydantic_attributes = {
5557
to_snake(make_python_identifier(attr.name)): attr.to_python()
5658
for attr in (obj.sub_attributes or [])
5759
if attr.name
5860
}
59-
base = MultiValuedComplexAttribute if multiple else ComplexAttribute
6061

6162
else:
6263
pydantic_attributes = {
@@ -214,7 +215,9 @@ def to_python(self) -> Optional[tuple[Any, Field]]:
214215
attr_type = self.type.to_python(bool(self.multi_valued), self.reference_types)
215216

216217
if attr_type in (ComplexAttribute, MultiValuedComplexAttribute):
217-
attr_type = make_python_model(obj=self, multiple=bool(self.multi_valued))
218+
attr_type = make_python_model(
219+
obj=self, base=attr_type, multiple=bool(self.multi_valued)
220+
)
218221

219222
if self.multi_valued:
220223
attr_type = list[attr_type] # type: ignore

0 commit comments

Comments
 (0)