66from typing import List # noqa : UP005,UP035
77from typing import Literal
88from typing import Optional
9+ from typing import TypeVar
910from typing import Union
1011from typing import get_origin
1112
3132from ..reference import URIReference
3233from ..utils import Base64Bytes
3334from ..utils import normalize_attribute_name
34- from .resource import Extension
3535from .resource import Resource
3636
37+ T = TypeVar ("T" , bound = BaseModel )
38+
3739
3840def 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
4749def 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