You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode中使用Django插件时Mypy忽略类型提示的问题

Django项目中VSCode的mypy插件无类型提示(终端运行正常)的解决方法

问题场景

本地在Django项目中使用VSCode的mypy插件,按照Django Stubs文档配置了pyproject.toml:

[tool.mypy]
mypy_path = "."
python_version = "3.13"
strict = true
plugins = ["mypy_django_plugin.main"]

[tool.django-stubs]
django_settings_module = "conf.settings"

出现以下异常情况:

  • 终端手动运行mypy时,类型检查完全正常
  • VSCode中mypy插件无任何类型提示
  • 注释掉plugins配置项后,VSCode的mypy恢复正常提示

排查过程

  • 确认VSCode已加载正确的pyproject.toml配置
  • 测试发现通过Remote Explorer在Docker容器内使用VSCode时,该问题不存在,推测本地VSCode环境未正确初始化Django上下文
  • 查看VSCode输出终端,捕获到关键错误日志:
django.core.exceptions.ImproperlyConfigured: 'django.db.backends.None' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of: 'mysql', 'oracle', 'postgresql', 'sqlite3' 

最终解决方法

移除原pyproject.toml中的mypy相关配置,创建mypy.ini文件并添加以下内容:

[mypy]
ignore_missing_imports = True # 允许从未类型化模块导入
strict = True
disallow_untyped_calls = False
disallow_untyped_defs = True
disallow_incomplete_defs = True
disallow_untyped_decorators = False
explicit_package_bases = True

plugins =
    mypy_django_plugin.main,
    mypy_drf_plugin.main

内容的提问来源于stack exchange,提问作者Zio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 10:32:34