From cb932453e761ce18dd573cc9c4e590d06ff213a9 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Thu, 16 Aug 2018 23:48:44 +0200 Subject: [PATCH 1/4] Add ExcInfo and OptExcInfo type aliases --- stdlib/2/sys.pyi | 8 +++++--- stdlib/3/sys.pyi | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/stdlib/2/sys.pyi b/stdlib/2/sys.pyi index 48c3436d6614..debadf40a04c 100644 --- a/stdlib/2/sys.pyi +++ b/stdlib/2/sys.pyi @@ -6,6 +6,10 @@ from typing import ( ) from types import FrameType, ModuleType, TracebackType, ClassType +# The following type alias are stub-only and do not exist during runtime +ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType] +OptExcInfo = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]] + class _flags: bytes_warning = ... # type: int debug = ... # type: int @@ -113,9 +117,7 @@ def __excepthook__(type_: type, value: BaseException, traceback: TracebackType) def exc_clear() -> None: raise DeprecationWarning() # TODO should be a union of tuple, see mypy#1178 -def exc_info() -> Tuple[Optional[Type[BaseException]], - Optional[BaseException], - Optional[TracebackType]]: ... +def exc_info() -> OptExcInfo: ... # sys.exit() accepts an optional argument of anything printable def exit(arg: Any = ...) -> NoReturn: diff --git a/stdlib/3/sys.pyi b/stdlib/3/sys.pyi index b35f6d7a91f2..41dc2021b0d8 100644 --- a/stdlib/3/sys.pyi +++ b/stdlib/3/sys.pyi @@ -14,6 +14,10 @@ from importlib.abc import MetaPathFinder _T = TypeVar('_T') +# The following type alias are stub-only and do not exist during runtime +ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType] +OptExcInfo = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]] + # ----- sys variables ----- abiflags: str argv: List[str] @@ -128,9 +132,7 @@ def _current_frames() -> Dict[int, Any]: ... def displayhook(value: Optional[int]) -> None: ... def excepthook(type_: Type[BaseException], value: BaseException, traceback: TracebackType) -> None: ... -def exc_info() -> Tuple[Optional[Type[BaseException]], - Optional[BaseException], - Optional[TracebackType]]: ... +def exc_info() -> OptExcInfo: ... # sys.exit() accepts an optional argument of anything printable def exit(arg: object = ...) -> NoReturn: raise SystemExit() From 873db9fb3e7858c91c2a5fab8e12ff5b1cafec55 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Thu, 16 Aug 2018 23:48:56 +0200 Subject: [PATCH 2/4] Implement StartResponse using a protocol --- stdlib/2and3/wsgiref/types.pyi | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/stdlib/2and3/wsgiref/types.pyi b/stdlib/2and3/wsgiref/types.pyi index 8f35cce51c7c..81b7edbcc2c7 100644 --- a/stdlib/2and3/wsgiref/types.pyi +++ b/stdlib/2and3/wsgiref/types.pyi @@ -15,10 +15,12 @@ # you need to use 'WSGIApplication' and not simply WSGIApplication when type # hinting your code. Otherwise Python will raise NameErrors. -from typing import Callable, Dict, Iterable, List, Any, Text, Protocol +from sys import OptExcInfo +from typing import Callable, Dict, Iterable, List, Any, Text, Protocol, Tuple, Optional + +class StartResponse(Protocol): + def __call__(self, status: str, headers: List[Tuple[str, str]], exc_info: Optional[OptExcInfo] = ...) -> Callable[[bytes], Any]: ... -# (status: str, headers: List[Tuple[str, str]], exc_info = None) -StartResponse = Callable[..., Callable[[bytes], Any]] WSGIEnvironment = Dict[Text, Any] WSGIApplication = Callable[[WSGIEnvironment, StartResponse], Iterable[bytes]] From d71663461abc6894a4d7dea183592904e121cf65 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Fri, 17 Aug 2018 09:02:23 +0200 Subject: [PATCH 3/4] Mark stub-only types with an underscore --- stdlib/2/sys.pyi | 6 +++--- stdlib/2and3/wsgiref/types.pyi | 4 ++-- stdlib/3/sys.pyi | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/stdlib/2/sys.pyi b/stdlib/2/sys.pyi index debadf40a04c..2ed8fdfa5b2c 100644 --- a/stdlib/2/sys.pyi +++ b/stdlib/2/sys.pyi @@ -7,8 +7,8 @@ from typing import ( from types import FrameType, ModuleType, TracebackType, ClassType # The following type alias are stub-only and do not exist during runtime -ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType] -OptExcInfo = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]] +_ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType] +_OptExcInfo = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]] class _flags: bytes_warning = ... # type: int @@ -117,7 +117,7 @@ def __excepthook__(type_: type, value: BaseException, traceback: TracebackType) def exc_clear() -> None: raise DeprecationWarning() # TODO should be a union of tuple, see mypy#1178 -def exc_info() -> OptExcInfo: ... +def exc_info() -> _OptExcInfo: ... # sys.exit() accepts an optional argument of anything printable def exit(arg: Any = ...) -> NoReturn: diff --git a/stdlib/2and3/wsgiref/types.pyi b/stdlib/2and3/wsgiref/types.pyi index 81b7edbcc2c7..808c68034161 100644 --- a/stdlib/2and3/wsgiref/types.pyi +++ b/stdlib/2and3/wsgiref/types.pyi @@ -15,11 +15,11 @@ # you need to use 'WSGIApplication' and not simply WSGIApplication when type # hinting your code. Otherwise Python will raise NameErrors. -from sys import OptExcInfo +from sys import _OptExcInfo from typing import Callable, Dict, Iterable, List, Any, Text, Protocol, Tuple, Optional class StartResponse(Protocol): - def __call__(self, status: str, headers: List[Tuple[str, str]], exc_info: Optional[OptExcInfo] = ...) -> Callable[[bytes], Any]: ... + def __call__(self, status: str, headers: List[Tuple[str, str]], exc_info: Optional[_OptExcInfo] = ...) -> Callable[[bytes], Any]: ... WSGIEnvironment = Dict[Text, Any] WSGIApplication = Callable[[WSGIEnvironment, StartResponse], Iterable[bytes]] diff --git a/stdlib/3/sys.pyi b/stdlib/3/sys.pyi index 41dc2021b0d8..0e7b4760080a 100644 --- a/stdlib/3/sys.pyi +++ b/stdlib/3/sys.pyi @@ -15,8 +15,8 @@ from importlib.abc import MetaPathFinder _T = TypeVar('_T') # The following type alias are stub-only and do not exist during runtime -ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType] -OptExcInfo = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]] +_ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType] +_OptExcInfo = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]] # ----- sys variables ----- abiflags: str @@ -132,7 +132,7 @@ def _current_frames() -> Dict[int, Any]: ... def displayhook(value: Optional[int]) -> None: ... def excepthook(type_: Type[BaseException], value: BaseException, traceback: TracebackType) -> None: ... -def exc_info() -> OptExcInfo: ... +def exc_info() -> _OptExcInfo: ... # sys.exit() accepts an optional argument of anything printable def exit(arg: object = ...) -> NoReturn: raise SystemExit() From 0c25474ac9fa54787d5e9397dd49646c496439a4 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Fri, 17 Aug 2018 09:06:07 +0200 Subject: [PATCH 4/4] Remove wrong TODO note python/mypy#1178 is about variable-length tuples, while exc_info() always returns a tuple with length 3. Ideally, exc_info() would return Union[Tuple[Type[_E], _E, TracebackType], Tuple[None, None, None]], but that is a different issue. --- stdlib/2/sys.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/2/sys.pyi b/stdlib/2/sys.pyi index 2ed8fdfa5b2c..33fca3bea8f5 100644 --- a/stdlib/2/sys.pyi +++ b/stdlib/2/sys.pyi @@ -116,7 +116,6 @@ def __displayhook__(value: int) -> None: ... def __excepthook__(type_: type, value: BaseException, traceback: TracebackType) -> None: ... def exc_clear() -> None: raise DeprecationWarning() -# TODO should be a union of tuple, see mypy#1178 def exc_info() -> _OptExcInfo: ... # sys.exit() accepts an optional argument of anything printable