0

autopep8 converts this code

sum_of_two_numbers = first \
                     + second

to this

sum_of_two_numbers = first \
    + second

Is it a bug? If so, is it a bug in pycodestyle or autopep8? Are there any error codes I can ignore to prevent this behavior? If I ignore E127 and E128 it also stops indenting all other cases.

I know that if I use brackets instead of backslash it will work correctly, however, there is an existing repository that uses backslashes in some places which I do not want to change.


UPD. Adding another example from pep8 (https://www.python.org/dev/peps/pep-0008/#maximum-line-length)

Backslashes may still be appropriate at times. For example, long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable:

with open('/path/to/some/file/you/want/to/read') as file_1, \
     open('/path/to/some/file/being/written', 'w') as file_2:
    file_2.write(file_1.read()) ```

autopep8 does not align this example correctly, too.

Waska
  • 1
  • 1
  • *"there is an existing repository that uses backslashes in some places which I do not want to change."* Then why run autopep8 on it at all? Either you do want to change it to make it PEP 8 compliant, or you don't. – kaya3 Mar 23 '21 at 20:28

1 Answers1

0

To my knowledge, you cannot disable just this feature without disabling other auto-indenting. If you are running autopep8 that implies that you are trying to follow pep8 guidelines, in the case of the example you should really be using implied continuation (no backslash, '+' on the previous line, 4 space indent.) If you are using autopep8 on a project you have to be willing to let autopep8 format the project for you. I'm not sure what the attachment to backslashes is here, but it's probably not the nicest way to format what you're doing.

TheTrooble
  • 53
  • 9
  • Thanks for the answer! Please see another example from PEP website. Also PEP encourages +- on next lines and does not prohibit using backslash for line-continuation though I agree it is less common. – Waska Mar 23 '21 at 20:59
  • You are right on the +-, I was thinking of an old flake8 warning from a few years ago, I still to this day get those mixed up. From just above the example you added to your post: "The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation." – TheTrooble Mar 23 '21 at 22:22