Skip to content

Commit

Permalink
Implement StartResponse using a protocol (#2392)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
srittau authored and JelleZijlstra committed Aug 17, 2018
1 parent a2676ec commit 25ac4d6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
9 changes: 5 additions & 4 deletions stdlib/2/sys.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -112,10 +116,7 @@ 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() -> 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:
Expand Down
8 changes: 5 additions & 3 deletions stdlib/2and3/wsgiref/types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]]

Expand Down
8 changes: 5 additions & 3 deletions stdlib/3/sys.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 25ac4d6

Please sign in to comment.