Python Pycharm 的代码风格检查:忽略/关闭特定规则

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28428307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:16:24  来源:igfitidea点击:

Pycharm's code style inspection: ignore/switch off specific rules

pythonidepycharmpep8

提问by Krzysztof Stanis?awek

I'm trying to import existing project into PyCharm. I can refactor the code so that PyCharm will be pleased, but we like to have spaces around colons in dictionaries, like this: {"A" : "B"}. We also like aligning assignments:

我正在尝试将现有项目导入 PyCharm。我可以重构,使PyCharm会很高兴的代码,但我们希望有各地的冒号空间字典中,像这样:{"A" : "B"}。我们也喜欢对齐作业:

a   = 1
abc = 3

Is there a way to configure PyCharm, so that he'll ignore all errors/warnings related to this specific stuff?

有没有办法配置 PyCharm,以便他忽略与此特定内容相关的所有错误/警告?

回答by wolfrevo

to have spaces around colons in dictionaries, configure Settings > Editor > PythonSpaces

要在字典中的冒号周围有空格,请配置 Settings > Editor > PythonSpaces

Other > Before ':'

Other > Before ':'

and

Other > After ':'

Other > After ':'

回答by Cui Heng

@Krzysztof Stanis?awek, function is different as Pycharm follows the PEP8 coding style, so it is recommended that there is no whitespace between the function variables and ":", if you don't want this, you can disable it by

@Krzysztof Stanis?awek,函数不同,Pycharm 遵循 PEP8 编码风格,所以建议函数变量和“:”之间没有空格,如果你不想这样,你可以禁用它

"Settings"-> "Editor"-> "Inspections"->"PEP8 coding style violation"

However, this is not recommended.

但是,不建议这样做。

回答by fantabolous

If you're ok to ignore all matching issues, you can just press Alt-Enter (or click on the lightbulb) and choose "Disable Inspection". Saves you going into the settings and trying to figure out the inspection rule that matches.

如果您可以忽略所有匹配的问题,您只需按 Alt-Enter(或单击灯泡)并选择“禁用检查”。保存您进入设置并尝试找出匹配的检查规则。

From http://iambigblind.blogspot.jp/2013/02/configuring-pep8py-support-in-pycharm-27.html

来自http://iambigblind.blogspot.jp/2013/02/configuring-pep8py-support-in-pycharm-27.html

回答by arcseldon

Using PyCharm 5 (community edition), you can do the following: Code –> Inspect Code. Then select the required inspection error, and click on the "Suppress" option or "Ignore errors like this" option on right hand side. Please look at the screenshot below:

使用PyCharm 5(社区版),你可以做到以下几点:Code –> Inspect Code。然后选择所需的检查错误,并单击右侧的“抑制”选项或“忽略此类错误”选项。请看下面的截图:

enter image description here

在此处输入图片说明

When you choose the "Suppress" option, it adds a comment as shown in the screenshot below:

当您选择“ Suppress”选项时,它会添加一条评论,如下面的屏幕截图所示:

enter image description here

在此处输入图片说明

Suppressing can be done at the statement, or function/method, levels. If trying to suppress an argument to a function, the suppression only works at the function level (meaning it also suppresses other name reference violations that might exist within that function).

可以在语句或函数/方法级别进行抑制。如果试图抑制函数的参数,则抑制仅在函数级别起作用(这意味着它还抑制了该函数中可能存在的其他名称引用冲突)。

You also have the option of switching off "PEP8 coding style violations" altogether (by ticking the box shown below), or explicitly managing "Ignore Errors" manually. Screenshot given below:

您还可以选择完全关闭“ PEP8 编码风格违规”(通过勾选下面显示的框),或手动明确管理“忽略错误”。截图如下:

enter image description here

在此处输入图片说明

In general, you should perhaps question why you are suppressing PEP8 guidelines. However, sometimes it appears necessary, for instance when using the pytestmodule, it is necessary to shadow variables, etc, which the PEP8 Inspection complains about in. In such cases, this feature in PyCharm is very helpful.

一般来说,您或许应该质疑为什么要禁止 PEP8 指南。然而,有时它似乎是必要的,例如在使用pytest模块时,需要隐藏变量等,而 PEP8 检查在这种情况下抱怨。在这种情况下,PyCharm 中的此功能非常有用。

回答by Sylvain

I got the same issue, and the neat solution has been pointed by @fantabolous, from configuring PEP8.py support in PyCharm 2.7

我遇到了同样的问题,@fantabolous 指出了巧妙的解决方案,从在 PyCharm 2.7 中配置 PEP8.py 支持

Example before
Code with multiple space warning

之前的例子
带有多个空格警告的代码

Adding the error code either manually or with "Alt+Enter" on error highlight
The error code can be found here
enter image description here

手动添加错误代码或在错误突出显示中使用“Alt+Enter”添加
错误代码可以在此处找到错误代码
在此处输入图片说明

After the changes
enter image description here

更改后
在此处输入图片说明

It's great to select precisely some rules instead of disabling all warning from PEP8. Thanks to the previous comments.

精确地选择一些规则而不是禁用来自 PEP8 的所有警告是很好的。感谢之前的评论。