Skip to content

Commit 17282d8

Browse files
copalcoCaselIT
andauthored
feat: type more falcon modules (#2171)
* feat: Type app helpers module * feat: Add typing to errors module * feat: Add typings to forwarded module * feat: Add typing to hooks * feat: Add typing to falcon hooks * feat: Add typing to http_error module * feat: Extract RawHeaders and NormalizedHeaders to typing module * feat: Extract status to typing module * feat: Add typing to http_status module * feat: Add typing to inspect module * feat: Add typing to middleware module * feat: Replace protocol with interface * feat: Add typing to redirects * feat: Type vendor mimeparse * Changed RawHeaders to not include None * Reformated imports * Test that interface raises not implemented * Type algorithm int values as float * Changed allowed methods to Iterable * Imported annotations in hooks * Change argnames type to list of strings * Changed Dict to mutable mapping * Fixed formatting * Remove unused imports * Fix typing * Replaced assert with cast * Fix blue * Type resource as object * Fix style * Revert "Type algorithm int values as float" This reverts commit ca1df71. * Revert "feat: Type vendor mimeparse" This reverts commit 11ca7ca. * Ignore vendore package * Use async package instead of importing AsyncRequest and AsyncResponse and aliasing them * Solve circular imports while typing * Fix style * Changed inspect obj type to Any * Import annotations where missing * Replace Union with | where future annotations imported * Revert "Replace Union with | where future annotations imported" This reverts commit fd8b3be. * Improve imports to avoid them inside functions * Fix typo * Rename Kwargs to HTTPErrorKeywordArgs * Import whole package insted of specific types * Fix style * Replace Serializer and MediaHandler with protocol * Add assertion reason message * Fix import issue * Fix import order * Fix coverage issues * Add ResponderOrResource and Action types * style: run ruff * typing: apply review feedmack * typing: avoid protocols for handlers * typing: improve type alias name * fix: restore support to python 3.8 and 3.7 --------- Co-authored-by: Federico Caselli <cfederico87@gmail.com>
1 parent 3b35eea commit 17282d8

16 files changed

+542
-137
lines changed

falcon/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ def add_static_route(
674674
self._static_routes.insert(0, (sr, sr, False))
675675
self._update_sink_and_static_routes()
676676

677-
def add_sink(self, sink: Callable, prefix: SinkPrefix = r'/'):
677+
def add_sink(self, sink: Callable, prefix: SinkPrefix = r'/') -> None:
678678
"""Register a sink method for the App.
679679
680680
If no route matches a request, but the path in the requested URI

falcon/app_helpers.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
"""Utilities for the App class."""
1616

17+
from __future__ import annotations
18+
1719
from inspect import iscoroutinefunction
1820
from typing import IO, Iterable, List, Tuple
1921

@@ -202,7 +204,7 @@ def prepare_middleware_ws(middleware: Iterable) -> Tuple[list, list]:
202204
return request_mw, resource_mw
203205

204206

205-
def default_serialize_error(req: Request, resp: Response, exception: HTTPError):
207+
def default_serialize_error(req: Request, resp: Response, exception: HTTPError) -> None:
206208
"""Serialize the given instance of HTTPError.
207209
208210
This function determines which of the supported media types, if
@@ -281,22 +283,22 @@ class CloseableStreamIterator:
281283
block_size (int): Number of bytes to read per iteration.
282284
"""
283285

284-
def __init__(self, stream: IO, block_size: int):
286+
def __init__(self, stream: IO, block_size: int) -> None:
285287
self._stream = stream
286288
self._block_size = block_size
287289

288-
def __iter__(self):
290+
def __iter__(self) -> CloseableStreamIterator:
289291
return self
290292

291-
def __next__(self):
293+
def __next__(self) -> bytes:
292294
data = self._stream.read(self._block_size)
293295

294296
if data == b'':
295297
raise StopIteration
296298
else:
297299
return data
298300

299-
def close(self):
301+
def close(self) -> None:
300302
try:
301303
self._stream.close()
302304
except (AttributeError, TypeError):

falcon/asgi_spec.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
"""Constants, etc. defined by the ASGI specification."""
1616

17+
from __future__ import annotations
18+
1719

1820
class EventType:
1921
"""Standard ASGI event type strings."""

0 commit comments

Comments
 (0)