从 mypy.ini 或 pyproject.toml 文件中禁用 mypy 的错误代码 arg-type

问题描述 投票:0回答:1

如何在 mypy 配置文件中禁用错误代码

[arg-type]
的错误,使用其
mypy.ini
pyproject.toml
配置文件?

我已经尝试过

disable_error_code = [arg-type]
disable_error_code = arg-type
disable_error_code = arg_type
mypy.ini
文件中,但这些都没有解决我的问题。

我试图忽略配置文件中不兼容的类型。

python mypy typechecking
1个回答
0
投票

如果您确实想在整个项目中全局禁用

arg-type
错误,您可以将
"arg-type"
添加到
tool.mypy.disable_error_code
中的
pyproject.toml
:

[tool.mypy]
disable_error_code = ["arg-type"]

但是,这可能不是最好的主意。禁用错误类型的函数参数的错误会削弱许多静态类型检查。更有可能的是,您只想在更本地的范围内禁用此 lint,并且仅在需要时禁用。

要仅对几个模块禁用它,您可以使用

tool.mypy.override
表。例如,要在模块
my_proj.foo
以及
my_proj.bar
内的所有模块中禁用它,您可以将此配置包含在您的
pyproject.toml
中:

[[tool.mypy.overrides]]
module = ["my_proj.foo", "my_proj.bar.*"]
disable_error_code = ["arg-type"]

或者,您可以添加行

# mypy: disable_error_code="arg-type"

就在您想要禁用错误的文件的顶部。

或者对于最有限的范围,仅对带有错误类型参数的函数调用使用

type: ignore
注释:

foo("bad")  # type: ignore[arg-type]
© www.soinside.com 2019 - 2024. All rights reserved.