From db802405c89fe76dc58f765d0c5311a0afaa547e Mon Sep 17 00:00:00 2001 From: Reuben Thomas-Davis Date: Thu, 1 Oct 2020 20:04:03 +0100 Subject: [PATCH] :label: resolve mypy errors and add mypy_cache to gitignore --- .gitignore | 4 ++++ slowapi/extension.py | 15 ++++++++++++--- slowapi/middleware.py | 8 ++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index fcb129e..ad672a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,9 @@ +# testing .pytest_cache __pycache__ +# typing +.mypy_cache + # editors .idea diff --git a/slowapi/extension.py b/slowapi/extension.py index 02d1d95..2e6da1f 100644 --- a/slowapi/extension.py +++ b/slowapi/extension.py @@ -269,7 +269,7 @@ class Limiter: if not self._application_limits and app_limits: self._application_limits = [ LimitGroup( - app_limits, self._key_func, "global", False, None, None, None + app_limits, self._key_func, "global", False, None, None, None, False ) ] @@ -278,7 +278,9 @@ class Limiter: ) if not self._default_limits and conf_limits: self._default_limits = [ - LimitGroup(conf_limits, self._key_func, None, False, None, None, None) + LimitGroup( + conf_limits, self._key_func, None, False, None, None, None, False + ) ] fallback_enabled = self.get_app_config(C.IN_MEMORY_FALLBACK_ENABLED, False) fallback_limits: Optional[StrOrCallableStr] = self.get_app_config( @@ -287,7 +289,14 @@ class Limiter: if not self._in_memory_fallback and fallback_limits: self._in_memory_fallback = [ LimitGroup( - fallback_limits, self._key_func, None, False, None, None, None + fallback_limits, + self._key_func, + None, + False, + None, + None, + None, + False, ) ] if not self._in_memory_fallback_enabled: diff --git a/slowapi/middleware.py b/slowapi/middleware.py index 9ac4c35..f0747db 100644 --- a/slowapi/middleware.py +++ b/slowapi/middleware.py @@ -1,3 +1,5 @@ +from typing import Union + from starlette.applications import Starlette from starlette.middleware.base import ( BaseHTTPMiddleware, @@ -6,6 +8,7 @@ from starlette.middleware.base import ( ) from starlette.requests import Request from starlette.responses import Response +from starlette.routing import Route, BaseRoute, WebSocketRoute from slowapi import Limiter, _rate_limit_exceeded_handler @@ -19,8 +22,8 @@ class SlowAPIMiddleware(BaseHTTPMiddleware): handler = None for route in app.routes: match, _ = route.matches(request.scope) - if match.FULL: - handler = route.endpoint + if match.FULL and hasattr(route, "endpoint"): + handler = route.endpoint # type: ignore # if we can't find the route handler if handler is None: return await call_next(request) @@ -50,3 +53,4 @@ class SlowAPIMiddleware(BaseHTTPMiddleware): response = await call_next(request) response = limiter._inject_headers(response, request.state.view_rate_limit) return response + return await call_next(request)