Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
How to format Django setting file for flake8

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?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

about 4 years ago · Santiago Trujillo Report

0

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"".

about 4 years ago · Santiago Trujillo Report

0

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
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!