Resolve typing of stacked route definitions (#2455)

This commit is contained in:
Adam Hopkins 2022-06-16 15:15:20 +03:00 committed by GitHub
parent 1668e1532f
commit a411bc06e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,7 +8,17 @@ from pathlib import PurePath
from re import sub
from textwrap import dedent
from time import gmtime, strftime
from typing import Any, Callable, Iterable, List, Optional, Set, Tuple, Union
from typing import (
Any,
Callable,
Iterable,
List,
Optional,
Set,
Tuple,
Union,
cast,
)
from urllib.parse import unquote
from sanic_routing.route import Route # type: ignore
@ -283,7 +293,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix: str = "/v",
error_format: Optional[str] = None,
**ctx_kwargs,
) -> RouteWrapper:
) -> RouteHandler:
"""
Add an API URL under the **GET** *HTTP* method
@ -299,7 +309,9 @@ class RouteMixin(metaclass=SanicMeta):
will be appended to the route context (``route.ctx``)
:return: Object decorated with :func:`route` method
"""
return self.route(
return cast(
RouteHandler,
self.route(
uri,
methods=frozenset({"GET"}),
host=host,
@ -310,6 +322,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix=version_prefix,
error_format=error_format,
**ctx_kwargs,
),
)
def post(
@ -323,7 +336,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix: str = "/v",
error_format: Optional[str] = None,
**ctx_kwargs,
) -> RouteWrapper:
) -> RouteHandler:
"""
Add an API URL under the **POST** *HTTP* method
@ -339,7 +352,9 @@ class RouteMixin(metaclass=SanicMeta):
will be appended to the route context (``route.ctx``)
:return: Object decorated with :func:`route` method
"""
return self.route(
return cast(
RouteHandler,
self.route(
uri,
methods=frozenset({"POST"}),
host=host,
@ -350,6 +365,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix=version_prefix,
error_format=error_format,
**ctx_kwargs,
),
)
def put(
@ -363,7 +379,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix: str = "/v",
error_format: Optional[str] = None,
**ctx_kwargs,
) -> RouteWrapper:
) -> RouteHandler:
"""
Add an API URL under the **PUT** *HTTP* method
@ -379,7 +395,9 @@ class RouteMixin(metaclass=SanicMeta):
will be appended to the route context (``route.ctx``)
:return: Object decorated with :func:`route` method
"""
return self.route(
return cast(
RouteHandler,
self.route(
uri,
methods=frozenset({"PUT"}),
host=host,
@ -390,6 +408,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix=version_prefix,
error_format=error_format,
**ctx_kwargs,
),
)
def head(
@ -403,7 +422,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix: str = "/v",
error_format: Optional[str] = None,
**ctx_kwargs,
) -> RouteWrapper:
) -> RouteHandler:
"""
Add an API URL under the **HEAD** *HTTP* method
@ -427,7 +446,9 @@ class RouteMixin(metaclass=SanicMeta):
will be appended to the route context (``route.ctx``)
:return: Object decorated with :func:`route` method
"""
return self.route(
return cast(
RouteHandler,
self.route(
uri,
methods=frozenset({"HEAD"}),
host=host,
@ -438,6 +459,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix=version_prefix,
error_format=error_format,
**ctx_kwargs,
),
)
def options(
@ -451,7 +473,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix: str = "/v",
error_format: Optional[str] = None,
**ctx_kwargs,
) -> RouteWrapper:
) -> RouteHandler:
"""
Add an API URL under the **OPTIONS** *HTTP* method
@ -475,7 +497,9 @@ class RouteMixin(metaclass=SanicMeta):
will be appended to the route context (``route.ctx``)
:return: Object decorated with :func:`route` method
"""
return self.route(
return cast(
RouteHandler,
self.route(
uri,
methods=frozenset({"OPTIONS"}),
host=host,
@ -486,6 +510,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix=version_prefix,
error_format=error_format,
**ctx_kwargs,
),
)
def patch(
@ -499,7 +524,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix: str = "/v",
error_format: Optional[str] = None,
**ctx_kwargs,
) -> RouteWrapper:
) -> RouteHandler:
"""
Add an API URL under the **PATCH** *HTTP* method
@ -525,7 +550,9 @@ class RouteMixin(metaclass=SanicMeta):
will be appended to the route context (``route.ctx``)
:return: Object decorated with :func:`route` method
"""
return self.route(
return cast(
RouteHandler,
self.route(
uri,
methods=frozenset({"PATCH"}),
host=host,
@ -536,6 +563,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix=version_prefix,
error_format=error_format,
**ctx_kwargs,
),
)
def delete(
@ -549,7 +577,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix: str = "/v",
error_format: Optional[str] = None,
**ctx_kwargs,
) -> RouteWrapper:
) -> RouteHandler:
"""
Add an API URL under the **DELETE** *HTTP* method
@ -565,7 +593,9 @@ class RouteMixin(metaclass=SanicMeta):
will be appended to the route context (``route.ctx``)
:return: Object decorated with :func:`route` method
"""
return self.route(
return cast(
RouteHandler,
self.route(
uri,
methods=frozenset({"DELETE"}),
host=host,
@ -576,6 +606,7 @@ class RouteMixin(metaclass=SanicMeta):
version_prefix=version_prefix,
error_format=error_format,
**ctx_kwargs,
),
)
def websocket(