Skip to content

Commit 18bac3c

Browse files
committed
fix: get rid of private method
1 parent 78d64c4 commit 18bac3c

File tree

2 files changed

+9
-27
lines changed

2 files changed

+9
-27
lines changed

app.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""A simple FastAPI app that serves a REST API for proselint."""
22

3-
from typing import TYPE_CHECKING, cast
4-
53
from fastapi import FastAPI, HTTPException, Request, Response, status
64
from fastapi.middleware.cors import CORSMiddleware
75
from slowapi import Limiter
@@ -13,9 +11,6 @@
1311
from proselint.registry import CheckRegistry
1412
from proselint.tools import LintFile, LintResult
1513

16-
if TYPE_CHECKING:
17-
from slowapi import ViewRateLimit
18-
1914

2015
def _lint(input_text: str) -> list[LintResult]:
2116
return LintFile(content=input_text, source="<api>").lint()
@@ -35,21 +30,20 @@ def _lint(input_text: str) -> list[LintResult]:
3530
CheckRegistry().register_many(__register__)
3631

3732

38-
# NOTE: We don't use slowapi's handler due to the error key name.
3933
@app.exception_handler(RateLimitExceeded)
4034
def rate_limit_exceeded_handler(
41-
request: Request,
35+
_: Request,
4236
exc: RateLimitExceeded,
4337
) -> Response:
4438
"""Middleware to handle exceeded ratelimits."""
45-
response = JSONResponse(
46-
{"detail": f"rate limit exceeded: {exc.detail}"},
39+
return JSONResponse(
40+
{
41+
"status": "error",
42+
"message": "rate limit exceeded",
43+
"limit": str(exc.detail),
44+
},
4745
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
48-
)
49-
50-
rate_limit = cast("ViewRateLimit", request.state.view_rate_limit)
51-
return limiter._inject_headers( # noqa: SLF001
52-
response, rate_limit
46+
headers=getattr(exc, "headers", None),
5347
)
5448

5549

typings/slowapi/__init__.pyi

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
from typing import Any, Callable, Protocol, TypeVar
1+
from typing import Any, Callable, TypeVar
22
from starlette.requests import Request
3-
from starlette.responses import Response
4-
5-
class ViewRateLimit(Protocol):
6-
limit: str
7-
reset: int
8-
remaining: int
93

104
F = TypeVar("F", bound=Callable[..., Any])
115
KeyFunc = Callable[[Request], str]
@@ -14,9 +8,3 @@ class Limiter:
148
def __init__(self, key_func: KeyFunc) -> None: ...
159

1610
def limit(self, limit: str) -> Callable[[F], F]: ...
17-
18-
def _inject_headers(
19-
self,
20-
response: Response,
21-
rate_limit: ViewRateLimit,
22-
) -> Response: ...

0 commit comments

Comments
 (0)