Skip to content

Commit

Permalink
feat: type more falcon modules (#2171)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
copalco and CaselIT authored Aug 11, 2024
1 parent 3b35eea commit 17282d8
Show file tree
Hide file tree
Showing 16 changed files with 542 additions and 137 deletions.
2 changes: 1 addition & 1 deletion falcon/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ def add_static_route(
self._static_routes.insert(0, (sr, sr, False))
self._update_sink_and_static_routes()

def add_sink(self, sink: Callable, prefix: SinkPrefix = r'/'):
def add_sink(self, sink: Callable, prefix: SinkPrefix = r'/') -> None:
"""Register a sink method for the App.
If no route matches a request, but the path in the requested URI
Expand Down
12 changes: 7 additions & 5 deletions falcon/app_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

"""Utilities for the App class."""

from __future__ import annotations

from inspect import iscoroutinefunction
from typing import IO, Iterable, List, Tuple

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


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

def __init__(self, stream: IO, block_size: int):
def __init__(self, stream: IO, block_size: int) -> None:
self._stream = stream
self._block_size = block_size

def __iter__(self):
def __iter__(self) -> CloseableStreamIterator:
return self

def __next__(self):
def __next__(self) -> bytes:
data = self._stream.read(self._block_size)

if data == b'':
raise StopIteration
else:
return data

def close(self):
def close(self) -> None:
try:
self._stream.close()
except (AttributeError, TypeError):
Expand Down
2 changes: 2 additions & 0 deletions falcon/asgi_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

from __future__ import annotations


class EventType:
"""Standard ASGI event type strings."""
Expand Down
Loading

0 comments on commit 17282d8

Please sign in to comment.