【问题标题】:Is it possible to reduce/eliminate MyPy errors for system level code with a command line option?是否可以使用命令行选项减少/消除系统级代码的 MyPy 错误?
【发布时间】:2020-10-19 18:00:17
【问题描述】:

在我的代码上运行 MyPy,这些错误似乎无法通过命令行选项禁用。我不想将代码标记为被忽略,这样当他们得到类型提示时事情会变得更好。我只想从 site-packages 文件夹中的文件中删除它,而不是在我的任何代码中。

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

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

【问题讨论】:

  • 这不是您问题的直接答案,但您是否考虑过在这种特殊情况下使用Django type stubs?
  • 我现在正在使用它。确实解决了很多问题。谢谢。如果这是一个答案,即使它只处理一些问题,我也会接受它,它确实解决了代码示例,我找不到任何选项来实际做我想做的事情。

标签: python django mypy


【解决方案1】:

这就是我的工作:

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

【讨论】:

  • 如果我正在导入自己的文件,这不会默默地抑制这些错误吗?
  • 我不是这方面的专家,但如果你列出我写过“file1.py file2.py”的所有相关文件,我相信它们不应该被跳过。
【解决方案2】:

您可以通过创建mypy.ini config file 来抑制来自第 3 方模块的所有警告,该mypy.ini config file 会在每个模块的基础上抑制导入错误,如下所示:

[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.*] 设置一个部分,而不是列出以上所有内容,但这最终可能会意外隐藏上面 cmets 中提到的 django 类型提示存根可能对 django 的任何误用否则能抓到。

有关处理这些“未找到类型提示”错误的选项的更多详细信息,请参阅https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-type-hints-for-third-party-library。

【讨论】:

    猜你喜欢
    • 2012-01-29
    • 2022-08-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    相关资源
    最近更新 更多