4

I'm using Sublime + Anaconda which has a built-in PyLint feature.

I can't figure out why the pars_f_name) line in the following block:

            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]

is being flagged as:

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

I've tried every indentation I could think of (as suggested here) but Anaconda keeps flagging that line as a PEP8 E128 warning.

What am I doing wrong here?

Community
  • 1
  • 1
Gabriel
  • 40,504
  • 73
  • 230
  • 404

2 Answers2

8

You need to further indent the str.format() arguments:

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

As a personal choice, I'd put those arguments all on one line, indented:

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

This is called a hanging indent.

See the Indentation section of PEP 8; these considerations apply recursively to each nested call expression.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

Replace:

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

with:

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

It's basically complaining about the lack of indentation for the str.format() part; but formatting your code as per the above example makes it far more readable!

Update: This is called "hanging indent" in PEP8 (See: @MartijnPieters's response) This response is more of "here's how to fix it and make it readable at the same time". (unfortunately there are many competing subjective opinions on this though!)

James Mills
  • 18,669
  • 3
  • 49
  • 62
  • Not sure why this answer got downvoted. I prefer Martijn's formatting but +1 for providing an explanation. Thank you! – Gabriel May 17 '15 at 03:13
  • @Gabriel I'm not sure either; both are quite valid approaches but there are different ways to solve the same "hanging indent" issue with PEP8 compliance. – James Mills May 17 '15 at 04:56