Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line is too long. Django PEP8

PEP8 info:

models.py:10:80: E501 line too long (83 > 79 characters)

Models.py:

field = TreeForeignKey('self', null=True, blank=True, related_name='abcdefgh')

How to correctly write this line?

like image 502
Angelina Avatar asked Jan 03 '13 16:01

Angelina


4 Answers

It's "correct", PEP8 just flags lines over 79 characters long. But if you're concerned about that, you could write it like this:

field = TreeForeignKey('self',
                       null=True,
                       blank=True,
                       related_name='abcdefgh')

Or this:

field = TreeForeignKey(
    'self',
    null=True,
    blank=True,
    related_name='abcdefgh',
)

Or, really, any other style that would break the single line into multiple shorter lines.

like image 154
mipadi Avatar answered Nov 12 '22 11:11

mipadi


I just found this neat program called autopep8! https://github.com/hhatto/autopep8

pip install autopep8
autopep8 -i models.py

You can also do (recursively):

autopep8 -ri package/

Auto PEP8 only makes safe changes to the files, only changing layout, not code logic.

like image 26
umeboshi Avatar answered Nov 12 '22 11:11

umeboshi


If you have some ridiculous long string that isn't very convenient to break into pieces (thinking about things like Sentry DSNs, the occasional module in MIDDLEWARE or INSTALLED_APPS), you can just put # noqa at the end of the line and the linters will ignore the line. Use sparingly thou and definitely not for the case you asked for.

like image 9
Árni St. Sigurðsson Avatar answered Nov 12 '22 11:11

Árni St. Sigurðsson


This is very subjective. I'd write, if I were following E501 strictly:

field = TreeForeignKey('self',
                       null=True,
                       blank=True,
                       related_name='abcdefgh')

I usually consider 100 too long, not 80.

like image 5
Pavel Anossov Avatar answered Nov 12 '22 13:11

Pavel Anossov