Skip to content

Commit

Permalink
more types
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz committed Aug 29, 2022
1 parent 54b0bbc commit 9f687c5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/whitenoise/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
import warnings
from posixpath import normpath
from typing import Callable, Iterator
from typing import Callable, Generator
from wsgiref.headers import Headers
from wsgiref.util import FileWrapper

Expand Down Expand Up @@ -136,7 +136,7 @@ def add_file_to_dictionary(
static_file = self.get_static_file(path, url, stat_cache=stat_cache)
self.files[url] = static_file

def find_file(self, url: str): # -> | None
def find_file(self, url: str) -> Redirect | StaticFile | None:
# Optimization: bail early if the URL can never match a file
if not self.index_file and url.endswith("/"):
return
Expand All @@ -148,22 +148,24 @@ def find_file(self, url: str): # -> | None
except MissingFileError:
pass

def candidate_paths_for_url(self, url):
def candidate_paths_for_url(self, url: str) -> Generator[str, None, None]:
for root, prefix in self.directories:
if url.startswith(prefix):
path = os.path.join(root, url[len(prefix) :])
if os.path.commonprefix((root, path)) == root:
yield path

def find_file_at_path(self, path, url):
def find_file_at_path(self, path: str, url: str) -> Redirect | StaticFile:
if self.is_compressed_variant(path):
raise MissingFileError(path)
if self.index_file:
return self.find_file_at_path_with_indexes(path, url)
else:
return self.get_static_file(path, url)

def find_file_at_path_with_indexes(self, path, url):
def find_file_at_path_with_indexes(
self, path: str, url: str
) -> Redirect | StaticFile:
if url.endswith("/"):
path = os.path.join(path, self.index_file)
return self.get_static_file(path, url)
Expand Down Expand Up @@ -269,7 +271,7 @@ def redirect(self, from_url: str, to_url: str) -> Redirect:
return Redirect(relative_url, headers=headers)


def scantree(root: str) -> Iterator[tuple[str, os.stat_result]]:
def scantree(root: str) -> Generator[tuple[str, os.stat_result], None, None]:
"""
Recurse the given directory yielding (pathname, os.stat(pathname)) pairs
"""
Expand Down
8 changes: 4 additions & 4 deletions src/whitenoise/responders.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def __init__(self, fileobj: BinaryIO, start: int, end: int) -> None:
self.fileobj = fileobj
self.remaining = end - start + 1

def read(self, size: int = -1) -> bytes:
def read(self, size: int | None = -1) -> bytes:
if self.remaining <= 0:
return b""
if size < 0:
if size is None or size < 0:
size = self.remaining
else:
size = min(size, self.remaining)
Expand Down Expand Up @@ -235,12 +235,12 @@ def get_path_and_headers(self, request_headers):


class Redirect:
def __init__(self, location, headers=None):
def __init__(self, location: str, headers: dict[str, str] | None = None) -> None:
headers = list(headers.items()) if headers else []
headers.append(("Location", quote(location.encode("utf8"))))
self.response = Response(HTTPStatus.FOUND, headers, None)

def get_response(self, method, request_headers):
def get_response(self, method: str, request_headers: dict[str, str]) -> Response:
return self.response


Expand Down
2 changes: 1 addition & 1 deletion src/whitenoise/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def post_process(self, *args, **kwargs):
processed = self.make_helpful_exception(processed, name)
yield name, hashed_name, processed

def make_helpful_exception(self, exception, name):
def make_helpful_exception(self, exception: Exception, name: str) -> Exception:
if isinstance(exception, ValueError):
message = exception.args[0] if len(exception.args) else ""
# Stringly typed exceptions. Yay!
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def close(self) -> None:


class Files:
def __init__(self, directory, **files):
def __init__(self, directory: str, **files: str) -> None:
self.directory = os.path.join(TEST_FILE_PATH, directory)
for name, path in files.items():
url = f"/{AppServer.PREFIX}/{path}"
Expand Down

0 comments on commit 9f687c5

Please sign in to comment.