我想问是否有一个 pylint 风格的代码分析器能够建议在可能改进代码的地方使用
:=
运算符。不过,看起来这样的测试已经在两年前添加到pylint
-> github PR(合并)。
无论如何,我从未见过这样的建议,即使对于链接 PR 中的这个例子也没有:
x = 2
if x:
print(x)
# -----
# if (x := 2):
# print(x)
# -----
此功能自 Python 3.8 起可用。 (我正在使用最新的 Python 和 pylint 版本。)虽然我必须以某种方式启用它,但帮助说:
--py-版本
用于版本相关检查的最低 Python 版本。将要 默认为用于运行 pylint 的版本。
出了什么问题?为什么
consider-using-assignment-expr
没有pylint
?
可以通过将以下行添加到 pylint 配置文件来启用
consider-using-assignment-expr
签入 pylint
。我正在使用名为 pylint.toml
: 的配置文件
[tool.pylint.main]
load-plugins="pylint.extensions.code_style"
然后您可以使用
pylint --rcfile <config_file> <python_file>
运行 linter。
请参阅此处了解更多说明。请注意,我使用的是 Python 3.11 和 Pylint 2.17,但自 Python 3.8 起该检查应该可用。