Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport PR 2973 #2975

Merged
merged 4 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/exception_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class' default handler, we can do anything including sending exceptions to
an external service.
"""

from sanic import Sanic
from sanic.exceptions import SanicException
from sanic.handlers import ErrorHandler
Expand Down
1 change: 1 addition & 0 deletions examples/pytest_xdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

$ pytest examples/pytest_xdist.py -n 8 # 8 workers
"""

import re

import pytest
Expand Down
1 change: 1 addition & 0 deletions guide/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Sanic
- html5tagger
- HTMX"""

from pathlib import Path

from webapp.worker.factory import create_app
Expand Down
35 changes: 15 additions & 20 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,7 @@ def __init__(
inspector: bool = False,
inspector_class: Optional[Type[Inspector]] = None,
certloader_class: Optional[Type[CertLoader]] = None,
) -> None:
...
) -> None: ...

@overload
def __init__(
Expand All @@ -269,8 +268,7 @@ def __init__(
inspector: bool = False,
inspector_class: Optional[Type[Inspector]] = None,
certloader_class: Optional[Type[CertLoader]] = None,
) -> None:
...
) -> None: ...

@overload
def __init__(
Expand All @@ -291,8 +289,7 @@ def __init__(
inspector: bool = False,
inspector_class: Optional[Type[Inspector]] = None,
certloader_class: Optional[Type[CertLoader]] = None,
) -> None:
...
) -> None: ...

@overload
def __init__(
Expand All @@ -313,8 +310,7 @@ def __init__(
inspector: bool = False,
inspector_class: Optional[Type[Inspector]] = None,
certloader_class: Optional[Type[CertLoader]] = None,
) -> None:
...
) -> None: ...

def __init__(
self,
Expand Down Expand Up @@ -380,7 +376,7 @@ def __init__(
self.listeners: Dict[str, List[ListenerType[Any]]] = defaultdict(list)
self.named_request_middleware: Dict[str, Deque[Middleware]] = {}
self.named_response_middleware: Dict[str, Deque[Middleware]] = {}
self.request_class: Type[Request] = request_class or Request
self.request_class: Type[Request] = request_class or Request # type: ignore
self.request_middleware: Deque[Middleware] = deque()
self.response_middleware: Deque[Middleware] = deque()
self.router: Router = router or Router()
Expand Down Expand Up @@ -661,8 +657,7 @@ def dispatch(
fail_not_found: bool = True,
inline: Literal[True],
reverse: bool = False,
) -> Coroutine[Any, Any, Awaitable[Any]]:
...
) -> Coroutine[Any, Any, Awaitable[Any]]: ...

@overload
def dispatch(
Expand All @@ -674,8 +669,7 @@ def dispatch(
fail_not_found: bool = True,
inline: Literal[False] = False,
reverse: bool = False,
) -> Coroutine[Any, Any, Awaitable[Task]]:
...
) -> Coroutine[Any, Any, Awaitable[Task]]: ...

def dispatch(
self,
Expand Down Expand Up @@ -1114,7 +1108,7 @@ def url_for(self, view_name: str, **kwargs):
)
passes_pattern = pattern.match(supplied_param)
if not passes_pattern:
if param_info.cast != str:
if param_info.cast is not str:
msg = (
f'Value "{supplied_param}" '
f"for parameter `{param_info.name}` does "
Expand Down Expand Up @@ -1778,18 +1772,19 @@ def add_task(
return None

@overload
def get_task(self, name: str, *, raise_exception: Literal[True]) -> Task:
...
def get_task(
self, name: str, *, raise_exception: Literal[True]
) -> Task: ...

@overload
def get_task(
self, name: str, *, raise_exception: Literal[False]
) -> Optional[Task]:
...
) -> Optional[Task]: ...

@overload
def get_task(self, name: str, *, raise_exception: bool) -> Optional[Task]:
...
def get_task(
self, name: str, *, raise_exception: bool
) -> Optional[Task]: ...

def get_task(
self, name: str, *, raise_exception: bool = True
Expand Down
18 changes: 6 additions & 12 deletions sanic/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,12 +779,10 @@ def __iter__(self) -> Iterator[Blueprint]:
return iter(self._blueprints)

@overload
def __getitem__(self, item: int) -> Blueprint:
...
def __getitem__(self, item: int) -> Blueprint: ...

@overload
def __getitem__(self, item: slice) -> MutableSequence[Blueprint]:
...
def __getitem__(self, item: slice) -> MutableSequence[Blueprint]: ...

def __getitem__(
self, item: Union[int, slice]
Expand All @@ -807,12 +805,10 @@ def __getitem__(
return self._blueprints[item]

@overload
def __setitem__(self, index: int, item: Blueprint) -> None:
...
def __setitem__(self, index: int, item: Blueprint) -> None: ...

@overload
def __setitem__(self, index: slice, item: Iterable[Blueprint]) -> None:
...
def __setitem__(self, index: slice, item: Iterable[Blueprint]) -> None: ...

def __setitem__(
self,
Expand Down Expand Up @@ -848,12 +844,10 @@ def __setitem__(
raise TypeError("Index must be int or slice")

@overload
def __delitem__(self, index: int) -> None:
...
def __delitem__(self, index: int) -> None: ...

@overload
def __delitem__(self, index: slice) -> None:
...
def __delitem__(self, index: slice) -> None: ...

def __delitem__(self, index: Union[int, slice]) -> None:
"""Delete the Blueprint object at the specified index.
Expand Down
3 changes: 1 addition & 2 deletions sanic/cli/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def add_bool_arguments(self, *args, nullable=False, **kwargs):
params = {args[0][2:].replace("-", "_"): None}
group.set_defaults(**params)

def prepare(self, args) -> None:
...
def prepare(self, args) -> None: ...


class GeneralGroup(Group):
Expand Down
3 changes: 1 addition & 2 deletions sanic/cli/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ def respond(self, response):
response.stream = self
return response

async def send(self, data, end_stream):
...
async def send(self, data, end_stream): ...


class Result(NamedTuple):
Expand Down
2 changes: 2 additions & 0 deletions sanic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"FORWARDED_FOR_HEADER": "X-Forwarded-For",
"FORWARDED_SECRET": None,
"GRACEFUL_SHUTDOWN_TIMEOUT": 15.0,
"GRACEFUL_TCP_CLOSE_TIMEOUT": 5.0,
"INSPECTOR": False,
"INSPECTOR_HOST": "localhost",
"INSPECTOR_PORT": 6457,
Expand Down Expand Up @@ -102,6 +103,7 @@ class Config(dict, metaclass=DescriptorMeta):
FORWARDED_FOR_HEADER: str
FORWARDED_SECRET: Optional[str]
GRACEFUL_SHUTDOWN_TIMEOUT: float
GRACEFUL_TCP_CLOSE_TIMEOUT: float
INSPECTOR: bool
INSPECTOR_HOST: str
INSPECTOR_PORT: int
Expand Down
2 changes: 1 addition & 1 deletion sanic/cookies/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


COOKIE_NAME_RESERVED_CHARS = re.compile(
'[\x00-\x1F\x7F-\xFF()<>@,;:\\\\"/[\\]?={} \x09]'
'[\x00-\x1f\x7f-\xff()<>@,;:\\\\"/[\\]?={} \x09]'
)
OCTAL_PATTERN = re.compile(r"\\[0-3][0-7][0-7]")
QUOTE_PATTERN = re.compile(r"[\\].")
Expand Down
2 changes: 1 addition & 1 deletion sanic/cookies/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

LEGAL_CHARS = string.ascii_letters + string.digits + "!#$%&'*+-.^_`|~:"
UNESCAPED_CHARS = LEGAL_CHARS + " ()/<=>?@[]{}"
TRANSLATOR = {ch: f"\\{ch:03o}" for ch in bytes(range(32)) + b'";\\\x7F'}
TRANSLATOR = {ch: f"\\{ch:03o}" for ch in bytes(range(32)) + b'";\\\x7f'}


def _quote(str): # no cov
Expand Down
1 change: 1 addition & 0 deletions sanic/errorpages.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
will attempt to provide an appropriate response format based upon the
request type.
"""

from __future__ import annotations

import sys
Expand Down
6 changes: 3 additions & 3 deletions sanic/handlers/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ def lookup(self, exception, route_name: Optional[str] = None):
exception_key = (ancestor, name)
if exception_key in self.cached_handlers:
handler = self.cached_handlers[exception_key]
self.cached_handlers[
(exception_class, route_name)
] = handler
self.cached_handlers[(exception_class, route_name)] = (
handler
)
return handler

if ancestor is BaseException:
Expand Down
6 changes: 2 additions & 4 deletions sanic/http/http3.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,13 @@ def _send(self, data: bytes, end_stream: bool) -> None:
class WebsocketReceiver(Receiver): # noqa
"""Websocket receiver implementation."""

async def run(self):
...
async def run(self): ...


class WebTransportReceiver(Receiver): # noqa
"""WebTransport receiver implementation."""

async def run(self):
...
async def run(self): ...


class Http3:
Expand Down
6 changes: 2 additions & 4 deletions sanic/mixins/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def listener(
apply: bool = ...,
*,
priority: int = 0,
) -> ListenerType[Sanic]:
...
) -> ListenerType[Sanic]: ...

@overload
def listener(
Expand All @@ -51,8 +50,7 @@ def listener(
apply: bool = ...,
*,
priority: int = 0,
) -> Callable[[ListenerType[Sanic]], ListenerType[Sanic]]:
...
) -> Callable[[ListenerType[Sanic]], ListenerType[Sanic]]: ...

def listener(
self,
Expand Down
6 changes: 2 additions & 4 deletions sanic/mixins/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def middleware(
apply: bool = True,
*,
priority: int = 0,
) -> MiddlewareType:
...
) -> MiddlewareType: ...

@overload
def middleware(
Expand All @@ -37,8 +36,7 @@ def middleware(
apply: bool = True,
*,
priority: int = 0,
) -> Callable[[MiddlewareType], MiddlewareType]:
...
) -> Callable[[MiddlewareType], MiddlewareType]: ...

def middleware(
self,
Expand Down
3 changes: 1 addition & 2 deletions sanic/mixins/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ def add_signal(
"""
if not handler:

async def noop(**context):
...
async def noop(**context): ...

handler = noop
self.signal(event=event, condition=condition, exclusive=exclusive)(
Expand Down
3 changes: 1 addition & 2 deletions sanic/models/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,4 @@ class FutureSignal(NamedTuple):
priority: int


class FutureRegistry(set):
...
class FutureRegistry(set): ...
6 changes: 2 additions & 4 deletions sanic/models/protocol_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
from typing import Protocol

class HTMLProtocol(Protocol):
def __html__(self) -> Union[str, bytes]:
...
def __html__(self) -> Union[str, bytes]: ...

def _repr_html_(self) -> Union[str, bytes]:
...
def _repr_html_(self) -> Union[str, bytes]: ...

class Range(Protocol):
start: Optional[int]
Expand Down
3 changes: 1 addition & 2 deletions sanic/pages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def _foot(self) -> None:
self.doc.div("DEBUG mode")

@abstractmethod
def _body(self) -> None:
...
def _body(self) -> None: ...

def _sanic_logo(self) -> None:
self.doc.a(
Expand Down
2 changes: 1 addition & 1 deletion sanic/request/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
)
else:
sanic_type = TypeVar("sanic_type")
ctx_type = TypeVar("ctx_type")
ctx_type = TypeVar("ctx_type", default=SimpleNamespace)


class Request(Generic[sanic_type, ctx_type]):
Expand Down
6 changes: 3 additions & 3 deletions sanic/response/convenience.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ async def file(
if _range:
await f.seek(_range.start)
out_stream = await f.read(_range.size)
headers[
"Content-Range"
] = f"bytes {_range.start}-{_range.end}/{_range.total}"
headers["Content-Range"] = (
f"bytes {_range.start}-{_range.end}/{_range.total}"
)
status = 206
else:
out_stream = await f.read()
Expand Down
Loading
Loading