Search code examples
pythondjangopep8flake8

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?


Solution

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