gpt4 book ai didi

python - 如何避免 mypy 检查明确排除但导入的模块_without_ 手动添加 `type:ignore`(自动生成)?

转载 作者:行者123 更新时间:2023-12-05 03:24:11 24 4
gpt4 key购买 nike

在下面的 MWE 中,我有两个文件/模块:

  1. main.py 应该用 mypy 检查
  2. importedmodule.py 不应进行类型检查,因为它是自动生成的。 这个文件是自动生成的,我不想添加type:ignore

MyPy 命令

$ mypy main.py --exclude '.*importedmodule.*'
$ mypy --version
mypy 0.931

主.py

"""
This should be type checked
"""

from importedmodule import say_hello

greeting = say_hello("Joe")
print(greeting)

导入模块.py

"""
This module should not be checked in mypy, because it is excluded
"""


def say_hello(name: str) -> str:
# This function is imported and called from my type checked code
return f"Hello {name}!"


def return_an_int() -> int:
# ok, things are obviously wrong here but mypy should ignore them
# also, I never expclitly imported this function
return "this is a str, not an int" # <-- this is line 14 referenced in the mypy error message

但是 MyPy 提示函数甚至没有在 main.py 中导入:

importedmodule.py:14: error: Incompatible return value type (got "str", expected "int")Found 1 error in 1 file (checked 1 source file)

我的排除有什么问题?

最佳答案

为了制作SUTerliakov答案更明显,我想在这里再详细介绍一下。

pyprocject.toml 文件中,您可以在其他 mypy 配置下方插入以下内容

[[tool.mypy.overrides]]
module = "importedmodule"
ignore_errors = true

使用此配置,您将忽略来自上述模块的所有错误。

通过使用通配符,您还可以忽略目录中的所有模块:

[[tool.mypy.overrides]]
module = "importedpackage.*"
ignore_errors = true

关于python - 如何避免 mypy 检查明确排除但导入的模块_without_ 手动添加 `type:ignore`(自动生成)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72345536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com