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/scgi/__init__.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'wsgitools/scgi/__init__.py') diff --git a/wsgitools/scgi/__init__.py b/wsgitools/scgi/__init__.py index e2a68c2..677e3b5 100644 --- a/wsgitools/scgi/__init__.py +++ b/wsgitools/scgi/__init__.py @@ -1,5 +1,8 @@ __all__ = [] +import socket +import typing + try: import sendfile except ImportError: @@ -7,6 +10,8 @@ except ImportError: else: have_sendfile = True +from wsgitools.internal import Environ + class FileWrapper: """ @ivar offset: Initially 0. Becomes -1 when reading using next and @@ -14,18 +19,20 @@ class FileWrapper: counts the number of bytes sent. It also ensures that next and transfer are never mixed. """ - def __init__(self, filelike, blksize=8192): + def __init__(self, filelike, blksize: int = 8192): self.filelike = filelike self.blksize = blksize self.offset = 0 if hasattr(filelike, "close"): self.close = filelike.close - def can_transfer(self): + def can_transfer(self) -> bool: return have_sendfile and hasattr(self.filelike, "fileno") and \ self.offset >= 0 - def transfer(self, sock, blksize=None): + def transfer( + self, sock: socket.socket, blksize: typing.Optional[int] = None + ) -> int: assert self.offset >= 0 if blksize is None: blksize = self.blksize @@ -42,10 +49,10 @@ class FileWrapper: self.offset += sent return sent - def __iter__(self): + def __iter__(self) -> typing.Iterator[bytes]: return self - def __next__(self): + def __next__(self) -> bytes: assert self.offset <= 0 self.offset = -1 data = self.filelike.read(self.blksize) @@ -53,8 +60,12 @@ class FileWrapper: return data raise StopIteration -def _convert_environ(environ, multithread=False, multiprocess=False, - run_once=False): +def _convert_environ( + environ: Environ, + multithread: bool = False, + multiprocess: bool = False, + run_once: bool = False, +) -> None: environ.update({ "wsgi.version": (1, 0), "wsgi.url_scheme": "http", -- cgit v1.2.3