如何修复Pylint"错误的悬挂缩进"和PEP8 E121?

Jat*_*mar 27 python pep8 pylint indentation

我试图正确缩进下面的代码:

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')
]
Run Code Online (Sandbox Code Playgroud)

pylint的抱怨Wrong hanging indentation.对于上面的代码,并PEP8抱怨E121: under-indented for hanging indent.

针对pylint的可能修复方法是将其更改为:

RULES_LIST = [\
    ('Name1', 1, 'Long string upto 40 chars'),
     ...
    ('Name8', 8, 'Long string upto 40 chars')]
Run Code Online (Sandbox Code Playgroud)

但是PEP8抱怨道 E121 and E502

PEP8:1.5.7(默认配置)
Pylint:1.3.0(默认配置)
Python:2.7.5(在OSX 10.9.3上运行)

该列表可以变长.有人可以为此建议适当的缩进吗?

Jul*_*ard 10

您使用制表符而不是四个空格.

如果您使用四个空格而不是制表符,那么这三种可能性是正确的:

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')
]
Run Code Online (Sandbox Code Playgroud)


Jam*_*ley 9

如果要继续使用选项卡,可以在.pylintrc文件中更改以下设置:

indent-string='\t'
indent-after-paren=1
Run Code Online (Sandbox Code Playgroud)

如果您只更改第一个,则pylint将需要四个选项卡用于缩进.


idw*_*ker -3

本来期待的也许是这样

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')
    ]
Run Code Online (Sandbox Code Playgroud)

右方括号。