From af41e56bf0444bd17311607836ae749ebef95f89 Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Sun, 11 Aug 2024 23:22:18 +0200 Subject: [PATCH] typing: apply review feedmack --- falcon/errors.py | 84 +++++++++++++++++++++---------------------- falcon/http_error.py | 4 +-- falcon/http_status.py | 4 +-- falcon/redirects.py | 22 ++++-------- falcon/request.py | 3 +- falcon/typing.py | 4 +-- 6 files changed, 55 insertions(+), 66 deletions(-) diff --git a/falcon/errors.py b/falcon/errors.py index 5175308eb..afc1faa8a 100644 --- a/falcon/errors.py +++ b/falcon/errors.py @@ -45,8 +45,8 @@ def on_get(self, req, resp): from falcon.util.misc import dt_to_http if TYPE_CHECKING: - from falcon.typing import NormalizedHeaders - from falcon.typing import RawHeaders + from falcon.typing import HeaderList + from falcon.typing import Headers __all__ = ( @@ -229,7 +229,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ) -> None: super().__init__( @@ -312,7 +312,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, challenges: Optional[Iterable[str]] = None, **kwargs: HTTPErrorKeywordArguments, ): @@ -390,7 +390,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -461,7 +461,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -588,7 +588,7 @@ def __init__( allowed_methods: Iterable[str], title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): headers = _load_headers(headers) @@ -659,7 +659,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -732,7 +732,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -811,7 +811,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -875,7 +875,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -940,7 +940,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -1016,7 +1016,7 @@ def __init__( title: Optional[str] = None, description: Optional[str] = None, retry_after: RetryAfter = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ) -> None: super().__init__( @@ -1086,7 +1086,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -1151,7 +1151,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -1230,7 +1230,7 @@ def __init__( resource_length: int, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): headers = _load_headers(headers) @@ -1300,7 +1300,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -1362,7 +1362,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -1423,7 +1423,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -1492,7 +1492,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -1566,7 +1566,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, retry_after: RetryAfter = None, **kwargs: HTTPErrorKeywordArguments, ): @@ -1635,7 +1635,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -1710,7 +1710,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -1771,7 +1771,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -1839,7 +1839,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -1900,7 +1900,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -1977,7 +1977,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, retry_after: RetryAfter = None, **kwargs: HTTPErrorKeywordArguments, ): @@ -2040,7 +2040,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -2107,7 +2107,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -2172,7 +2172,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -2234,7 +2234,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -2308,7 +2308,7 @@ def __init__( self, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): super().__init__( @@ -2367,7 +2367,7 @@ def __init__( self, msg: str, header_name: str, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): description = 'The value provided for the "{0}" header is invalid. {1}' @@ -2426,7 +2426,7 @@ class HTTPMissingHeader(HTTPBadRequest): def __init__( self, header_name: str, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ): description = 'The "{0}" header is required.' @@ -2489,7 +2489,7 @@ def __init__( self, msg: str, param_name: str, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ) -> None: description = 'The "{0}" parameter is invalid. {1}' @@ -2550,7 +2550,7 @@ class HTTPMissingParam(HTTPBadRequest): def __init__( self, param_name: str, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ) -> None: description = 'The "{0}" parameter is required.' @@ -2649,7 +2649,7 @@ class MediaMalformedError(HTTPBadRequest): """ def __init__( - self, media_type: str, **kwargs: Union[RawHeaders, HTTPErrorKeywordArguments] + self, media_type: str, **kwargs: Union[HeaderList, HTTPErrorKeywordArguments] ): super().__init__( title='Invalid {0}'.format(media_type), description=None, **kwargs @@ -2718,7 +2718,7 @@ def __init__( *, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, **kwargs: HTTPErrorKeywordArguments, ) -> None: super().__init__( @@ -2753,7 +2753,7 @@ class MultipartParseError(MediaMalformedError): def __init__( self, description: Optional[str] = None, - **kwargs: Union[RawHeaders, HTTPErrorKeywordArguments], + **kwargs: Union[HeaderList, HTTPErrorKeywordArguments], ) -> None: HTTPBadRequest.__init__( self, @@ -2768,7 +2768,7 @@ def __init__( # ----------------------------------------------------------------------------- -def _load_headers(headers: Optional[RawHeaders]) -> NormalizedHeaders: +def _load_headers(headers: Optional[HeaderList]) -> Headers: """Transform the headers to dict.""" if headers is None: return {} @@ -2778,9 +2778,9 @@ def _load_headers(headers: Optional[RawHeaders]) -> NormalizedHeaders: def _parse_retry_after( - headers: Optional[RawHeaders], + headers: Optional[HeaderList], retry_after: RetryAfter, -) -> Optional[RawHeaders]: +) -> Optional[HeaderList]: """Set the Retry-After to the headers when required.""" if retry_after is None: return headers diff --git a/falcon/http_error.py b/falcon/http_error.py index f684de63f..7f6c36be8 100644 --- a/falcon/http_error.py +++ b/falcon/http_error.py @@ -26,8 +26,8 @@ from falcon.util.deprecation import deprecated_args if TYPE_CHECKING: + from falcon.typing import HeaderList from falcon.typing import Link - from falcon.typing import RawHeaders from falcon.typing import Serializer from falcon.typing import Status @@ -123,7 +123,7 @@ def __init__( status: Status, title: Optional[str] = None, description: Optional[str] = None, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, href: Optional[str] = None, href_text: Optional[str] = None, code: Optional[int] = None, diff --git a/falcon/http_status.py b/falcon/http_status.py index 927b89bb4..040d2f83f 100644 --- a/falcon/http_status.py +++ b/falcon/http_status.py @@ -21,7 +21,7 @@ from falcon.util.deprecation import AttributeRemovedError if TYPE_CHECKING: - from falcon.typing import RawHeaders + from falcon.typing import HeaderList from falcon.typing import Status @@ -56,7 +56,7 @@ class HTTPStatus(Exception): def __init__( self, status: Status, - headers: Optional[RawHeaders] = None, + headers: Optional[HeaderList] = None, text: Optional[str] = None, ) -> None: self.status = status diff --git a/falcon/redirects.py b/falcon/redirects.py index e1001bbab..7d2381d47 100644 --- a/falcon/redirects.py +++ b/falcon/redirects.py @@ -21,7 +21,7 @@ from falcon.http_status import HTTPStatus if TYPE_CHECKING: - from falcon.typing import NormalizedHeaders + from falcon.typing import Headers class HTTPMovedPermanently(HTTPStatus): @@ -43,9 +43,7 @@ class HTTPMovedPermanently(HTTPStatus): response. """ - def __init__( - self, location: str, headers: Optional[NormalizedHeaders] = None - ) -> None: + def __init__(self, location: str, headers: Optional[Headers] = None) -> None: if headers is None: headers = {} headers.setdefault('location', location) @@ -74,9 +72,7 @@ class HTTPFound(HTTPStatus): response. """ - def __init__( - self, location: str, headers: Optional[NormalizedHeaders] = None - ) -> None: + def __init__(self, location: str, headers: Optional[Headers] = None) -> None: if headers is None: headers = {} headers.setdefault('location', location) @@ -110,9 +106,7 @@ class HTTPSeeOther(HTTPStatus): response. """ - def __init__( - self, location: str, headers: Optional[NormalizedHeaders] = None - ) -> None: + def __init__(self, location: str, headers: Optional[Headers] = None) -> None: if headers is None: headers = {} headers.setdefault('location', location) @@ -141,9 +135,7 @@ class HTTPTemporaryRedirect(HTTPStatus): response. """ - def __init__( - self, location: str, headers: Optional[NormalizedHeaders] = None - ) -> None: + def __init__(self, location: str, headers: Optional[Headers] = None) -> None: if headers is None: headers = {} headers.setdefault('location', location) @@ -169,9 +161,7 @@ class HTTPPermanentRedirect(HTTPStatus): response. """ - def __init__( - self, location: str, headers: Optional[NormalizedHeaders] = None - ) -> None: + def __init__(self, location: str, headers: Optional[Headers] = None) -> None: if headers is None: headers = {} headers.setdefault('location', location) diff --git a/falcon/request.py b/falcon/request.py index 6aa9a5982..f8fc6f4ab 100644 --- a/falcon/request.py +++ b/falcon/request.py @@ -14,7 +14,6 @@ from __future__ import annotations -from collections import UserDict from datetime import datetime from io import BytesIO from uuid import UUID @@ -2093,7 +2092,7 @@ class RequestOptions: auto_parse_qs_csv: bool strip_url_path_trailing_slash: bool default_media_type: str - media_handlers: UserDict + media_handlers: Handlers __slots__ = ( 'keep_blank_qs_values', diff --git a/falcon/typing.py b/falcon/typing.py index 9733e528f..cb345d485 100644 --- a/falcon/typing.py +++ b/falcon/typing.py @@ -64,6 +64,6 @@ def _resolve( # arguments afterwords? # class SinkCallable(Protocol): # def __call__(sef, req: Request, resp: Response, ): ... -NormalizedHeaders = Dict[str, str] -RawHeaders = Union[NormalizedHeaders, List[Tuple[str, str]]] +Headers = Dict[str, str] +HeaderList = Union[Headers, List[Tuple[str, str]]] Status = Union[http.HTTPStatus, str, int]