From 496d75876937b4834e73303f44a7f8364abc2336 Mon Sep 17 00:00:00 2001 From: Shantanu Date: Wed, 22 Apr 2020 10:38:47 -0700 Subject: [PATCH] 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 --- stdlib/3/typing.pyi | 33 ++++++++++++------------ tests/stubtest_whitelists/py35.txt | 2 -- tests/stubtest_whitelists/py36.txt | 2 -- tests/stubtest_whitelists/py37.txt | 30 --------------------- tests/stubtest_whitelists/py38.txt | 29 --------------------- tests/stubtest_whitelists/py3_common.txt | 7 ++--- 6 files changed, 19 insertions(+), 84 deletions(-) diff --git a/stdlib/3/typing.pyi b/stdlib/3/typing.pyi index 11e7ed76a..6c4dcc253 100644 --- a/stdlib/3/typing.pyi +++ b/stdlib/3/typing.pyi @@ -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 diff --git a/tests/stubtest_whitelists/py35.txt b/tests/stubtest_whitelists/py35.txt index 77f9404fc..4a5b6e575 100644 --- a/tests/stubtest_whitelists/py35.txt +++ b/tests/stubtest_whitelists/py35.txt @@ -71,10 +71,8 @@ typing.MutableSequence.remove typing.MutableSet.add typing.MutableSet.discard typing.MutableSet.remove -typing.Protocol typing.Sequence.count typing.Sequence.index -typing.Type typing.runtime_checkable unittest.async_case uuid.UUID.int diff --git a/tests/stubtest_whitelists/py36.txt b/tests/stubtest_whitelists/py36.txt index 6cbcabace..a80097857 100644 --- a/tests/stubtest_whitelists/py36.txt +++ b/tests/stubtest_whitelists/py36.txt @@ -71,10 +71,8 @@ typing.MutableSequence.remove typing.MutableSet.add typing.MutableSet.discard typing.MutableSet.remove -typing.Protocol typing.Sequence.count typing.Sequence.index -typing.Type typing.runtime_checkable unittest.async_case urllib.parse.parse_qs diff --git a/tests/stubtest_whitelists/py37.txt b/tests/stubtest_whitelists/py37.txt index 8c7c6aeeb..92e465d75 100644 --- a/tests/stubtest_whitelists/py37.txt +++ b/tests/stubtest_whitelists/py37.txt @@ -66,36 +66,6 @@ tracemalloc.Traceback.format types.ClassMethodDescriptorType.__get__ types.MethodDescriptorType.__get__ types.WrapperDescriptorType.__get__ -typing.AbstractSet -typing.AsyncContextManager -typing.AsyncGenerator -typing.AsyncIterable -typing.AsyncIterator -typing.Awaitable -typing.ByteString -typing.Collection -typing.Container -typing.ContextManager -typing.Coroutine -typing.Generator -typing.GenericMeta -typing.Hashable -typing.ItemsView -typing.Iterable -typing.Iterator -typing.KeysView -typing.Mapping -typing.MappingView -typing.MutableMapping -typing.MutableSequence -typing.MutableSet -typing.Optional -typing.Protocol -typing.Reversible -typing.Sequence -typing.Sized -typing.Union -typing.ValuesView typing.runtime_checkable unittest.async_case urllib.parse.parse_qs diff --git a/tests/stubtest_whitelists/py38.txt b/tests/stubtest_whitelists/py38.txt index 52858b37d..a9e60fabb 100644 --- a/tests/stubtest_whitelists/py38.txt +++ b/tests/stubtest_whitelists/py38.txt @@ -148,35 +148,6 @@ types.ClassMethodDescriptorType.__get__ types.CodeType.replace types.MethodDescriptorType.__get__ types.WrapperDescriptorType.__get__ -typing.AbstractSet -typing.AsyncContextManager -typing.AsyncGenerator -typing.AsyncIterable -typing.AsyncIterator -typing.Awaitable -typing.ByteString -typing.Collection -typing.Container -typing.ContextManager -typing.Coroutine -typing.Generator -typing.GenericMeta -typing.Hashable -typing.ItemsView -typing.Iterable -typing.Iterator -typing.KeysView -typing.Mapping -typing.MappingView -typing.MutableMapping -typing.MutableSequence -typing.MutableSet -typing.Optional -typing.Reversible -typing.Sequence -typing.Sized -typing.Union -typing.ValuesView unittest.TestCase.addCleanup unittest.case.TestCase.addCleanup unittest.doModuleCleanups diff --git a/tests/stubtest_whitelists/py3_common.txt b/tests/stubtest_whitelists/py3_common.txt index 0b367f6d8..8c3511c6e 100644 --- a/tests/stubtest_whitelists/py3_common.txt +++ b/tests/stubtest_whitelists/py3_common.txt @@ -510,17 +510,12 @@ types.coroutine types.new_class types.prepare_class typing.AwaitableGenerator -typing.Generic typing.IO.__iter__ typing.IO.__next__ typing.IO.closed -typing.Match typing.NamedTuple._asdict typing.NamedTuple._make typing.NamedTuple._replace -typing.Pattern -typing.TypeAlias -typing.cast typing.type_check_only unittest.TestCase.assertAlmostEqual unittest.TestCase.assertDictContainsSubset @@ -592,6 +587,8 @@ builtins.quit # Builtins that mypy pretends exist builtins.reveal_locals builtins.reveal_type +# Various classes in typing aren't types at runtime. In addition, mypy thinks some special forms are tautologically defined. +typing.[A-Z]\w+ # We can't distinguish not having a default value from having a default value of inspect.Parameter.empty inspect.Parameter.__init__ inspect.Signature.__init__