Repository: jazzband/django-axes

Issue:
Add code linting to CI pipeline
Check and possibly fix PEP8 compliance in the process.


Discussion:
FYI using flake8 in a separate tox environment has been successful in other Jazzband projects.

Resolving patch:
diff --git a/axes/management/commands/axes_list_attempts.py b/axes/management/commands/axes_list_attempts.py
--- a/axes/management/commands/axes_list_attempts.py
+++ b/axes/management/commands/axes_list_attempts.py
@@ -9,7 +9,7 @@ class Command(BaseCommand):
     args = ''
     help = ('List registered login attempts')
 
-    def handle(self, *args, **kwargs):
+    def handle(self, *args, **kwargs):  # pylint: disable=unused-argument
         for obj in AccessAttempt.objects.all():
             self.stdout.write('{ip}\t{username}\t{failures}'.format(
                 ip=obj.ip_address,
diff --git a/axes/models.py b/axes/models.py
--- a/axes/models.py
+++ b/axes/models.py
@@ -42,7 +42,7 @@ class CommonAccess(models.Model):
         auto_now_add=True,
     )
 
-    class Meta:
+    class Meta(object):
         app_label = 'axes'
         abstract = True
         ordering = ['-attempt_time']
diff --git a/axes/signals.py b/axes/signals.py
--- a/axes/signals.py
+++ b/axes/signals.py
@@ -29,7 +29,7 @@
 
 
 @receiver(user_login_failed)
-def log_user_login_failed(sender, credentials, request, **kwargs):
+def log_user_login_failed(sender, credentials, request, **kwargs):  # pylint: disable=unused-argument
     """ Create an AccessAttempt record if the login wasn't successful
     """
     if request is None or settings.AXES_USERNAME_FORM_FIELD not in credentials:
@@ -123,7 +123,7 @@ def log_user_login_failed(sender, credentials, request, **kwargs):
 
 
 @receiver(user_logged_in)
-def log_user_logged_in(sender, request, user, **kwargs):
+def log_user_logged_in(sender, request, user, **kwargs):  # pylint: disable=unused-argument
     """ When a user logs in, update the access log
     """
     username = user.get_username()
@@ -148,7 +148,7 @@ def log_user_logged_in(sender, request, user, **kwargs):
 
 
 @receiver(user_logged_out)
-def log_user_logged_out(sender, request, user, **kwargs):
+def log_user_logged_out(sender, request, user, **kwargs):  # pylint: disable=unused-argument
     """ When a user logs out, update the access log
     """
     log.info('AXES: Successful logout by %s.', user)
@@ -161,7 +161,7 @@ def log_user_logged_out(sender, request, user, **kwargs):
 
 
 @receiver(post_save, sender=AccessAttempt)
-def update_cache_after_save(instance, **kwargs):
+def update_cache_after_save(instance, **kwargs):  # pylint: disable=unused-argument
     cache_hash_key = get_cache_key(instance)
     if not get_axes_cache().get(cache_hash_key):
         cache_timeout = get_cache_timeout()
@@ -169,6 +169,6 @@ def update_cache_after_save(instance, **kwargs):
 
 
 @receiver(post_delete, sender=AccessAttempt)
-def delete_cache_after_delete(instance, **kwargs):
+def delete_cache_after_delete(instance, **kwargs):  # pylint: disable=unused-argument
     cache_hash_key = get_cache_key(instance)
     get_axes_cache().delete(cache_hash_key)


Test patch:
diff --git a/axes/test_settings_cache.py b/axes/test_settings_cache.py
--- a/axes/test_settings_cache.py
+++ b/axes/test_settings_cache.py
@@ -1,6 +1,6 @@
 from __future__ import unicode_literals
 
-from .test_settings import *
+from .test_settings import *  # pylint: disable=unused-wildcard-import
 
 AXES_CACHE = 'axes'
 
diff --git a/axes/tests/compatibility.py b/axes/tests/compatibility.py
--- a/axes/tests/compatibility.py
+++ b/axes/tests/compatibility.py
@@ -1,6 +1,6 @@
 from __future__ import unicode_literals
 
 try:
-    from unittest.mock import patch
+    from unittest.mock import patch  # pylint: disable=unused-import
 except ImportError:
-    from mock import patch
+    from mock import patch  # pylint: disable=unused-import
diff --git a/axes/tests/test_access_attempt.py b/axes/tests/test_access_attempt.py
--- a/axes/tests/test_access_attempt.py
+++ b/axes/tests/test_access_attempt.py
@@ -86,7 +86,7 @@ def test_failure_limit_once(self):
         than failure limit
         """
         # test until one try before the limit
-        for i in range(1, settings.AXES_FAILURE_LIMIT):
+        for _ in range(1, settings.AXES_FAILURE_LIMIT):
             response = self._login()
             # Check if we are in the same login page
             self.assertContains(response, self.LOGIN_FORM_KEY)
@@ -100,14 +100,14 @@ def test_failure_limit_many(self):
         """Tests the login lock trying to login a lot of times more
         than failure limit
         """
-        for i in range(1, settings.AXES_FAILURE_LIMIT):
+        for _ in range(1, settings.AXES_FAILURE_LIMIT):
             response = self._login()
             # Check if we are in the same login page
             self.assertContains(response, self.LOGIN_FORM_KEY)
 
         # So, we shouldn't have gotten a lock-out yet.
         # We should get a locked message each time we try again
-        for i in range(0, random.randrange(1, settings.AXES_FAILURE_LIMIT)):
+        for _ in range(random.randrange(1, settings.AXES_FAILURE_LIMIT)):
             response = self._login()
             self.assertContains(response, self.LOCKED_MESSAGE, status_code=403)
 
@@ -121,10 +121,10 @@ def test_valid_logout(self):
         """Tests a valid logout and make sure the logout_time is updated
         """
         response = self._login(is_valid_username=True, is_valid_password=True)
-        self.assertEquals(AccessLog.objects.latest('id').logout_time, None)
+        self.assertEqual(AccessLog.objects.latest('id').logout_time, None)
 
         response = self.client.get(reverse('admin:logout'))
-        self.assertNotEquals(AccessLog.objects.latest('id').logout_time, None)
+        self.assertNotEqual(AccessLog.objects.latest('id').logout_time, None)
         self.assertContains(response, 'Logged out')
 
     def test_cooling_off(self):
@@ -162,7 +162,7 @@ def test_long_user_agent_not_valid(self):
         """Tests if can handle a long user agent with failure
         """
         long_user_agent = 'ie6' * 1024
-        for i in range(0, settings.AXES_FAILURE_LIMIT + 1):
+        for _ in range(settings.AXES_FAILURE_LIMIT + 1):
             response = self._login(user_agent=long_user_agent)
 
         self.assertContains(response, self.LOCKED_MESSAGE, status_code=403)
@@ -192,7 +192,7 @@ def test_reset_all(self):
         self.test_valid_login()
 
     @patch('axes.utils.get_client_ip', return_value='127.0.0.1')
-    def test_get_cache_key(self, get_ip_mock):
+    def test_get_cache_key(self, _):
         """ Test the cache key format"""
         # Getting cache key from request
         ip_address = '127.0.0.1'
@@ -230,7 +230,7 @@ class Scope(object): pass
         scope = Scope()
         scope.signal_received = 0
 
-        def signal_handler(request, username, ip_address, *args, **kwargs):
+        def signal_handler(request, username, ip_address, *args, **kwargs):  # pylint: disable=unused-argument
             scope.signal_received += 1
             self.assertIsNotNone(request)
 
@@ -239,13 +239,13 @@ def signal_handler(request, username, ip_address, *args, **kwargs):
 
         # Make a lockout
         self.test_failure_limit_once()
-        self.assertEquals(scope.signal_received, 1)
+        self.assertEqual(scope.signal_received, 1)
 
         reset()
 
         # Make another lockout
         self.test_failure_limit_once()
-        self.assertEquals(scope.signal_received, 2)
+        self.assertEqual(scope.signal_received, 2)
 
     @override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
     def test_lockout_by_combination_user_and_ip(self):
@@ -253,7 +253,7 @@ def test_lockout_by_combination_user_and_ip(self):
         when AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP is True
         """
         # test until one try before the limit
-        for i in range(1, settings.AXES_FAILURE_LIMIT):
+        for _ in range(1, settings.AXES_FAILURE_LIMIT):
             response = self._login(
                 is_valid_username=True,
                 is_valid_password=False,
@@ -272,7 +272,7 @@ def test_lockout_by_user_only(self):
         when AXES_ONLY_USER_FAILURES is True
         """
         # test until one try before the limit
-        for i in range(1, settings.AXES_FAILURE_LIMIT):
+        for _ in range(1, settings.AXES_FAILURE_LIMIT):
             response = self._login(
                 is_valid_username=True,
                 is_valid_password=False,
@@ -297,7 +297,7 @@ def test_lockout_by_user_only(self):
 
         # now create failure_limit + 1 failed logins and then we should still
         # be able to login with valid_username
-        for i in range(1, settings.AXES_FAILURE_LIMIT + 1):
+        for _ in range(settings.AXES_FAILURE_LIMIT):
             response = self._login(
                 is_valid_username=False,
                 is_valid_password=False,
@@ -313,7 +313,7 @@ def test_log_data_truncated(self):
         # An impossibly large post dict
         extra_data = {string.ascii_letters * x: x for x in range(0, 1000)}
         self._login(**extra_data)
-        self.assertEquals(
+        self.assertEqual(
             len(AccessAttempt.objects.latest('id').post_data), 1024
         )
 
@@ -322,8 +322,8 @@ def test_json_response(self):
         """
         self.test_failure_limit_once()
         response = self._login(is_json=True)
-        self.assertEquals(response.status_code, 403)
-        self.assertEquals(response.get('Content-Type'), 'application/json')
+        self.assertEqual(response.status_code, 403)
+        self.assertEqual(response.get('Content-Type'), 'application/json')
 
     @override_settings(AXES_DISABLE_SUCCESS_ACCESS_LOG=True)
     def test_valid_logout_without_success_log(self):
@@ -332,7 +332,7 @@ def test_valid_logout_without_success_log(self):
         response = self._login(is_valid_username=True, is_valid_password=True)
         response = self.client.get(reverse('admin:logout'))
 
-        self.assertEquals(AccessLog.objects.all().count(), 0)
+        self.assertEqual(AccessLog.objects.all().count(), 0)
         self.assertContains(response, 'Logged out')
 
     @override_settings(AXES_DISABLE_SUCCESS_ACCESS_LOG=True)
@@ -355,7 +355,7 @@ def test_valid_logout_without_log(self):
         response = self._login(is_valid_username=True, is_valid_password=True)
         response = self.client.get(reverse('admin:logout'))
 
-        self.assertEquals(AccessLog.objects.first().logout_time, None)
+        self.assertEqual(AccessLog.objects.first().logout_time, None)
         self.assertContains(response, 'Logged out')
 
     @override_settings(AXES_DISABLE_ACCESS_LOG=True)
@@ -367,9 +367,9 @@ def test_non_valid_login_without_log(self):
         AccessLog.objects.all().delete()
 
         response = self._login(is_valid_username=True, is_valid_password=False)
-        self.assertEquals(response.status_code, 200)
+        self.assertEqual(response.status_code, 200)
 
-        self.assertEquals(AccessLog.objects.all().count(), 0)
+        self.assertEqual(AccessLog.objects.all().count(), 0)
 
     @override_settings(AXES_DISABLE_ACCESS_LOG=True)
     def test_check_is_not_made_on_GET(self):
diff --git a/axes/tests/test_access_attempt_config.py b/axes/tests/test_access_attempt_config.py
--- a/axes/tests/test_access_attempt_config.py
+++ b/axes/tests/test_access_attempt_config.py
@@ -54,7 +54,7 @@ def _login(self, username, password, ip_addr='127.0.0.1',
         return response
 
     def _lockout_user_from_ip(self, username, ip_addr):
-        for i in range(1, settings.AXES_FAILURE_LIMIT + 1):
+        for _ in range(settings.AXES_FAILURE_LIMIT):
             response = self._login(
                 username=username,
                 password=self.WRONG_PASSWORD,
@@ -187,9 +187,7 @@ def test_lockout_by_user_allows_when_diff_user_diff_ip_without_cache(self):
         self.assertEqual(response.status_code, self.ALLOWED)
 
     @override_settings(AXES_ONLY_USER_FAILURES=True)
-    def test_lockout_by_user_with_empty_username_allows_other_users_without_cache(
-        self, cache_get_mock=None, cache_set_mock=None
-    ):
+    def test_lockout_by_user_with_empty_username_allows_other_users_without_cache(self):
         # User with empty username is locked out from IP 1.
         self._lockout_user_from_ip(username='', ip_addr=self.IP_1)
 
@@ -252,9 +250,7 @@ def test_lockout_by_user_and_ip_allows_when_diff_user_diff_ip_without_cache(self
         self.assertEqual(response.status_code, self.ALLOWED)
 
     @override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
-    def test_lockout_by_user_and_ip_with_empty_username_allows_other_users_without_cache(
-        self, cache_get_mock=None, cache_set_mock=None
-    ):
+    def test_lockout_by_user_and_ip_with_empty_username_allows_other_users_without_cache(self):
         # User with empty username is locked out from IP 1.
         self._lockout_user_from_ip(username='', ip_addr=self.IP_1)
 
@@ -313,9 +309,7 @@ def test_lockout_by_ip_allows_when_diff_user_diff_ip_using_cache(self):
         self.assertEqual(response.status_code, self.ALLOWED)
 
     @override_settings(AXES_ONLY_USER_FAILURES=True)
-    def test_lockout_by_user_with_empty_username_allows_other_users_using_cache(
-        self, cache_get_mock=None, cache_set_mock=None
-    ):
+    def test_lockout_by_user_with_empty_username_allows_other_users_using_cache(self):
         # User with empty username is locked out from IP 1.
         self._lockout_user_from_ip(username='', ip_addr=self.IP_1)
 
@@ -432,9 +426,7 @@ def test_lockout_by_user_and_ip_allows_when_diff_user_diff_ip_using_cache(self):
         self.assertEqual(response.status_code, self.ALLOWED)
 
     @override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
-    def test_lockout_by_user_and_ip_with_empty_username_allows_other_users_using_cache(
-        self, cache_get_mock=None, cache_set_mock=None
-    ):
+    def test_lockout_by_user_and_ip_with_empty_username_allows_other_users_using_cache(self):
         # User with empty username is locked out from IP 1.
         self._lockout_user_from_ip(username='', ip_addr=self.IP_1)