I am kinda obsessed with formating my python code with flake8. However, I cannot find a good way to solve E501 (line too long x > 79 characters) in settings file of Django.
First it was like this (4xE501):
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
and then I came up with this:
AUTH_PASSWORD_VALIDATORS = [{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
}, {
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
}, {
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
}, {
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
But still 'NAME':django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
is too long. Is there a way to format this or should I ignore this one?
If you are obsessed with not getting this warning more than the actual looks of your code, then you can break a line of python code (without breaking it's continuity) by adding a \
character at the breaking point:
Examples:
# 1
from some_module import some_method, some_other_method, \
a_third_method
# 2
s = "A really very long string, which exist to mesh with your pep8" \
" warning free obsession. Well, not anymore!!!"
Attention: The \
character raises an error when the line you are going to split is inside {}, [] or ()
, so you may simply do:
AUTH_PASSWORD_VALIDATORS = [{
'NAME': 'django.contrib.auth.password_validation.'
'UserAttributeSimilarityValidator'
}, ...
which is not that ugly considering...
If you don't want the warning and you like your code as is, then you can add:
# nopep8
at the end of every line that you want to exempt from pep8 analysis.
As an alternative (the following rewrite passes PEP8):
[{"NAME": f"django.contrib.auth.password_validation.{name}"}
for name in [
"UserAttributeSimilarityValidator",
"MinimumLengthValidator",
"CommonPasswordValidator",
"NumericPasswordValidator"]]
In python 2 you can use {}".format(name)
rather than f""
.
Was looking at Coding style | Django docs and found this:
An exception to PEP 8 is our rules on line lengths. Don’t limit lines of code to 79 characters if it means the code looks significantly uglier or is harder to read. We allow up to 119 characters as this is the width of GitHub code review.
Even people at Django avoid it (they also prefer flake8 for PEP8 checking). So, it'll be better if you make a .flake8
or setup.cfg
file and type:
[flake8]
max-line-length = 119
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With