From 8c7f489d1b6da15da320e1446b51ba06d88c6c92 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Tue, 5 May 2020 13:55:31 +0100 Subject: [PATCH] Organize special forms in typing (#3966) This is an accompanying PR for https://github.com/python/mypy/pull/8779, see https://github.com/python/mypy/pull/8779#issuecomment-624001349 I also noticed that Python 2 and Python 3 versions are a bit out of sync, so I also put them back in sync. --- stdlib/2/typing.pyi | 21 ++++++++++----------- stdlib/3/typing.pyi | 6 +++--- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/stdlib/2/typing.pyi b/stdlib/2/typing.pyi index bea5ba985..e43495d7e 100644 --- a/stdlib/2/typing.pyi +++ b/stdlib/2/typing.pyi @@ -15,6 +15,8 @@ _promote = object() class _SpecialForm(object): def __getitem__(self, typeargs: Any) -> object: ... +Union: _SpecialForm = ... +Optional: _SpecialForm = ... Tuple: _SpecialForm = ... Generic: _SpecialForm = ... Protocol: _SpecialForm = ... @@ -53,20 +55,17 @@ 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) +List = _Alias() +Dict = _Alias() +DefaultDict = _Alias() +Set = _Alias() +FrozenSet = _Alias() +Counter = _Alias() +Deque = _Alias() # Predefined type variables. AnyStr = TypeVar('AnyStr', str, unicode) diff --git a/stdlib/3/typing.pyi b/stdlib/3/typing.pyi index 6c4dcc253..046d0cf42 100644 --- a/stdlib/3/typing.pyi +++ b/stdlib/3/typing.pyi @@ -14,8 +14,10 @@ TypeVar = object() _promote = object() class _SpecialForm: - def __getitem__(self, typeargs: Any) -> Any: ... + def __getitem__(self, typeargs: Any) -> object: ... +Union: _SpecialForm = ... +Optional: _SpecialForm = ... Tuple: _SpecialForm = ... Generic: _SpecialForm = ... # Protocol is only present in 3.8 and later, but mypy needs it unconditionally @@ -61,8 +63,6 @@ class _Alias: # Class for defining generic aliases for library types. def __getitem__(self, typeargs: Any) -> Any: ... -Union = _Alias() -Optional = _Alias() List = _Alias() Dict = _Alias() DefaultDict = _Alias()