From a41066b413489b407b9d99174af697563ad680b9 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Mon, 13 Apr 2020 21:30:34 +0200 Subject: add type hints to all of the code In order to use type hint syntax, we need to bump the minimum Python version to 3.7 and some of the features such as Literal and Protocol are opted in when a sufficiently recent Python is available. This does not make all of the code pass type checking with mypy. A number of typing issues remain, but the output of mypy becomes something one can read through. In adding type hints, a lot of epydoc @type annotations are removed as redundant. This update also adopts black-style line breaking. --- wsgitools/internal.py | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'wsgitools/internal.py') diff --git a/wsgitools/internal.py b/wsgitools/internal.py index 9bf7ded..86a9d5a 100644 --- a/wsgitools/internal.py +++ b/wsgitools/internal.py @@ -1,11 +1,42 @@ -def bytes2str(bstr): +import sys +import typing + +def bytes2str(bstr: bytes) -> str: assert isinstance(bstr, bytes) return bstr.decode("iso-8859-1") # always successful -def str2bytes(sstr): +def str2bytes(sstr: str) -> bytes: assert isinstance(sstr, str) return sstr.encode("iso-8859-1") # might fail, but spec says it doesn't -def textopen(filename, mode): +def textopen(filename: str, mode: str) -> typing.TextIO: # We use the same encoding as for all wsgi strings here. return open(filename, mode, encoding="iso-8859-1") + +Environ = typing.Dict[str, typing.Any] + +HeaderList = typing.List[typing.Tuple[str, str]] + +if sys.version_info >= (3, 11): + OptExcInfo = typing.Optional[ + typing.Tuple[type[BaseException], BaseException, typing.Any] + ] +else: + OptExcInfo = typing.Optional[ + typing.Tuple[typing.Any, BaseException, typing.Any] + ] + +WriteCallback = typing.Callable[[bytes], None] + +if sys.version_info >= (3, 11): + class StartResponse(typing.Protocol): + def __call__( + self, status: str, headers: HeaderList, exc_info: OptExcInfo = None + ) -> WriteCallback: + ... +else: + StartResponse = typing.Callable[ + [str, HeaderList, OptExcInfo], WriteCallback + ] + +WsgiApp = typing.Callable[[Environ, StartResponse], typing.Iterable[bytes]] -- cgit v1.2.3