77from typing import Any , cast , TypeVar
88import typing
99
10- if sys .version_info >= (3 , 10 ):
11- from typing import TypeGuard
12- else :
13- from typing_extensions import TypeGuard
10+ from typing import TypeGuard
1411
1512from typing import Optional , Union , Annotated , Literal
1613from pydantic import BaseModel , TypeAdapter , Field , RootModel , ConfigDict
2421 from .base import DiscriminatorBase
2522 from ._types import SchemaType , ReferenceType , PrimitiveTypes , DiscriminatorType
2623
27- type_format_to_class : dict [str , dict [Optional [ str ] , type ]] = collections .defaultdict (dict )
24+ type_format_to_class : dict [str , dict [str | None , type ]] = collections .defaultdict (dict )
2825
2926log = logging .getLogger ("aiopenapi3.model" )
3027
@@ -135,7 +132,7 @@ def createFields(self, schema: "SchemaType", overwrite=False):
135132 return
136133
137134 if Model .is_type (schema , "object" ) or Model .is_type_any (schema ):
138- f : Union [ SchemaBase , ReferenceBase ]
135+ f : SchemaBase | ReferenceBase
139136 assert schema .properties is not None
140137 for name , f in schema .properties .items ():
141138 if (
@@ -236,7 +233,7 @@ def _createAnnotations(
236233 raise ValueError ()
237234 return
238235
239- def model (self ) -> Union [ type [BaseModel ], type [None ] ]:
236+ def model (self ) -> type [BaseModel ] | type [None ]:
240237 if self .root :
241238 m = self .root
242239 else :
@@ -253,7 +250,7 @@ def model(self) -> Union[type[BaseModel], type[None]]:
253250
254251 @classmethod
255252 def collapse (cls , type_name , items : list ["_ClassInfo" ]) -> type [BaseModel ]:
256- r : list [Union [ type [BaseModel ], type [None ] ]]
253+ r : list [type [BaseModel ] | type [None ]]
257254
258255 r = [i .model () for i in items ]
259256
@@ -287,9 +284,9 @@ class Model: # (BaseModel):
287284 def from_schema (
288285 cls ,
289286 schema : "SchemaType" ,
290- schemanames : Optional [ list [str ]] = None ,
291- discriminators : Optional [ list ["DiscriminatorType" ]] = None ,
292- extra : Optional [ list ["SchemaType" ]] = None ,
287+ schemanames : list [str ] | None = None ,
288+ discriminators : list ["DiscriminatorType" ] | None = None ,
289+ extra : list ["SchemaType" ] | None = None ,
293290 ) -> type [BaseModel ]:
294291 if schemanames is None :
295292 schemanames = []
@@ -313,7 +310,7 @@ def createClassInfo(
313310 _type : str ,
314311 schemanames : list [str ],
315312 discriminators : list ["DiscriminatorType" ],
316- extra : Optional [ list ["SchemaType" ]] ,
313+ extra : list ["SchemaType" ] | None ,
317314 ) -> _ClassInfo :
318315 from . import v20 , v30 , v31
319316
@@ -504,7 +501,7 @@ def createConfigDict(schema: "SchemaType"):
504501
505502 @staticmethod
506503 def createAnnotation (
507- schema : Optional [ Union ["SchemaType" , "ReferenceType" ]] , _type : Optional [ str ] = None , fwdref : bool = False
504+ schema : Union ["SchemaType" , "ReferenceType" ] | None , _type : str | None = None , fwdref : bool = False
508505 ) -> type :
509506 if schema is None :
510507 return BaseModel
@@ -662,7 +659,7 @@ def is_type(schema: "SchemaType", type_) -> bool:
662659 return isinstance (schema .type , str ) and schema .type == type_ or Model .or_type (schema , type_ , l = None )
663660
664661 @staticmethod
665- def or_type (schema : "SchemaType" , type_ : str , l : Optional [ int ] = 2 ) -> bool :
662+ def or_type (schema : "SchemaType" , type_ : str , l : int | None = 2 ) -> bool :
666663 return isinstance ((t := schema .type ), list ) and (l is None or len (t ) == l ) and type_ in t
667664
668665 @staticmethod
0 commit comments