typing: minor changes (#3933)

* typing: fix argument names of cast
* typing: use private _Alias class

For py37 and above, this is _GenericAlias, for py36 and below it's
_TypeAlias. I don't think we need to make typing.pyi definitions
correspond more precisely, but we should avoid leaking a
typing.TypeAlias class
This commit is contained in:
Shantanu
2020-04-22 10:38:47 -07:00
committed by GitHub
parent 0532e72e7f
commit 496d758769
6 changed files with 19 additions and 84 deletions

View File

@@ -18,6 +18,7 @@ class _SpecialForm:
Tuple: _SpecialForm = ...
Generic: _SpecialForm = ...
# Protocol is only present in 3.8 and later, but mypy needs it unconditionally
Protocol: _SpecialForm = ...
Callable: _SpecialForm = ...
Type: _SpecialForm = ...
@@ -30,7 +31,8 @@ if sys.version_info >= (3, 8):
# TypedDict is a (non-subscriptable) special form.
TypedDict: object
class GenericMeta(type): ...
if sys.version_info < (3, 7):
class GenericMeta(type): ...
# Return type that indicates a function does not return.
# This type is equivalent to the None type, but the no-op Union is necessary to
@@ -55,24 +57,23 @@ def no_type_check_decorator(decorator: _C) -> _C: ...
# Type aliases and type constructors
class TypeAlias:
class _Alias:
# Class for defining generic aliases for library types.
def __init__(self, target_type: type) -> None: ...
def __getitem__(self, typeargs: Any) -> Any: ...
Union = TypeAlias(object)
Optional = TypeAlias(object)
List = TypeAlias(object)
Dict = TypeAlias(object)
DefaultDict = TypeAlias(object)
Set = TypeAlias(object)
FrozenSet = TypeAlias(object)
Counter = TypeAlias(object)
Deque = TypeAlias(object)
ChainMap = TypeAlias(object)
Union = _Alias()
Optional = _Alias()
List = _Alias()
Dict = _Alias()
DefaultDict = _Alias()
Set = _Alias()
FrozenSet = _Alias()
Counter = _Alias()
Deque = _Alias()
ChainMap = _Alias()
if sys.version_info >= (3, 7):
OrderedDict = TypeAlias(object)
OrderedDict = _Alias()
if sys.version_info >= (3, 9):
Annotated: _SpecialForm = ...
@@ -616,9 +617,9 @@ if sys.version_info >= (3, 8):
def get_args(tp: Any) -> Tuple[Any, ...]: ...
@overload
def cast(tp: Type[_T], obj: Any) -> _T: ...
def cast(typ: Type[_T], val: Any) -> _T: ...
@overload
def cast(tp: str, obj: Any) -> Any: ...
def cast(typ: str, val: Any) -> Any: ...
# Type constructors