简体   繁体   中英

How to stop autopep from indenting line after backslash?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM