PEP8 E128:无法弄清楚为什么标记一行

PEP8 E128: can't figure out why a line is being flagged

我正在使用具有内置 PyLint 功能的 Sublime + Anaconda

我不明白为什么下面的块中有 pars_f_name) 行:

            else:
                # Get parameters file name from path.
                pars_f_name = pars_f_path.split('/')[-1]
                print ("  WARNING: Unknown '{}' ID found in line {}\n"
                       "  of '{}' file.\n").format(reader[0], l + 1,
                       pars_f_name)

# Pack params in lists.
pl_params = [flag_make_plot, plot_frmt, plot_dpi]

被标记为:

[W] PEP 8 (E128): continuation line under-indented for visual indent

我已经尝试了所有我能想到的缩进(按照建议 here),但 Anaconda 一直将该行标记为 PEP8 E128 警告。

我做错了什么?

您需要进一步缩进 str.format() 个参数:

print ("  WARNING: Unknown '{}' ID found in line {}\n"
       "  of '{}' file.\n").format(reader[0], l + 1,
                                   pars_f_name)
#                                  ^^^^^^^^^^^^

作为个人选择,我会将这些参数全部放在一行中,缩进:

print ("  WARNING: Unknown '{}' ID found in line {}\n"
       "  of '{}' file.\n").format(
           reader[0], l + 1, pars_f_name)

这叫做hanging indent

参见 PEP 8 的 Indentation section;这些注意事项递归地应用于每个嵌套调用表达式。

替换:

print ("  WARNING: Unknown '{}' ID found in line {}\n"
                   "  of '{}' file.\n").format(reader[0], l + 1,
                   pars_f_name)

与:

print(
    "  WARNING: Unknown '{}' ID found in line {}\n"
    "  of '{}' file.\n".format(
        reader[0], l + 1, pars_f_name
    )
)

基本上是抱怨 str.format() 部分没有缩进;但是按照上面的例子格式化你的代码可以让它更具可读性!

更新: 这在 PEP8 中称为 "hanging indent"(参见:@MartijnPieters 的回复)这个回复更多的是 "here's how to fix it and make it readable at the same time"。 (不幸的是,对此有很多相互竞争的主观意见!