可以评论 pylint 注释

Possible to comment a pylint annotation

我想禁用 pylint 中的警告并说明我禁用它的原因:

from typing import Union, NoReturn, Optional # pylint: disable=unused-import (unused because hack for forward declaration!)

但这给了我错误:

bad-option-value: Bad option value 'declaration'

有没有办法 comment/provide 了解为什么禁用警告的上下文?还是必须在另一条线上才能做到这一点?

我不确定这是否是 'correct' 的答案,但经过反复试验,在 annotation/disable 允许停止解释后添加另一个注释字符 # pylint。例如:

from typing import NoReturn, Optional # pylint: disable=unused-import # my comment here...

解析这些“pragma”注释的源代码在这里:

https://github.com/PyCQA/pylint/blob/master/pylint/utils/pragma_parser.py

它说:

Allow stopping after the first semicolon/hash encountered, so that an option can be continued with the reasons why it is active or disabled.

所以,这是可能的。

如果我正确理解这段代码,你应该可以使用

# pylint: disable=unused-import # unused because hack for forward declaration!

# pylint: disable=unused-import ; unused because hack for forward declaration!

可以在disable=unused-import后面加分号吗?

from typing import Union, NoReturn, Optional # pylint: disable=unused-import; (unused because hack for forward declaration!)

参考:http://pylint.pycqa.org/en/latest/user_guide/message-control.html