Skip to content
Myr

🔥 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

1 file+17 added−77 removed

modified:typer/_typing.py+17 added-77 removed
1 unmodified line
2# https://github.com/pydantic/pydantic/blob/v1.9.2/pydantic/typing.py3# mypy: ignore-errors45import sys6from collections.abc import Callable as Callable7from os import PathLike8from 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 lines
25 _eval_type,26 cast,27 get_type_hints,28)29from typing import Callable as TypingCallable3031from typing_extensions import Annotated, Literal3233AnyCallable = TypingCallable[..., Any]34NoArgAnyCallable = TypingCallable[[], Any]3536try:37 from typing import _TypingBase as typing_base # type: ignore38except ImportError:39 from typing import _Final as typing_base # type: ignore
10 unmodified lines
50 # python < 3.10 does not have UnionType (str | int, byte | bool and so on)51 TypesUnionType = ()525348if sys.version_info < (3, 7):49 if TYPE_CHECKING:54if sys.version_info < (3, 9):5551 class ForwardRef:52 def __init__(self, arg: Any):53 pass5455 def _eval_type(self, globalns: Any, localns: Any) -> Any:56 pass5758 else:59 from typing import _ForwardRef as ForwardRef60else:61 from typing import ForwardRef626364if sys.version_info < (3, 7):6556 def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:67 return type_._eval_type(globalns, localns)6869elif sys.version_info < (3, 9):7071 def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:57 return type_._evaluate(globalns, localns)5859else:60
4 unmodified lines
656667if 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_hints7273else:
1 unmodified line
75 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)777894if sys.version_info < (3, 7):95 from typing import Callable as Callable9697 AnyCallable = Callable[..., Any]98 NoArgAnyCallable = Callable[[], Any]99else:100 from collections.abc import Callable as Callable101 from typing import Callable as TypingCallable102103 AnyCallable = TypingCallable[..., Any]104 NoArgAnyCallable = TypingCallable[[], Any]10510679# Annotated[...] is implemented by returning an instance of one of these classes, depending on80# python/typing_extensions version.81AnnotatedTypeNames = {"AnnotatedMeta", "_AnnotatedAlias"}82
21 unmodified lines
104 return cast(Type[Any], Annotated) # mypy complains about _SpecialForm105 return _typing_get_origin(tp) or getattr(tp, "__origin__", None)106107136if sys.version_info < (3, 7): # noqa: C901 (ignore complexity)137138 def get_args(t: Type[Any]) -> Tuple[Any, ...]:139 """Simplest get_args compatibility layer possible.140141 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).145146 """147 if type(t).__name__ in AnnotatedTypeNames:148 return t.__args__ + t.__metadata__149 return getattr(t, "__args__", ())150151elif sys.version_info < (3, 8): # noqa: C901108if sys.version_info < (3, 8): # noqa: C901109 from typing import _GenericAlias110111 def get_args(t: Type[Any]) -> Tuple[Any, ...]:112 """Compatibility version of get_args for python 3.7.
195 unmodified lines
308NONE_TYPES: Tuple[Any, Any, Any] = (None, NoneType, Literal[None])309310311if 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 lines
387 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_refs
5 unmodified lines
402def is_callable_type(type_: Type[Any]) -> bool:403 return type_ is Callable or get_origin(type_) is Callable404405451if sys.version_info >= (3, 7):406def is_literal_type(type_: Type[Any]) -> bool:407 return Literal is not None and get_origin(type_) is Literal408453 def is_literal_type(type_: Type[Any]) -> bool:454 return Literal is not None and get_origin(type_) is Literal409456 def literal_values(type_: Type[Any]) -> Tuple[Any, ...]:457 return get_args(type_)410def literal_values(type_: Type[Any]) -> Tuple[Any, ...]:411 return get_args(type_)412459else:413461 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 )467468 def literal_values(type_: Type[Any]) -> Tuple[Any, ...]:469 return type_.__values__470471414def 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 lines
463def _check_classvar(v: Optional[Type[Any]]) -> bool:464 if v is None:465 return False466525 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"468469470def is_classvar(ann_type: Type[Any]) -> bool:471 if _check_classvar(ann_type) or _check_classvar(get_origin(ann_type)):
99 unmodified lines