diff --git a/sanic/app.py b/sanic/app.py index 1ff5a079..00099267 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -14,6 +14,7 @@ from traceback import format_exc from typing import Any, Dict, Iterable, List, Optional, Set, Type, Union from urllib.parse import urlencode, urlunparse +from sanic_routing.exceptions import FinalizationError from sanic_routing.route import Route from sanic import reloader_helpers @@ -24,8 +25,6 @@ from sanic.blueprints import Blueprint from sanic.config import BASE_LOGO, Config from sanic.exceptions import ( InvalidUsage, - MethodNotSupported, - NotFound, SanicException, ServerError, URLBuildError, @@ -226,8 +225,6 @@ class Sanic(BaseSanic): return self.register_listener(listener.listener, listener.event) def _apply_route(self, route: FutureRoute) -> Route: - # TODO: - # - move websocket handler out and attach it when applying params = route._asdict() websocket = params.pop("websocket", False) subprotocols = params.pop("subprotocols", None) @@ -239,10 +236,8 @@ class Sanic(BaseSanic): route.handler, subprotocols=subprotocols, ) - websocket_handler.__name__ = ( - "websocket_handler_" + route.handler.__name__ - ) - websocket_handler.is_websocket = True + websocket_handler.__name__ = route.handler.__name__ # type: ignore + websocket_handler.is_websocket = True # type: ignore params["handler"] = websocket_handler return self.router.add(**params) @@ -304,7 +299,7 @@ class Sanic(BaseSanic): blueprint.register(self, options) def url_for(self, view_name: str, **kwargs): - r"""Build a URL based on a view name and the values provided. + """Build a URL based on a view name and the values provided. In order to build a URL, all request parameters must be supplied as keyword arguments, and each parameter must pass the test for the @@ -315,7 +310,7 @@ class Sanic(BaseSanic): the output URL's query string. :param view_name: string referencing the view name - :param \**kwargs: keys and values that are used to build request + :param **kwargs: keys and values that are used to build request parameters and query string arguments. :return: the built URL @@ -519,11 +514,9 @@ class Sanic(BaseSanic): # Fetch handler from router ( handler, - args, kwargs, uri, name, - endpoint, ignore_body, ) = self.router.get(request) request.name = name @@ -560,7 +553,7 @@ class Sanic(BaseSanic): request.endpoint = request.name # Run response handler - response = handler(request, *args, **kwargs) + response = handler(request, **kwargs) if isawaitable(response): response = await response if response: @@ -920,11 +913,9 @@ class Sanic(BaseSanic): ): """Helper function used by `run` and `create_server`.""" - # TODO: - # - Catch proper exception try: self.router.finalize() - except Exception as e: + except FinalizationError as e: if not Sanic.test_mode: raise e diff --git a/sanic/mixins/exceptions.py b/sanic/mixins/exceptions.py index e996be06..c48988de 100644 --- a/sanic/mixins/exceptions.py +++ b/sanic/mixins/exceptions.py @@ -1,5 +1,3 @@ -from enum import Enum, auto -from functools import partial from typing import Set from sanic.models.futures import FutureException diff --git a/sanic/mixins/routes.py b/sanic/mixins/routes.py index fcf7f5fd..878bd9eb 100644 --- a/sanic/mixins/routes.py +++ b/sanic/mixins/routes.py @@ -1,4 +1,3 @@ -from functools import partial from inspect import signature from pathlib import PurePath from typing import Set, Union diff --git a/sanic/router.py b/sanic/router.py index 3b667a63..f82460d4 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -1,5 +1,5 @@ from functools import lru_cache -from typing import FrozenSet, Iterable, List, Optional, Union +from typing import Iterable, List, Optional, Union from sanic_routing import BaseRouter from sanic_routing.exceptions import NoMethod @@ -44,17 +44,11 @@ class Router(BaseRouter): allowed_methods=e.allowed_methods, ) - # TODO: Implement response - # - args, - # - endpoint, - return ( handler, - (), params, route.path, route.name, - None, route.ctx.ignore_body, ) @@ -79,7 +73,7 @@ class Router(BaseRouter): uri: str, methods: Iterable[str], handler, - host: Optional[Union[str, FrozenSet[str]]] = None, + host: Optional[Union[str, Iterable[str]]] = None, strict_slashes: bool = False, stream: bool = False, ignore_body: bool = False, @@ -115,11 +109,6 @@ class Router(BaseRouter): :return: the route object :rtype: Route """ - # TODO: Implement - # - host - # - strict_slashes - # - ignore_body - # - stream if version is not None: version = str(version).strip("/").lstrip("v") uri = "/".join([f"/v{version}", uri.lstrip("/")]) @@ -136,7 +125,7 @@ class Router(BaseRouter): if isinstance(host, str): hosts = [host] else: - hosts = host or [None] + hosts = host or [None] # type: ignore routes = [] @@ -185,10 +174,6 @@ class Router(BaseRouter): if not view_name: return None - # TODO: - # - Check blueprint naming, we shouldn't need to double check here - # but it seems like blueprints are not receiving full names - # probably need tocheck the blueprint registration func route = self.name_index.get(view_name) if not route: full_name = self.ctx.app._generate_name(view_name) diff --git a/sanic/server.py b/sanic/server.py index 50c3e0be..3564f4fa 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -5,7 +5,6 @@ import secrets import socket import stat import sys -import time from asyncio import CancelledError from functools import partial diff --git a/sanic/static.py b/sanic/static.py index a89770d2..45cbc214 100644 --- a/sanic/static.py +++ b/sanic/static.py @@ -6,8 +6,6 @@ from re import sub from time import gmtime, strftime from urllib.parse import unquote -from sanic_routing.patterns import REGEX_TYPES - from sanic.compat import stat_async from sanic.exceptions import ( ContentRangeError, diff --git a/setup.py b/setup.py index c3f79166..d6a21dfb 100644 --- a/setup.py +++ b/setup.py @@ -83,6 +83,7 @@ ujson = "ujson>=1.35" + env_dependency uvloop = "uvloop>=0.5.3" + env_dependency requirements = [ + "sanic-routing", "httptools>=0.0.10", uvloop, ujson,