66import logging
77import pathlib
88import platform
9- import typing
109from types import GenericAlias , MappingProxyType
11- from typing import Any , Callable , ClassVar , Dict , Generic , List , Optional , Tuple , Type , TypeVar , get_args , get_origin
10+ from typing import Any , Callable , ClassVar , Dict , Generic , List , Optional , Tuple , Type , TypeVar , Union , get_args , get_origin
1211
13- import typing_extensions
1412from omegaconf import DictConfig
1513from packaging import version
1614from pydantic import (
@@ -100,35 +98,18 @@ def get_registry_dependencies(self, types: Optional[Tuple["ModelType"]] = None)
10098# NOTE: For this logic to be removed, require https://github.com/pydantic/pydantic-core/pull/1478
10199from pydantic ._internal ._model_construction import ModelMetaclass # noqa: E402
102100
103- # Required for py38 compatibility
104- # In python 3.8, get_origin(List[float]) returns list, but you can't call list[float] to retrieve the annotation
105- # Furthermore, Annotated is part of typing_Extensions and get_origin(Annotated[str, ...]) returns str rather than Annotated
106- _IS_PY38 = version .parse (platform .python_version ()) < version .parse ("3.9" )
107- # For a more complete list, see https://github.com/alexmojaki/eval_type_backport/blob/main/eval_type_backport/eval_type_backport.py
108- _PY38_ORIGIN_MAP = {
109- tuple : typing .Tuple ,
110- list : typing .List ,
111- dict : typing .Dict ,
112- set : typing .Set ,
113- frozenset : typing .FrozenSet ,
114- collections .abc .Callable : typing .Callable ,
115- collections .abc .Iterable : typing .Iterable ,
116- collections .abc .Mapping : typing .Mapping ,
117- collections .abc .MutableMapping : typing .MutableMapping ,
118- collections .abc .Sequence : typing .Sequence ,
119- }
101+ _IS_PY39 = version .parse (platform .python_version ()) < version .parse ("3.10" )
120102
121103
122104def _adjust_annotations (annotation ):
123105 origin = get_origin (annotation )
124- if _IS_PY38 :
125- origin = _PY38_ORIGIN_MAP .get (origin , origin )
126- if isinstance (annotation , typing_extensions ._AnnotatedAlias ):
127- args = annotation .__metadata__
128- else :
129- args = get_args (annotation )
130- else :
131- args = get_args (annotation )
106+ args = get_args (annotation )
107+ if not _IS_PY39 :
108+ from types import UnionType
109+
110+ if origin is UnionType :
111+ origin = Union
112+
132113 if isinstance (annotation , GenericAlias ) or (inspect .isclass (annotation ) and issubclass (annotation , PydanticBaseModel )):
133114 return SerializeAsAny [annotation ]
134115 elif origin and args :
@@ -139,10 +120,6 @@ def _adjust_annotations(annotation):
139120 return ClassVar [_adjust_annotations (args [0 ])]
140121 else :
141122 try :
142- if _IS_PY38 and isinstance (annotation , typing_extensions ._AnnotatedAlias ):
143- if origin != annotation :
144- origin = _adjust_annotations (origin )
145- return typing_extensions .Annotated [(origin ,) + tuple (_adjust_annotations (arg ) for arg in args )]
146123 return origin [tuple (_adjust_annotations (arg ) for arg in args )]
147124 except TypeError :
148125 raise TypeError (f"Could not adjust annotations for { origin } " )
0 commit comments