Skip to content

Commit

Permalink
Fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alisaifee committed Nov 27, 2021
1 parent 744c074 commit 06dddb5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 37 deletions.
29 changes: 10 additions & 19 deletions flask_limiter/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,19 +377,17 @@ def limiter(self):

def __check_conditional_deductions(self, response):

for lim, args in getattr(g,
'%s_conditional_deductions'
% self._key_prefix, {}).items():
for lim, args in getattr(
g, "%s_conditional_deductions" % self._key_prefix, {}
).items():
if lim.deduct_when(response):
self.limiter.hit(lim.limit, *args)

return response

def __inject_headers(self, response):
self.__check_conditional_deductions(response)
current_limit = getattr(g,
'%s_view_rate_limit' % self._key_prefix,
None)
current_limit = getattr(g, "%s_view_rate_limit" % self._key_prefix, None)
if self.enabled and self._headers_enabled and current_limit:
try:
window_stats = self.limiter.get_window_stats(*current_limit)
Expand Down Expand Up @@ -446,9 +444,7 @@ def __evaluate_limits(self, endpoint, limits):
failed_limit = None
limit_for_header = None
limits_escape = []
if not getattr(g,
"%s_conditional_deductions" % self._key_prefix,
None):
if not getattr(g, "%s_conditional_deductions" % self._key_prefix, None):
setattr(g, "%s_conditional_deductions" % self._key_prefix, {})

for lim in limits:
Expand All @@ -471,9 +467,7 @@ def __evaluate_limits(self, endpoint, limits):
args = [self._key_prefix] + args

if lim.deduct_when:
getattr(g,
"%s_conditional_deductions" % self._key_prefix
)[lim] = args
getattr(g, "%s_conditional_deductions" % self._key_prefix)[lim] = args
method = self.limiter.test
else:
method = self.limiter.hit
Expand Down Expand Up @@ -703,16 +697,13 @@ def _inner(obj):

@wraps(obj)
def __inner(*a, **k):
if (
self._auto_check
and not
g.get("%s_rate_limiting_complete" % self._key_prefix)
if self._auto_check and not g.get(
"%s_rate_limiting_complete" % self._key_prefix
):
self.__check_request_limit(False)
setattr(g,
"%s_rate_limiting_complete" % self._key_prefix,
True)
setattr(g, "%s_rate_limiting_complete" % self._key_prefix, True)
return obj(*a, **k)

return __inner

return _inner
Expand Down
22 changes: 4 additions & 18 deletions tests/test_flask_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,15 +586,9 @@ def app3_test():

def test_second_instance_bypassed_by_shared_g():
app = Flask(__name__)
limiter1 = Limiter(
app,
key_func=get_remote_address
)
limiter1 = Limiter(app, key_func=get_remote_address)

limiter2 = Limiter(
app,
key_func=get_remote_address
)
limiter2 = Limiter(app, key_func=get_remote_address)

@app.route("/test1")
@limiter2.limit("1/second")
Expand Down Expand Up @@ -627,17 +621,9 @@ def app_test2():

def test_independent_instances_by_key_prefix():
app = Flask(__name__)
limiter1 = Limiter(
app,
key_prefix="lmt1",
key_func=get_remote_address
)
limiter1 = Limiter(app, key_prefix="lmt1", key_func=get_remote_address)

limiter2 = Limiter(
app,
key_prefix="lmt2",
key_func=get_remote_address
)
limiter2 = Limiter(app, key_prefix="lmt2", key_func=get_remote_address)

@app.route("/test1")
@limiter2.limit("1/second")
Expand Down

0 comments on commit 06dddb5

Please sign in to comment.