如何避免MyPy检查明确排除但导入的模块_without_手动添加`type:impogenerate)(自动化)?

发布于 2025-01-31 07:23:57 字数 1332 浏览 1 评论 0原文

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

  1. main.py,它应该使用mypy
  2. importedmodule.py.py不应被检查,因为它是因为它是自动化。 此文件已自动化,我不想添加类型:ignore

mypy命令

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

main.py

"""
This should be type checked
"""

from importedmodule import say_hello

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

importedModule.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抱怨ain.py中甚至没有导入的功能:

importedModule.py:14:错误:不兼容的返回值类型(get str”,预期“ int”) 在1个文件中找到1个错误(检查1个源文件)

我的排除问题是什么?

In the following MWE, I have two files/modules:

  1. main.py which is and should be checked with mypy
  2. and importedmodule.py which should not be type checked because it is autogenerated. This file is autogenerated, I don't want to add type:ignore.

MyPy Command

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

main.py

"""
This should be type checked
"""

from importedmodule import say_hello

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

importedmodule.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

But MyPy complains about the function that is not even imported in main.py:

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

What is wrong about my exclude?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一影成城 2025-02-07 07:23:57

这是 suterliakov的评论关于您写为答案的问题。

pyproject.toml文件中,您可以

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

使用此配置插入其他mypy配置下面的以下内容。您将忽略来自上述模块的所有错误。

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

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

相关文档:

Here is SUTerliakov's comment on your question written as an answer.

In the pyproject.toml file you can insert the following below your other mypy config

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

With this config you will ignore all errors coming from the mentioned module.

By using a wildcard, you can also ignore all modules in a directory:

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

Related Documentation:

那请放手 2025-02-07 07:23:57

来自 mypy documentation

特别是

- 排除不会影响mypy的导入关注。您可以使用每示波器 a>配置选项,以避免Mypy遵循输入和检查您不希望检查的代码。


不幸的是,似乎没有一个CLI选项可以使MyPy用于导入模块中未使用的功能。导入模块时,Mypy分析了所有这些。 static 分析仪未检查是否使用函数。

但是,您可以使这些导入模块中造成的任何错误保持沉默

$ mypy main.py --follow-imports silent
Success: no issues found in 1 source file
$ mypy main.py --follow-imports skip
Success: no issues found in 1 source file

From the mypy documentation,

In particular, --exclude does not affect mypy’s import following. You can use a per-module follow_imports config option to additionally avoid mypy from following imports and checking code you do not wish to be checked.

Unfortunately it doesn't seem like there is a cli option for disabling mypy for unused functions in imported modules. When importing a module, mypy analyses all of it. The static analyser doesn't check if a function is used or not.

However you can silence any errors created in those imported modules

$ mypy main.py --follow-imports silent
Success: no issues found in 1 source file
$ mypy main.py --follow-imports skip
Success: no issues found in 1 source file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文