Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Pylint "Wrong hanging indentation" and PEP8 E121?

I am trying to properly indent following piece of code:

RULES_LIST = [
    ('Name1', 1, 'Long string upto 40 chars'),
    ('Name2', 2, 'Long string upto 40 chars'),
    ('Name3', 3, 'Long string upto 40 chars'),
    ('Name4', 4, 'Long string upto 40 chars'),
    ('Name5', 5, 'Long string upto 40 chars'),
    ('Name6', 6, 'Long string upto 40 chars'),
    ('Name7', 7, 'Long string upto 40 chars'),
    ('Name8', 8, 'Long string upto 40 chars')
]

Pylint complains Wrong hanging indentation. for above code, and PEP8 complains E121: under-indented for hanging indent.

A possible fix for pylint is changing it to:

RULES_LIST = [\
    ('Name1', 1, 'Long string upto 40 chars'),
     ...
    ('Name8', 8, 'Long string upto 40 chars')]

but PEP8 complains E121 and E502

PEP8: 1.5.7 (default configuration)
Pylint: 1.3.0 (default configuration)
Python: 2.7.5 (Running on OSX 10.9.3)

The list can grow longer. Can someone please suggest a proper indentation for this?

like image 430
Jatin Kumar Avatar asked Aug 14 '14 08:08

Jatin Kumar


2 Answers

If you want to continue using tabs, you can change the following settings in the .pylintrc file:

indent-string='\t'
indent-after-paren=1

If you only change the first one, pylint will expect four tabs to be used for indentation.

like image 163
James Brierley Avatar answered Nov 13 '22 04:11

James Brierley


You're using tabs instead of four spaces.

Those three possibilities are correct from pylint point of view, as long as you use four spaces instead of tabs, the first one is the one black would use:

RULES_LIST = [
    ('Name1', 1, 'Long string upto 40 chars'),
    ('Name2', 2, 'Long string upto 40 chars'),
    ('Name3', 3, 'Long string upto 40 chars'),
    ('Name4', 4, 'Long string upto 40 chars'),
    ('Name5', 5, 'Long string upto 40 chars'),
    ('Name6', 6, 'Long string upto 40 chars'),
    ('Name7', 7, 'Long string upto 40 chars'),
    ('Name8', 8, 'Long string upto 40 chars'),
]

RULES_LIST = [('Name1', 1, 'Long string upto 40 chars'),
              ('Name2', 2, 'Long string upto 40 chars'),
              ('Name3', 3, 'Long string upto 40 chars'),
              ('Name4', 4, 'Long string upto 40 chars'),
              ('Name5', 5, 'Long string upto 40 chars'),
              ('Name6', 6, 'Long string upto 40 chars'),
              ('Name7', 7, 'Long string upto 40 chars'),
              ('Name8', 8, 'Long string upto 40 chars')]

RULES_LIST = [
    ('Name1', 1, 'Long string upto 40 chars'),
    ('Name2', 2, 'Long string upto 40 chars'),
    ('Name3', 3, 'Long string upto 40 chars'),
    ('Name4', 4, 'Long string upto 40 chars'),
    ('Name5', 5, 'Long string upto 40 chars'),
    ('Name6', 6, 'Long string upto 40 chars'),
    ('Name7', 7, 'Long string upto 40 chars'),
    ('Name8', 8, 'Long string upto 40 chars')]

Note that, in the first one, there's a trailing coma, this is intended as it reduces differences when you add a line:

Adding a 9th line without trailing coma:

     ("Name7", 7, "Long string upto 40 chars"),
-    ("Name8", 8, "Long string upto 40 chars")
+    ("Name8", 8, "Long string upto 40 chars"),
+    ("Name9", 9, "Long string upto 40 chars")
 ]

Adding a 9th line with trailing coma:

     ("Name8", 8, "Long string upto 40 chars"),
+    ("Name9", 9, "Long string upto 40 chars"),
 ]
like image 27
Julien Palard Avatar answered Nov 13 '22 03:11

Julien Palard