Skip to content

Commit

Permalink
Fix <py3.9 Annotated
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobHayes committed Nov 27, 2020
1 parent 85cd7b7 commit bed794f
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pydantic/typing.py
Expand Up @@ -129,12 +129,25 @@ def get_args(tp: Type[Any]) -> Tuple[Any, ...]:

if sys.version_info < (3, 9):
if TYPE_CHECKING:
from typing_extensions import Annotated
from typing_extensions import Annotated, _AnnotatedAlias
else: # due to different mypy warnings raised during CI for python 3.7 and 3.8
try:
from typing_extensions import Annotated
from typing_extensions import Annotated, _AnnotatedAlias
except ImportError:
Annotated = None
Annotated, _AnnotatedAlias = None, None

from typing_extensions import get_origin as typing_extensions_get_origin

# Our custom get_origin for <3.8 and the builtin 3.8 get_origin don't recognize
# typing_extensions.Annotated, so wrap them to short-circuit with typing_extensions.get_origin.
# We still want to use our wrapped get_origins defined above for non-Annotated data.
_get_origin = get_origin

def get_origin(tp: Type[Any]) -> Type[Any]:
if _AnnotatedAlias is not None and isinstance(tp, _AnnotatedAlias):
return typing_extensions_get_origin(tp)
return _get_origin(tp)

else:
from typing import Annotated

Expand All @@ -157,6 +170,7 @@ def get_args(tp: Type[Any]) -> Tuple[Any, ...]:
__all__ = (
'ForwardRef',
'Callable',
'Annotated',
'AnyCallable',
'NoArgAnyCallable',
'NoneType',
Expand Down

0 comments on commit bed794f

Please sign in to comment.