From cdc5dd6b752ee320a7b45678e999b19b8b5caf5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= <98187+Tronic@users.noreply.github.com> Date: Wed, 25 Oct 2023 02:36:08 +0000 Subject: [PATCH] Hand-fixed typing extra () and changed some formatting to f-strings that Ruff had missed --- sanic/blueprints.py | 10 +++++----- sanic/config.py | 10 +++++----- sanic/headers.py | 2 +- sanic/http/tls/context.py | 2 +- sanic/mixins/startup.py | 8 ++++---- sanic/request/parameters.py | 4 ++-- sanic/request/types.py | 6 +++--- sanic/response/convenience.py | 9 ++++----- sanic/response/types.py | 2 +- sanic/router.py | 2 +- sanic/utils.py | 2 +- 11 files changed, 28 insertions(+), 29 deletions(-) diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 2d5e3805..4324d700 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -118,7 +118,7 @@ class Blueprint(BaseSanic): name: str, url_prefix: str | None = None, host: list[str] | str | None = None, - version: int | (str | float) | None = None, + version: int | str | float | None = None, strict_slashes: bool | None = None, version_prefix: str = "/v", ): @@ -203,7 +203,7 @@ class Blueprint(BaseSanic): self, name: str, url_prefix: str | Default | None = _default, - version: int | (str | (float | Default)) | None = _default, + version: int | str | float | Default | None = _default, version_prefix: str | Default = _default, allow_route_overwrite: bool | Default = _default, strict_slashes: bool | Default | None = _default, @@ -273,7 +273,7 @@ class Blueprint(BaseSanic): def group( *blueprints: Blueprint | BlueprintGroup, url_prefix: str | None = None, - version: int | (str | float) | None = None, + version: int | str | float | None = None, strict_slashes: bool | None = None, version_prefix: str = "/v", name_prefix: str | None = "", @@ -675,7 +675,7 @@ class BlueprintGroup(bpg_base): self._name_prefix = name_prefix @property - def url_prefix(self) -> int | (str | float) | None: + def url_prefix(self) -> int | str | float | None: """The URL prefix for the Blueprint Group. Returns: @@ -695,7 +695,7 @@ class BlueprintGroup(bpg_base): return self._blueprints @property - def version(self) -> str | (int | float) | None: + def version(self) -> str | int | float | None: """API Version for the Blueprint Group, if any. Returns: diff --git a/sanic/config.py b/sanic/config.py index df1eff34..d58cbd1d 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -98,14 +98,14 @@ class Config(dict, metaclass=DescriptorMeta): INSPECTOR: bool INSPECTOR_HOST: str INSPECTOR_PORT: int - INSPECTOR_TLS_KEY: Path | (str | Default) - INSPECTOR_TLS_CERT: Path | (str | Default) + INSPECTOR_TLS_KEY: Path | str | Default + INSPECTOR_TLS_CERT: Path | str | Default INSPECTOR_API_KEY: str KEEP_ALIVE_TIMEOUT: int KEEP_ALIVE: bool LOCAL_CERT_CREATOR: str | LocalCertCreator - LOCAL_TLS_KEY: Path | (str | Default) - LOCAL_TLS_CERT: Path | (str | Default) + LOCAL_TLS_KEY: Path | str | Default + LOCAL_TLS_CERT: Path | str | Default LOCALHOST: str MOTD: bool MOTD_DISPLAY: dict[str, str] @@ -323,7 +323,7 @@ class Config(dict, metaclass=DescriptorMeta): except ValueError: pass - def update_config(self, config: bytes | (str | (dict | Any))): + def update_config(self, config: bytes | str | dict | Any): """Update app.config. .. note:: diff --git a/sanic/headers.py b/sanic/headers.py index 8b618a4e..892452b9 100644 --- a/sanic/headers.py +++ b/sanic/headers.py @@ -529,7 +529,7 @@ def format_http1_response(status: int, headers: HeaderBytesIterable) -> bytes: def parse_credentials( header: str | None, - prefixes: list | (tuple | set) | None = None, + prefixes: list | tuple | set | None = None, ) -> tuple[str | None, str | None]: """Parses any header with the aim to retrieve any credentials from it. diff --git a/sanic/http/tls/context.py b/sanic/http/tls/context.py index 7c03091f..911a634a 100644 --- a/sanic/http/tls/context.py +++ b/sanic/http/tls/context.py @@ -55,7 +55,7 @@ def shorthand_to_ctx( def process_to_context( - ssldef: None | (ssl.SSLContext | (dict | (str | (list | tuple)))) + ssldef: None | ssl.SSLContext | dict | str | list | tuple ) -> ssl.SSLContext | None: """Process app.run ssl argument from easy formats to full SSLContext.""" return ( diff --git a/sanic/mixins/startup.py b/sanic/mixins/startup.py index 488e4fa7..b743b827 100644 --- a/sanic/mixins/startup.py +++ b/sanic/mixins/startup.py @@ -157,7 +157,7 @@ class StartupMixin(metaclass=SanicMeta): debug: bool = False, auto_reload: bool | None = None, version: HTTPVersion = HTTP.VERSION_1, - ssl: None | (SSLContext | (dict | (str | (list | tuple)))) = None, + ssl: None | SSLContext | dict | str | list | tuple = None, sock: socket | None = None, workers: int = 1, protocol: type[Protocol] | None = None, @@ -287,7 +287,7 @@ class StartupMixin(metaclass=SanicMeta): debug: bool = False, auto_reload: bool | None = None, version: HTTPVersion = HTTP.VERSION_1, - ssl: None | (SSLContext | (dict | (str | (list | tuple)))) = None, + ssl: None | SSLContext | dict | str | list | tuple = None, sock: socket | None = None, workers: int = 1, protocol: type[Protocol] | None = None, @@ -466,7 +466,7 @@ class StartupMixin(metaclass=SanicMeta): port: int | None = None, *, debug: bool = False, - ssl: None | (SSLContext | (dict | (str | (list | tuple)))) = None, + ssl: None | SSLContext | dict | str | list | tuple = None, sock: socket | None = None, protocol: type[Protocol] | None = None, backlog: int = 100, @@ -632,7 +632,7 @@ class StartupMixin(metaclass=SanicMeta): port: int | None = None, debug: bool = False, version: HTTPVersion = HTTP.VERSION_1, - ssl: None | (SSLContext | (dict | (str | (list | tuple)))) = None, + ssl: None | SSLContext | dict | str | list | tuple = None, sock: socket | None = None, unix: str | None = None, workers: int = 1, diff --git a/sanic/request/parameters.py b/sanic/request/parameters.py index 08940099..9e9e7ad7 100644 --- a/sanic/request/parameters.py +++ b/sanic/request/parameters.py @@ -6,7 +6,7 @@ from typing import Any class RequestParameters(dict): """Hosts a dict with lists as values where get returns the first value of the list and getlist returns the whole shebang""" # noqa: E501 - def get(self, name: str, default: Any | None = None) -> Any | None: + def get(self, name: str, default: Any = None) -> Any: """Return the first value, either the default or actual Args: @@ -18,7 +18,7 @@ class RequestParameters(dict): """ # noqa: E501 return super().get(name, [default])[0] - def getlist(self, name: str, default: Any | None = None) -> Any | None: + def getlist(self, name: str, default: Any = None) -> Any: """Return the entire list Args: diff --git a/sanic/request/types.py b/sanic/request/types.py index 634cce1b..59b7b3f5 100644 --- a/sanic/request/types.py +++ b/sanic/request/types.py @@ -155,7 +155,7 @@ class Request(Generic[sanic_type, ctx_type]): except HttpParserInvalidURLError: url = url_bytes.decode(errors="backslashreplace") raise BadURL(f"Bad URL: {url}") - self._id: uuid.UUID | (str | int) | None = None + self._id: uuid.UUID | str | int | None = None self._name: str | None = None self._stream_id = stream_id self.app = app @@ -245,7 +245,7 @@ class Request(Generic[sanic_type, ctx_type]): return request @classmethod - def generate_id(*_) -> uuid.UUID | (str | int): + def generate_id(*_) -> uuid.UUID | str | int: """Generate a unique ID for the request. This method is called to generate a unique ID for each request. @@ -488,7 +488,7 @@ class Request(Generic[sanic_type, ctx_type]): return bytes(reqline) @property - def id(self) -> uuid.UUID | (str | int) | None: + def id(self) -> uuid.UUID | str | int | None: """A request ID passed from the client, or generated from the backend. By default, this will look in a request header defined at: diff --git a/sanic/response/convenience.py b/sanic/response/convenience.py index 747093d4..ccad7435 100644 --- a/sanic/response/convenience.py +++ b/sanic/response/convenience.py @@ -120,7 +120,7 @@ def raw( def html( - body: str | (bytes | HTMLProtocol), + body: str | bytes | HTMLProtocol, status: int = 200, headers: dict[str, str] | None = None, ) -> HTTPResponse: @@ -151,7 +151,7 @@ def html( async def validate_file( - request_headers: Header, last_modified: datetime | (float | int) + request_headers: Header, last_modified: datetime | float | int ) -> HTTPResponse | None: """Validate file based on request headers. @@ -170,8 +170,7 @@ async def validate_file( if_modified_since = parsedate_to_datetime(if_modified_since) except (TypeError, ValueError): logger.warning( - "Ignoring invalid If-Modified-Since header received: '%s'", - if_modified_since, + f"Ignoring invalid If-Modified-Since header received: '{if_modified_since}'" ) return None if not isinstance(last_modified, datetime): @@ -211,7 +210,7 @@ async def file( mime_type: str | None = None, headers: dict[str, str] | None = None, filename: str | None = None, - last_modified: datetime | (float | (int | Default)) | None = _default, + last_modified: datetime | float | int | Default | None = _default, max_age: float | int | None = None, no_store: bool | None = None, _range: Range | None = None, diff --git a/sanic/response/types.py b/sanic/response/types.py index df0a02c8..21de0c4e 100644 --- a/sanic/response/types.py +++ b/sanic/response/types.py @@ -64,7 +64,7 @@ class BaseHTTPResponse: self.asgi: bool = False self.body: bytes | None = None self.content_type: str | None = None - self.stream: Http | (ASGIApp | HTTPReceiver) | None = None + self.stream: Http | ASGIApp | HTTPReceiver | None = None self.status: int = None self.headers = Header({}) self._cookies: CookieJar | None = None diff --git a/sanic/router.py b/sanic/router.py index bf1a7a9a..dedcf1e9 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -82,7 +82,7 @@ class Router(BaseRouter): strict_slashes: bool = False, stream: bool = False, ignore_body: bool = False, - version: str | (float | int) | None = None, + version: str | float | int | None = None, name: str | None = None, unquote: bool = False, static: bool = False, diff --git a/sanic/utils.py b/sanic/utils.py index cd1e3b4f..83538f7d 100644 --- a/sanic/utils.py +++ b/sanic/utils.py @@ -128,4 +128,4 @@ def load_module_from_file_location( try: return import_string(location) except ValueError: - raise OSError("Unable to load configuration %s" % str(location)) + raise OSError(f"Unable to load configuration {location!s}")