Skip to content

Commit

Permalink
Pylint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Nov 11, 2021
1 parent e93df22 commit a870b66
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 62 deletions.
4 changes: 2 additions & 2 deletions pylint_django/augmentations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@
VIEW_ATTRS = {
(
(
'{}.{}'.format(cls.__module__, cls.__name__),
'.{}'.format(cls.__name__)
f'{cls.__module__}.{cls.__name__}',
f'.{cls.__name__}',
),
tuple(cls.__dict__.keys())
) for cls in (
Expand Down
16 changes: 8 additions & 8 deletions pylint_django/checkers/auth_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class AuthUserChecker(checkers.BaseChecker):

name = 'auth-user-checker'

msgs = {'E%d41' % BASE_ID: ("Hard-coded 'auth.User'",
'hard-coded-auth-user',
"Don't hard-code the auth.User model. "
"Use settings.AUTH_USER_MODEL instead!"),
'E%d42' % BASE_ID: ("User model imported from django.contrib.auth.models",
'imported-auth-user',
"Don't import django.contrib.auth.models.User model. "
"Use django.contrib.auth.get_user_model() instead!")}
msgs = {f'E{BASE_ID}41': ("Hard-coded 'auth.User'",
'hard-coded-auth-user',
"Don't hard-code the auth.User model. "
"Use settings.AUTH_USER_MODEL instead!"),
f'E{BASE_ID}42': ("User model imported from django.contrib.auth.models",
'imported-auth-user',
"Don't import django.contrib.auth.models.User model. "
"Use django.contrib.auth.get_user_model() instead!")}

@utils.check_messages('hard-coded-auth-user')
def visit_const(self, node):
Expand Down
16 changes: 8 additions & 8 deletions pylint_django/checkers/django_installed.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ class DjangoInstalledChecker(BaseChecker):
name = 'django-installed-checker'

msgs = {
'F%s01' % BASE_ID: ("Django is not available on the PYTHONPATH",
'django-not-available',
"Django could not be imported by the pylint-django plugin, so most Django related "
"improvements to pylint will fail."),
f'F{BASE_ID}01': ("Django is not available on the PYTHONPATH",
'django-not-available',
"Django could not be imported by the pylint-django plugin, so most Django related "
"improvements to pylint will fail."),

'W%s99' % BASE_ID: ('Placeholder message to prevent disabling of checker',
'django-not-available-placeholder',
'PyLint does not recognise checkers as being enabled unless they have at least'
' one message which is not fatal...')
f'W{BASE_ID}99': ('Placeholder message to prevent disabling of checker',
'django-not-available-placeholder',
'PyLint does not recognise checkers as being enabled unless they have at least'
' one message which is not fatal...')
}

@check_messages('django-not-available')
Expand Down
6 changes: 2 additions & 4 deletions pylint_django/checkers/foreign_key_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ class ForeignKeyStringsChecker(BaseChecker):
)

msgs = {
"E%s10"
% BASE_ID: (
f"E{BASE_ID}10": (
"Django was not configured. For more information run "
"pylint --load-plugins=pylint_django --help-msg=django-not-configured",
"django-not-configured",
_LONG_MESSAGE,
),
"F%s10"
% BASE_ID: (
f"F{BASE_ID}10": (
"Provided Django settings %s could not be loaded",
"django-settings-module-not-found",
"The provided Django settings module %s was not found on the path",
Expand Down
8 changes: 4 additions & 4 deletions pylint_django/checkers/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class FormChecker(BaseChecker):

name = 'django-form-checker'
msgs = {
'W%d04' % BASE_ID: ("Use explicit fields instead of exclude in ModelForm",
'modelform-uses-exclude',
"Prevents accidentally allowing users to set fields, "
"especially when adding new fields to a Model")
f'W{BASE_ID}04': ("Use explicit fields instead of exclude in ModelForm",
'modelform-uses-exclude',
"Prevents accidentally allowing users to set fields, "
"especially when adding new fields to a Model")
}

@check_messages('modelform-uses-exclude')
Expand Down
22 changes: 11 additions & 11 deletions pylint_django/checkers/json_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ class JsonResponseChecker(checkers.BaseChecker):

# configuration section name
name = 'json-response-checker'
msgs = {'R%s01' % BASE_ID: ("Instead of HttpResponse(json.dumps(data)) use JsonResponse(data)",
'http-response-with-json-dumps',
'Used when json.dumps() is used as an argument to HttpResponse().'),
'R%s02' % BASE_ID: ("Instead of HttpResponse(content_type='application/json') use JsonResponse()",
'http-response-with-content-type-json',
'Used when HttpResponse() is returning application/json.'),
'R%s03' % BASE_ID: ("Redundant content_type parameter for JsonResponse()",
'redundant-content-type-for-json-response',
'Used when JsonResponse() contains content_type parameter. '
'This is either redundant or the content_type is not JSON '
'which is probably an error.')}
msgs = {f'R{BASE_ID}01': ("Instead of HttpResponse(json.dumps(data)) use JsonResponse(data)",
'http-response-with-json-dumps',
'Used when json.dumps() is used as an argument to HttpResponse().'),
f'R{BASE_ID}02': ("Instead of HttpResponse(content_type='application/json') use JsonResponse()",
'http-response-with-content-type-json',
'Used when HttpResponse() is returning application/json.'),
f'R{BASE_ID}03': ("Redundant content_type parameter for JsonResponse()",
'redundant-content-type-for-json-response',
'Used when JsonResponse() contains content_type parameter. '
'This is either redundant or the content_type is not JSON '
'which is probably an error.')}

@utils.check_messages('http-response-with-json-dumps',
'http-response-with-content-type-json',
Expand Down
16 changes: 8 additions & 8 deletions pylint_django/checkers/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ class NewDbFieldWithDefaultChecker(checkers.BaseChecker):

# configuration section name
name = 'new-db-field-with-default'
msgs = {'W%s98' % BASE_ID: ("%s AddField with default value",
'new-db-field-with-default',
'Used when Pylint detects migrations adding new '
'fields with a default value.')}
msgs = {f'W{BASE_ID}98': ("%s AddField with default value",
'new-db-field-with-default',
'Used when Pylint detects migrations adding new '
'fields with a default value.')}

_migration_modules = []
_possible_offences = {}
Expand Down Expand Up @@ -117,10 +117,10 @@ class MissingBackwardsMigrationChecker(checkers.BaseChecker):

name = 'missing-backwards-migration-callable'

msgs = {'W%s97' % BASE_ID: ('Always include backwards migration callable',
'missing-backwards-migration-callable',
'Always include a backwards/reverse callable counterpart'
' so that the migration is not irreversable.')}
msgs = {f'W{BASE_ID}97': ('Always include backwards migration callable',
'missing-backwards-migration-callable',
'Always include a backwards/reverse callable counterpart'
' so that the migration is not irreversable.')}

@utils.check_messages('missing-backwards-migration-callable')
def visit_call(self, node):
Expand Down
28 changes: 14 additions & 14 deletions pylint_django/checkers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@


MESSAGES = {
'E%d01' % BASE_ID: ("__unicode__ on a model must be callable (%s)",
'model-unicode-not-callable',
"Django models require a callable __unicode__ method"),
'W%d01' % BASE_ID: ("No __unicode__ method on model (%s)",
'model-missing-unicode',
"Django models should implement a __unicode__ method for string representation"),
'W%d02' % BASE_ID: ("Found __unicode__ method on model (%s). Python3 uses __str__.",
'model-has-unicode',
"Django models should not implement a __unicode__ "
"method for string representation when using Python3"),
'W%d03' % BASE_ID: ("Model does not explicitly define __unicode__ (%s)",
'model-no-explicit-unicode',
"Django models should implement a __unicode__ method for string representation. "
"A parent class of this model does, but ideally all models should be explicit.")
f'E{BASE_ID}01': ("__unicode__ on a model must be callable (%s)",
'model-unicode-not-callable',
"Django models require a callable __unicode__ method"),
f'W{BASE_ID}01': ("No __unicode__ method on model (%s)",
'model-missing-unicode',
"Django models should implement a __unicode__ method for string representation"),
f'W{BASE_ID}02': ("Found __unicode__ method on model (%s). Python3 uses __str__.",
'model-has-unicode',
"Django models should not implement a __unicode__ "
"method for string representation when using Python3"),
f'W{BASE_ID}03': ("Model does not explicitly define __unicode__ (%s)",
'model-no-explicit-unicode',
"Django models should implement a __unicode__ method for string representation. "
"A parent class of this model does, but ideally all models should be explicit.")
}


Expand Down
5 changes: 3 additions & 2 deletions pylint_django/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def fake_module_builder():
http://pylint.pycqa.org/projects/astroid/en/latest/extending.html?highlight=MANAGER#module-extender-transforms
"""
transforms_dir = os.path.join(os.path.dirname(__file__), 'transforms')
fake_module_path = os.path.join(transforms_dir, '%s.py' % re.sub(r'\.', '_', package_name))
transformed_name = re.sub(r'\.', '_', package_name)
fake_module_path = os.path.join(transforms_dir, f'{transformed_name}.py')

with open(fake_module_path) as modulefile:
with open(fake_module_path, encoding='utf-8') as modulefile:
fake_module = modulefile.read()

return astroid.builder.AstroidBuilder(astroid.MANAGER).string_build(fake_module)
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"""
from setuptools import setup, find_packages

LONG_DESCRIPTION = open('README.rst').read() + "\n" + open('CHANGELOG.rst').read()
with open('README.rst', encoding='utf-8') as readme, \
open('CHANGELOG.rst', encoding='utf-8') as changelog:
LONG_DESCRIPTION = readme.read() + "\n" + changelog.read()

setup(
name='pylint-django',
Expand Down

0 comments on commit a870b66

Please sign in to comment.