简体   繁体   中英

How do I make autopep8 allow a comment at the end of a line?

I'm trying to format my code with autopep8 (because this appears to be the default one in VSCode). But there is a problem with it:

I am using a lib that doesn't care about typing, so I need to suffix some lines with # type:ignore to make the linter ignore that particular file. But when formatting (with autopep8) the formatter moves the comment to the line above. Then my linter doesn't ignore the line like it should.

So with eg this code:

somevar = dumblib.foobar() #type:ignore

I hit Shift+Alt+F to format, and then the code is formatted to this:

# type:ignore
somevar = dumblib.foobar()

And then, the linter complains that the type of "somevar" is unknonwn.

How can I prevent autopep8 from moving the comment during a reformat?

It's a relatively old question but in case others also struggle with this, I will leave a solution that helped me.

The autopep8 rule that is responsible for moving comments above the code line is nothing but a max-line-length . I was able to fix this by increasing the maximum line length. To do this I created a configuration file in my project (.pep8) and configured vscode to use it (in the autopep8 extension settings).

Configuration file contents:

[pep8]
max-line-length = 160

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