🔥 Remove Python 3.6 specific code paths (#850)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sofie Van Landeghem · committed by GitHub · parent c86da2a · Browse files · 1 file·+17 added−77 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+17 added−77 removed
- other:+17 added−77 removed
Provenance
- Commit
a79fbaf- Tree
b538ed1- Author
Sofie Van Landeghem <svlandeg@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
c86da2a
1 file changed+17 added−77 removed
modified:typer/_typing.py+17 added-77 removed
1 unmodified line2 # https://github.com/pydantic/pydantic/blob/v1.9.2/pydantic/typing.py3 # mypy: ignore-errors4 5 import sys6+from collections.abc import Callable as Callable7 from os import PathLike8 from typing import (9 TYPE_CHECKING,10 AbstractSet,11 Any,12 ClassVar,13 Dict,14+ ForwardRef,15 Generator,16 List,17 Mapping,18 NewType,6 unmodified lines25 _eval_type,26 cast,27 get_type_hints,28 )29+from typing import Callable as TypingCallable30 31 from typing_extensions import Annotated, Literal32 33+AnyCallable = TypingCallable[..., Any]34+NoArgAnyCallable = TypingCallable[[], Any]35+36 try:37 from typing import _TypingBase as typing_base # type: ignore38 except ImportError:39 from typing import _Final as typing_base # type: ignore10 unmodified lines50 # python < 3.10 does not have UnionType (str | int, byte | bool and so on)51 TypesUnionType = ()52 53 48-if sys.version_info < (3, 7):49- if TYPE_CHECKING:54+if sys.version_info < (3, 9):55 51- class ForwardRef:52- def __init__(self, arg: Any):53- pass54-55- def _eval_type(self, globalns: Any, localns: Any) -> Any:56- pass57-58- else:59- from typing import _ForwardRef as ForwardRef60-else:61- from typing import ForwardRef62-63-64-if sys.version_info < (3, 7):65-56 def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:67- return type_._eval_type(globalns, localns)68-69-elif sys.version_info < (3, 9):70-71- def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:57 return type_._evaluate(globalns, localns)58 59 else:60 4 unmodified lines65 66 67 if sys.version_info < (3, 9):68 # Ensure we always get all the whole `Annotated` hint, not just the annotated type.84- # For 3.6 to 3.8, `get_type_hints` doesn't recognize `typing_extensions.Annotated`,69+ # For 3.7 to 3.8, `get_type_hints` doesn't recognize `typing_extensions.Annotated`,70 # so it already returns the full annotation71 get_all_type_hints = get_type_hints72 73 else:1 unmodified line75 def get_all_type_hints(obj: Any, globalns: Any = None, localns: Any = None) -> Any:76 return get_type_hints(obj, globalns, localns, include_extras=True)77 78 94-if sys.version_info < (3, 7):95- from typing import Callable as Callable96-97- AnyCallable = Callable[..., Any]98- NoArgAnyCallable = Callable[[], Any]99-else:100- from collections.abc import Callable as Callable101- from typing import Callable as TypingCallable102-103- AnyCallable = TypingCallable[..., Any]104- NoArgAnyCallable = TypingCallable[[], Any]105-106-79 # Annotated[...] is implemented by returning an instance of one of these classes, depending on80 # python/typing_extensions version.81 AnnotatedTypeNames = {"AnnotatedMeta", "_AnnotatedAlias"}82 21 unmodified lines104 return cast(Type[Any], Annotated) # mypy complains about _SpecialForm105 return _typing_get_origin(tp) or getattr(tp, "__origin__", None)106 107 136-if sys.version_info < (3, 7): # noqa: C901 (ignore complexity)137-138- def get_args(t: Type[Any]) -> Tuple[Any, ...]:139- """Simplest get_args compatibility layer possible.140-141- The Python 3.6 typing module does not have `_GenericAlias` so142- this won't work for everything. In particular this will not143- support the `generics` module (we don't support generic models in144- python 3.6).145-146- """147- if type(t).__name__ in AnnotatedTypeNames:148- return t.__args__ + t.__metadata__149- return getattr(t, "__args__", ())150-151-elif sys.version_info < (3, 8): # noqa: C901108+if sys.version_info < (3, 8): # noqa: C901109 from typing import _GenericAlias110 111 def get_args(t: Type[Any]) -> Tuple[Any, ...]:112 """Compatibility version of get_args for python 3.7.195 unmodified lines308 NONE_TYPES: Tuple[Any, Any, Any] = (None, NoneType, Literal[None])309 310 311 if sys.version_info < (3, 8):355- # Even though this implementation is slower, we need it for python 3.6/3.7:356- # In python 3.6/3.7 "Literal" is not a builtin type and uses a different312+ # Even though this implementation is slower, we need it for python 3.7:313+ # In python 3.7 "Literal" is not a builtin type and uses a different314 # mechanism.315 # for this reason `Literal[None] is Literal[None]` evaluates to `False`,316 # breaking the faster implementation used for the other python versions.317 69 unmodified lines387 10,388 1,389 ):390 value = ForwardRef(value, is_argument=False, is_class=True)434- elif sys.version_info >= (3, 7):435- value = ForwardRef(value, is_argument=False)391 else:437- value = ForwardRef(value)392+ value = ForwardRef(value, is_argument=False)393 try:394 value = _eval_type(value, base_globals, None)395 except NameError:396 # this is ok, it can be fixed with update_forward_refs5 unmodified lines402 def is_callable_type(type_: Type[Any]) -> bool:403 return type_ is Callable or get_origin(type_) is Callable404 405 451-if sys.version_info >= (3, 7):406+def is_literal_type(type_: Type[Any]) -> bool:407+ return Literal is not None and get_origin(type_) is Literal408 453- def is_literal_type(type_: Type[Any]) -> bool:454- return Literal is not None and get_origin(type_) is Literal409 456- def literal_values(type_: Type[Any]) -> Tuple[Any, ...]:457- return get_args(type_)410+def literal_values(type_: Type[Any]) -> Tuple[Any, ...]:411+ return get_args(type_)412 459-else:413 461- def is_literal_type(type_: Type[Any]) -> bool:462- return (463- Literal is not None464- and hasattr(type_, "__values__")465- and type_ == Literal[type_.__values__]466- )467-468- def literal_values(type_: Type[Any]) -> Tuple[Any, ...]:469- return type_.__values__470-471-414 def all_literal_values(type_: Type[Any]) -> Tuple[Any, ...]:415 """416 This method is used to retrieve all Literal values as417 Literal can be used recursively (see https://www.python.org/dev/peps/pep-0586)45 unmodified lines463 def _check_classvar(v: Optional[Type[Any]]) -> bool:464 if v is None:465 return False466 525- return v.__class__ == ClassVar.__class__ and (526- sys.version_info < (3, 7) or getattr(v, "_name", None) == "ClassVar"527- )467+ return v.__class__ == ClassVar.__class__ and getattr(v, "_name", None) == "ClassVar"468 469 470 def is_classvar(ann_type: Type[Any]) -> bool:471 if _check_classvar(ann_type) or _check_classvar(get_origin(ann_type)):99 unmodified lines