如何让 autopep8 允许在行尾添加注释?
我正在尝试使用 autopep8 格式化我的代码(因为这似乎是 VSCode 中的默认代码)。但它有一个问题:
我使用的库不关心输入,因此我需要在某些行后加上 # type:ignore
以使 linter 忽略该特定文件。但是当格式化(使用 autopep8)时,格式化程序将注释移动到上面的行。然后我的 linter 不会像它应该的那样忽略该行。
例如,使用以下代码:
somevar = dumblib.foobar() #type:ignore
我按 Shift+Alt+F 进行格式化,然后代码格式化为:
# type:ignore
somevar = dumblib.foobar()
然后,linter 抱怨“somevar”的类型是 unknonwn。
如何防止 autopep8 在重新格式化期间移动注释?
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 e.g. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个相对较老的问题,但如果其他人也遇到这个问题,我将留下一个对我有帮助的解决方案。
负责将注释移动到代码行上方的 autopep8 规则只不过是一个
max-line-length
。我可以通过增加最大线路长度来解决这个问题。为此,我在项目 (.pep8) 中创建了一个配置文件,并配置 vscode 来使用它(在 autopep8 扩展设置中)。配置文件内容:
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: