From 06dddb502552dc081b53ea9a0061ba30a06aafcf Mon Sep 17 00:00:00 2001 From: Ali-Akber Saifee Date: Sat, 27 Nov 2021 09:25:15 -0800 Subject: [PATCH] Fix linting errors --- flask_limiter/extension.py | 29 ++++++++++------------------- tests/test_flask_ext.py | 22 ++++------------------ 2 files changed, 14 insertions(+), 37 deletions(-) diff --git a/flask_limiter/extension.py b/flask_limiter/extension.py index ad2f287a..8dceaff4 100644 --- a/flask_limiter/extension.py +++ b/flask_limiter/extension.py @@ -377,9 +377,9 @@ 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) @@ -387,9 +387,7 @@ def __check_conditional_deductions(self, 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) @@ -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: @@ -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 @@ -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 diff --git a/tests/test_flask_ext.py b/tests/test_flask_ext.py index 882a78a4..176a097a 100644 --- a/tests/test_flask_ext.py +++ b/tests/test_flask_ext.py @@ -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") @@ -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")