Skip to content

Commit

Permalink
ref: adjust typing so rate_limits can be a callable (#74807)
Browse files Browse the repository at this point in the history
this fixes an error in mypy 1.11 -- it also fixes an error in a
currently-ignored file which has a callable `rate_limits`

<!-- Describe your PR here. -->
  • Loading branch information
asottile-sentry authored Jul 24, 2024
1 parent 6429634 commit 815e565
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/sentry/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ class Endpoint(APIView):

owner: ApiOwner = ApiOwner.UNOWNED
publish_status: dict[HTTP_METHOD_NAME, ApiPublishStatus] = {}
rate_limits: RateLimitConfig | dict[
str, dict[RateLimitCategory, RateLimit]
rate_limits: RateLimitConfig | dict[str, dict[RateLimitCategory, RateLimit]] | Callable[
..., RateLimitConfig | dict[str, dict[RateLimitCategory, RateLimit]]
] = DEFAULT_RATE_LIMIT_CONFIG
enforce_rate_limit: bool = settings.SENTRY_RATELIMITER_ENABLED
snuba_methods: list[HTTP_METHOD_NAME] = []
Expand Down
3 changes: 2 additions & 1 deletion tests/sentry/middleware/test_ratelimit_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ class CallableRateLimitConfigEndpoint(Endpoint):
permission_classes = (AllowAny,)
enforce_rate_limit = True

def rate_limits(request):
@staticmethod
def rate_limits(*a, **k):
return {
"GET": {
RateLimitCategory.IP: RateLimit(limit=20, window=1),
Expand Down
28 changes: 0 additions & 28 deletions tests/sentry/ratelimits/utils/test_get_rate_limit_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,3 @@ class ChildEndpoint(ParentEndpoint):
assert get_rate_limit_value(
"GET", RateLimitCategory.IP, rate_limit_config
) == get_default_rate_limits_for_group("foo", RateLimitCategory.IP)

def test_multiple_inheritance(self):
class ParentEndpoint(Endpoint):
rate_limits: RateLimitConfig | dict[str, dict[RateLimitCategory, RateLimit]]
rate_limits = {"GET": {RateLimitCategory.IP: RateLimit(limit=100, window=5)}}

class Mixin:
rate_limits: RateLimitConfig | dict[str, dict[RateLimitCategory, RateLimit]]
rate_limits = {"GET": {RateLimitCategory.IP: RateLimit(limit=2, window=4)}}

class ChildEndpoint(ParentEndpoint, Mixin):
pass

_child_endpoint = ChildEndpoint.as_view()
rate_limit_config = get_rate_limit_config(_child_endpoint.view_class)

class ChildEndpointReverse(Mixin, ParentEndpoint):
pass

_child_endpoint_reverse = ChildEndpointReverse.as_view()
rate_limit_config_reverse = get_rate_limit_config(_child_endpoint_reverse.view_class)

assert get_rate_limit_value("GET", RateLimitCategory.IP, rate_limit_config) == RateLimit(
100, 5
)
assert get_rate_limit_value(
"GET", RateLimitCategory.IP, rate_limit_config_reverse
) == RateLimit(limit=2, window=4)

0 comments on commit 815e565

Please sign in to comment.