Skip to content

Commit

Permalink
Infer root type from Annotated (but toss away the rest of the annotat…
Browse files Browse the repository at this point in the history
…ion)
  • Loading branch information
JacobHayes committed Nov 26, 2020
1 parent 31bc243 commit dc9faa3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pydantic/fields.py
Expand Up @@ -28,6 +28,7 @@
from .errors import NoneIsNotAllowedError
from .types import Json, JsonWrapper
from .typing import (
Annotated,
Callable,
ForwardRef,
NoArgAnyCallable,
Expand Down Expand Up @@ -424,6 +425,10 @@ def _type_analysis(self) -> None: # noqa: C901 (ignore complexity)
if isinstance(self.type_, type) and isinstance(None, self.type_):
self.allow_none = True
return
if origin is Annotated:
self.type_ = get_args(self.type_)[0]
self._type_analysis()
return
if origin is Callable:
return
if origin is Union:
Expand Down
11 changes: 11 additions & 0 deletions pydantic/typing.py
Expand Up @@ -127,6 +127,17 @@ def get_args(tp: Type[Any]) -> Tuple[Any, ...]:
# the fallback is needed for the same reasons as `get_origin` (see above)
return typing_get_args(tp) or getattr(tp, '__args__', ()) or generic_get_args(tp)

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


if TYPE_CHECKING:
from .fields import ModelField
Expand Down

0 comments on commit dc9faa3

Please sign in to comment.