Sc4r
Sc4r

Reputation: 700

PEP8: continuation line over-indented for visual indent

I have this line of code which goes over the line and when testing for pep8 errors I get: line too long. So to try and fix this I used slash('\') but then I get continuation line over-indented for visual indent. What can I do to fix this?

enter image description here

Things I've tried:

if first_index < 0 or second_index > \
   self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index < 0 \ 
   or second_index > \
   self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index < 0 or \
   second_index > self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index \
   < 0 or second_index \
   > self._number_of_plates - 1:
     raise ValueError

continuation line over-indented for visual indent

Upvotes: 26

Views: 38420

Answers (4)

Mateen Ulhaq
Mateen Ulhaq

Reputation: 27201

This is what the popular black formatter does:

if (
    first_index < 0
    or second_index > self._number_of_plates - 1
):
    raise ValueError

This looks fine to me. However, it is probably a good idea to refactor so that the code isn't 7 levels of indentation deep in the first place!

Upvotes: 1

Alexander
Alexander

Reputation: 109520

For lines that are too long (e.g. > 79 characters), you can use parentheses to group your conditions:

if (first_index < 0 
        or second_index > self._number_of_plates - 1
        or condition2
        and candition3):
    raise ValueError

Note that any boolean conditions (or, and) should go at the start of the line before the condition.

In your case, there is a special rule because of the if (...) construct:

When the conditional part of an if-statement is long enough to require that it be written across multiple lines, it's worth noting that the combination of a two character keyword (i.e. if), plus a single space, plus an opening parenthesis creates a natural 4-space indent for the subsequent lines of the multiline conditional. This can produce a visual conflict with the indented suite of code nested inside the if-statement, which would also naturally be indented to 4 spaces. This PEP takes no explicit position on how (or whether) to further visually distinguish such conditional lines from the nested suite inside the if-statement. Acceptable options in this situation include, but are not limited to:

# No extra indentation.
if (this_is_one_thing and
    that_is_another_thing):
    do_something()

# Add a comment, which will provide some distinction in editors
# supporting syntax highlighting.
if (this_is_one_thing and
    that_is_another_thing):
    # Since both conditions are true, we can frobnicate.
    do_something()

# Add some extra indentation on the conditional continuation line.
if (this_is_one_thing
        and that_is_another_thing):
    do_something()

(Also see the discussion of whether to break before or after binary operators below.)

Source: PEP 8 Style Guide

Upvotes: 0

hassanzadeh.sd
hassanzadeh.sd

Reputation: 3461

A continuation line is indented farther than it should be for a visual indent.

Anti-pattern In this example, the string "World" is indented two spaces farther than it should be.

print("Python", ("Hello",
                   "World"))

Best practice

print("Python", ("Hello",
                 "World"))

reference: https://www.flake8rules.com/rules/E127.html

Upvotes: 1

David Ehrmann
David Ehrmann

Reputation: 7576

The line-extending backslash has the issue of having trailing whitespace that can break your code. This is a popular fix and is PEP8-compliant:

if (first_index < 0 or
    second_index > self._number_of_plates - 1):

Upvotes: 34

Related Questions