diff options
author | Sebastian Rittau <srittau@rittau.biz> | 2022-04-16 17:37:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-16 17:37:58 (GMT) |
commit | 0ddc63b240340a952692b11dfe0810973393ed11 (patch) | |
tree | ca8613b1571d8ac427cd265b0533072ada253a97 /Lib | |
parent | 1adc837bf1d88a110e1d9e3021abc92b7e7dfa8e (diff) | |
download | cpython-0ddc63b240340a952692b11dfe0810973393ed11.zip cpython-0ddc63b240340a952692b11dfe0810973393ed11.tar.gz cpython-0ddc63b240340a952692b11dfe0810973393ed11.tar.bz2 |
gh-86178: Add wsgiref.types (GH-32335)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/wsgiref/__init__.py | 2 | ||||
-rw-r--r-- | Lib/wsgiref/types.py | 54 |
2 files changed, 56 insertions, 0 deletions
diff --git a/Lib/wsgiref/__init__.py b/Lib/wsgiref/__init__.py index 1efbba0..59ee48f 100644 --- a/Lib/wsgiref/__init__.py +++ b/Lib/wsgiref/__init__.py @@ -13,6 +13,8 @@ Current Contents: * validate -- validation wrapper that sits between an app and a server to detect errors in either +* types -- collection of WSGI-related types for static type checking + To-Do: * cgi_gateway -- Run WSGI apps under CGI (pending a deployment standard) diff --git a/Lib/wsgiref/types.py b/Lib/wsgiref/types.py new file mode 100644 index 0000000..4a519e5 --- /dev/null +++ b/Lib/wsgiref/types.py @@ -0,0 +1,54 @@ +"""WSGI-related types for static type checking""" + +from collections.abc import Callable, Iterable +from types import TracebackType +from typing import Any, Protocol, TypeAlias + +__all__ = [ + "StartResponse", + "WSGIEnvironment", + "WSGIApplication", + "InputStream", + "ErrorStream", + "FileWrapper", +] + +_ExcInfo = tuple[type[BaseException], BaseException, TracebackType] +_OptExcInfo = _ExcInfo | tuple[None, None, None] + +class StartResponse(Protocol): + """start_response() callable as defined in PEP 3333""" + def __call__( + self, + status: str, + headers: list[tuple[str, str]], + exc_info: _OptExcInfo | None = ..., + /, + ) -> Callable[[bytes], object]: ... + +WSGIEnvironment: TypeAlias = dict[str, Any] +WSGIApplication: TypeAlias = Callable[[WSGIEnvironment, StartResponse], + Iterable[bytes]] + +class InputStream(Protocol): + """WSGI input stream as defined in PEP 3333""" + def read(self, size: int = ..., /) -> bytes: ... + def readline(self, size: int = ..., /) -> bytes: ... + def readlines(self, hint: int = ..., /) -> list[bytes]: ... + def __iter__(self) -> Iterable[bytes]: ... + +class ErrorStream(Protocol): + """WSGI error stream as defined in PEP 3333""" + def flush(self) -> object: ... + def write(self, s: str, /) -> object: ... + def writelines(self, seq: list[str], /) -> object: ... + +class _Readable(Protocol): + def read(self, size: int = ..., /) -> bytes: ... + # Optional: def close(self) -> object: ... + +class FileWrapper(Protocol): + """WSGI file wrapper as defined in PEP 3333""" + def __call__( + self, file: _Readable, block_size: int = ..., /, + ) -> Iterable[bytes]: ... |