如何避免MyPy检查明确排除但导入的模块_without_手动添加`type:impogenerate)(自动化)?
在以下MWE中,我有两个文件/模块:
main.py
,它应该使用mypy- 和
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:
main.py
which is and should be checked with mypy- and
importedmodule.py
which should not be type checked because it is autogenerated. This file is autogenerated, I don't want to addtype: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 suterliakov的评论关于您写为答案的问题。
在
pyproject.toml
文件中,您可以使用此配置插入其他mypy配置下面的以下内容。您将忽略来自上述模块的所有错误。
通过使用通配符,您还可以忽略目录中的所有模块:
相关文档:
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 configWith 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:
Related Documentation:
来自 mypy documentation
不幸的是,似乎没有一个CLI选项可以使MyPy用于导入模块中未使用的功能。导入模块时,Mypy分析了所有这些。 static 分析仪未检查是否使用函数。
但是,您可以使这些导入模块中造成的任何错误保持沉默
From the mypy documentation,
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