How to format Django setting file for flake8
Asked Answered
T

4

8

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?

Trifid answered 25/4, 2017 at 8:53 Comment(2)
You should ignore it. Obsessiveness over styling issues is, well.. just obsessive. (you could define a function that adds the prefix, but as I said.. obsessive :-)Wire
You can add the comment # nopep8 at the end of the long line to exclude it from pep8 checks - https://mcmap.net/q/202633/-how-to-disable-a-pep8-error-in-a-specific-fileWoodsman
S
6

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.

Smokeproof answered 25/4, 2017 at 10:19 Comment(3)
Happy to help mate :)Smokeproof
Your string reads pep8warning and not pep8 warning. I guess this shows why obsessiveness over styling isn't a good thing. ;)Spaceman
True that @Spaceman :D I have fixed it (obsessively)!Smokeproof
H
3

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

Heffner answered 10/5, 2017 at 4:54 Comment(1)
That's so DRY that it's living in the desert!Afferent
H
2

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
Hiding answered 8/8, 2017 at 17:24 Comment(0)
M
1

If you are using VS Code....

1) Create folder (.vscode) in your Project.

2) Create settings.json file in folder (.vscode) and paste this code

{
    "team.showWelcomeMessage": false,
    "editor.formatOnSave": true,
    "python.linting.pycodestyleEnabled": false,
    "python.linting.pylintPath": "C:Users/User/AppData/Roaming/Python/Python37/site-packages/pylint",
    "python.linting.pylintArgs": [
        "--load-plugins",
        "pylint_django"
    ],
    "python.linting.pylintEnabled": false,
    "python.linting.enabled": true
}

where "python.linting.pycodestyleEnabled": false, (do FALSE)

Mighty answered 28/4, 2020 at 20:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.