是否可以通过命令行选项减少/消除系统级代码的MyPy错误



在我的代码上运行MyPy,这些错误似乎无法用命令行选项禁用。我不想把代码标记为被忽略,这样当它们得到类型提示时,情况就会变得更好。我只是想从网站软件包文件夹中的文件中消除这一点,而不是在我的任何代码中。

from django.db import connection
from django.conf import settings
from django.test import TestCase, TransactionTestCase
from django.utils import timezone

所有这些都遭受着这种"痛苦";错误";

error: Skipping analyzing 'django.conf': found module but no type hints or library stubs

我应该清楚,我不想忽略这个代码的不存在,只想忽略类型提示的不存在。

这就是我所做的:

mypy --disallow-untyped-calls --ignore-missing-imports file1.py file2.py

您可以通过创建一个mypy.ini配置文件来抑制来自第三方模块的所有警告,该文件在每个模块的基础上抑制导入错误,如下所示:

[mypy]
# The [mypy] section is for any global mypy configs you want to set.
# Per-module configs are listed below.
[mypy-django.db.*]
ignore_missing_imports = True
[mypy-django.conf.*]
ignore_missing_imports = True
[mypy-django.test.*]
ignore_missing_imports = True
[mypy-django.utils.*]
ignore_missing_imports = True

这有点像运行mypy --ignore-missing-module your_code,只是我们只忽略列出的django模块(及其子模块(。

当然,您可以只为[mypy-django.*]提供一个单独的部分,而不是列出上面的所有内容,但这可能会意外地隐藏上面注释中提到的django类型提示存根可能会发现的对django的任何误用。

有关处理这些"问题"的选项的更多详细信息;没有找到类型提示";错误,请参阅https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-为第三方库键入提示。

最新更新