summaryrefslogtreecommitdiff
path: root/wsgitools/internal.py
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2020-04-13 21:30:34 +0200
committerHelmut Grohne <helmut@subdivi.de>2023-06-18 23:16:57 +0200
commita41066b413489b407b9d99174af697563ad680b9 (patch)
tree2f08f9e886e13a7500d1eb527e30737d961deab6 /wsgitools/internal.py
parent4d52eaa4801df3f3169df8e58758bcccf22dc4de (diff)
downloadwsgitools-a41066b413489b407b9d99174af697563ad680b9.tar.gz
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.
Diffstat (limited to 'wsgitools/internal.py')
-rw-r--r--wsgitools/internal.py37
1 files changed, 34 insertions, 3 deletions
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]]