Skip to content

Commit 25ac4d6

Browse files
srittauJelleZijlstra
authored andcommitted
Implement StartResponse using a protocol (#2392)
* Add ExcInfo and OptExcInfo type aliases * Implement StartResponse using a protocol * Mark stub-only types with an underscore * 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.
1 parent a2676ec commit 25ac4d6

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

stdlib/2/sys.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ from typing import (
66
)
77
from types import FrameType, ModuleType, TracebackType, ClassType
88

9+
# The following type alias are stub-only and do not exist during runtime
10+
_ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType]
11+
_OptExcInfo = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]]
12+
913
class _flags:
1014
bytes_warning = ... # type: int
1115
debug = ... # type: int
@@ -112,10 +116,7 @@ def __displayhook__(value: int) -> None: ...
112116
def __excepthook__(type_: type, value: BaseException, traceback: TracebackType) -> None: ...
113117
def exc_clear() -> None:
114118
raise DeprecationWarning()
115-
# TODO should be a union of tuple, see mypy#1178
116-
def exc_info() -> Tuple[Optional[Type[BaseException]],
117-
Optional[BaseException],
118-
Optional[TracebackType]]: ...
119+
def exc_info() -> _OptExcInfo: ...
119120

120121
# sys.exit() accepts an optional argument of anything printable
121122
def exit(arg: Any = ...) -> NoReturn:

stdlib/2and3/wsgiref/types.pyi

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
# you need to use 'WSGIApplication' and not simply WSGIApplication when type
1616
# hinting your code. Otherwise Python will raise NameErrors.
1717

18-
from typing import Callable, Dict, Iterable, List, Any, Text, Protocol
18+
from sys import _OptExcInfo
19+
from typing import Callable, Dict, Iterable, List, Any, Text, Protocol, Tuple, Optional
20+
21+
class StartResponse(Protocol):
22+
def __call__(self, status: str, headers: List[Tuple[str, str]], exc_info: Optional[_OptExcInfo] = ...) -> Callable[[bytes], Any]: ...
1923

20-
# (status: str, headers: List[Tuple[str, str]], exc_info = None)
21-
StartResponse = Callable[..., Callable[[bytes], Any]]
2224
WSGIEnvironment = Dict[Text, Any]
2325
WSGIApplication = Callable[[WSGIEnvironment, StartResponse], Iterable[bytes]]
2426

stdlib/3/sys.pyi

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ from importlib.abc import MetaPathFinder
1414

1515
_T = TypeVar('_T')
1616

17+
# The following type alias are stub-only and do not exist during runtime
18+
_ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType]
19+
_OptExcInfo = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]]
20+
1721
# ----- sys variables -----
1822
abiflags: str
1923
argv: List[str]
@@ -128,9 +132,7 @@ def _current_frames() -> Dict[int, Any]: ...
128132
def displayhook(value: Optional[int]) -> None: ...
129133
def excepthook(type_: Type[BaseException], value: BaseException,
130134
traceback: TracebackType) -> None: ...
131-
def exc_info() -> Tuple[Optional[Type[BaseException]],
132-
Optional[BaseException],
133-
Optional[TracebackType]]: ...
135+
def exc_info() -> _OptExcInfo: ...
134136
# sys.exit() accepts an optional argument of anything printable
135137
def exit(arg: object = ...) -> NoReturn:
136138
raise SystemExit()

0 commit comments

Comments
 (0)