Skip to content

Commit 6fbd592

Browse files
authored
Merge pull request #471 from alisaifee/public-limit-definition
Expose Limit dataclasses in public API
2 parents af85cf6 + 93b7cbb commit 6fbd592

File tree

13 files changed

+835
-422
lines changed

13 files changed

+835
-422
lines changed

doc/source/api.rst

+16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ Extension
77
---------
88
.. autoclass:: Limiter
99

10+
Limit objects
11+
--------------
12+
13+
The following dataclasses can be used to define rate limits with more
14+
granularity than what is available through the :class:`Limiter` constructor
15+
if needed (especially for **default**, **application wide** and **meta** limits).
16+
17+
.. autoclass:: Limit
18+
.. autoclass:: ApplicationLimit
19+
.. autoclass:: MetaLimit
20+
21+
For consistency the :class:`RouteLimit` dataclass is also available to define limits
22+
for decorating routes or blueprints.
23+
24+
.. autoclass:: RouteLimit
25+
1026
Utilities
1127
---------
1228
.. autoclass:: ExemptionScope

flask_limiter/__init__.py

+10
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@
66
from .constants import ExemptionScope, HeaderNames
77
from .errors import RateLimitExceeded
88
from .extension import Limiter, RequestLimit
9+
from .limits import (
10+
ApplicationLimit,
11+
Limit,
12+
MetaLimit,
13+
RouteLimit,
14+
)
915

1016
__all__ = [
1117
"ExemptionScope",
1218
"HeaderNames",
1319
"Limiter",
20+
"Limit",
21+
"RouteLimit",
22+
"ApplicationLimit",
23+
"MetaLimit",
1424
"RateLimitExceeded",
1525
"RequestLimit",
1626
]

flask_limiter/commands.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
from flask_limiter import Limiter
2525
from flask_limiter.constants import ConfigVars, ExemptionScope, HeaderNames
26+
from flask_limiter.limits import RuntimeLimit
2627
from flask_limiter.typing import Callable, Generator, cast
2728
from flask_limiter.util import get_qualified_name
28-
from flask_limiter.wrappers import Limit
2929

3030
limiter_theme = Theme(
3131
{
@@ -71,7 +71,7 @@ def render_strategy(strategy: RateLimiter) -> str:
7171

7272

7373
def render_limit_state(
74-
limiter: Limiter, endpoint: str, limit: Limit, key: str, method: str
74+
limiter: Limiter, endpoint: str, limit: RuntimeLimit, key: str, method: str
7575
) -> str:
7676
args = [key, limit.scope_for(endpoint, method)]
7777
if not limiter.storage or (limiter.storage and not limiter.storage.check()):
@@ -86,7 +86,7 @@ def render_limit_state(
8686
return f": [success]Pass[/success] ({stats[1]} out of {limit.limit.amount} remaining)"
8787

8888

89-
def render_limit(limit: Limit, simple: bool = True) -> str:
89+
def render_limit(limit: RuntimeLimit, simple: bool = True) -> str:
9090
render = str(limit.limit)
9191
if simple:
9292
return render
@@ -103,7 +103,7 @@ def render_limit(limit: Limit, simple: bool = True) -> str:
103103
def render_limits(
104104
app: Flask,
105105
limiter: Limiter,
106-
limits: tuple[list[Limit], ...],
106+
limits: tuple[list[RuntimeLimit], ...],
107107
endpoint: str | None = None,
108108
blueprint: str | None = None,
109109
rule: Rule | None = None,
@@ -511,7 +511,7 @@ def clear(
511511

512512
class Details(TypedDict):
513513
rule: Rule
514-
limits: tuple[list[Limit], ...]
514+
limits: tuple[list[RuntimeLimit], ...]
515515

516516
rule_limits: dict[str, Details] = {}
517517
for rule in sorted(

flask_limiter/errors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
from flask.wrappers import Response
66
from werkzeug import exceptions
77

8-
from .wrappers import Limit
8+
from .limits import RuntimeLimit
99

1010

1111
class RateLimitExceeded(exceptions.TooManyRequests):
1212
"""Exception raised when a rate limit is hit."""
1313

14-
def __init__(self, limit: Limit, response: Response | None = None) -> None:
14+
def __init__(self, limit: RuntimeLimit, response: Response | None = None) -> None:
1515
"""
1616
:param limit: The actual rate limit that was hit.
1717
Used to construct the default response message

0 commit comments

Comments
 (0)