Mypy未使用Optional却报名称未定义错误排查
mypy 执行Python类型检查时报错「Optional未定义」但对应代码行未使用Optional的问题
环境依赖版本
- mypy 0.950
- django-stubs 1.11.0
- Django 4.0.5
- Python 3.10.2
报错输出
命令行执行mypy检查时,返回如下报错信息:
project/suppliers/models.py:6: error: Name "Optional" is not defined project/suppliers/models.py:6: note: Did you forget to import it from "typing"? (Suggestion: "from typing import Optional") project/users/models.py:6: error: Name "Optional" is not defined project/users/models.py:6: note: Did you forget to import it from "typing"? (Suggestion: "from typing import Optional") project/products/models.py:6: error: Name "Optional" is not defined project/products/models.py:6: note: Did you forget to import it from "typing"? (Suggestion: "from typing import Optional")(Suggestion: "from typing import Optional")
代码核查结果
逐一核查报错文件对应行,均未使用Optional类型:
project/suppliers/models.py第6行为完全空行,文件头部代码如下:
from django.contrib.sites.managers import CurrentSiteManager from django.contrib.sites.models import Site from django.db import models from django.utils.text import slugify from django.utils.translation import gettext_lazy as _ from django_countries.fields import CountryField from project.core.models import BaseImageModel, BaseModel from project.suppliers.managers import SupplierQuerySet _SupplierManager = models.Manager.from_queryset(SupplierQuerySet) class Supplier(BaseModel, BaseImageModel): ...
project/users/models.py第6行为Django导入语句from django.contrib.contenttypes.models import ContentType,文件头部代码如下:
import random from typing import Any from django.conf import settings from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin from django.contrib.contenttypes.models import ContentType from django.contrib.sites.managers import CurrentSiteManager from django.contrib.sites.models import Site from django.core import signing from django.core.mail import send_mail from django.db import models from django.forms import ValidationError from django.http import HttpRequest from django.template.loader import render_to_string from django.utils import timezone from django.utils.encoding import force_bytes, force_str from django.utils.http import ( urlsafe_base64_decode as uid_decoder, urlsafe_base64_encode as uid_encoder, ) from django.utils.translation import gettext_lazy as _ import phonenumbers from project.users.enums import AvatarColors from project.users.managers import UserQuerySet from project.users.schemas.records import ( UserAuditLogsRecord, UserNotesRecord, UserProfileRecord, ) _UserManager = models.Manager.from_queryset(UserQuerySet) class User(AbstractBaseUser, PermissionsMixin):
project/products/models.py第6行为Django工具导入语句from django.utils.text import slugify,文件头部代码如下:
from decimal import Decimal from django.contrib.sites.managers import CurrentSiteManager from django.contrib.sites.models import Site from django.db import models from django.utils.text import slugify from django.utils.translation import gettext_lazy as _ from imagekit.models import ImageSpecField from imagekit.processors import ResizeToFill from mptt.models import TreeManyToManyField ...
现有mypy配置
[tool.mypy] plugins = ["mypy_django_plugin.main", "pydantic.mypy"] follow_imports = "normal" ignore_missing_imports = true disallow_untyped_calls = true disallow_untyped_defs = true disallow_incomplete_defs = true warn_unused_configs = true warn_redundant_casts = true warn_unused_ignores = true warn_return_any = true warn_unreachable = true no_implicit_optional = true no_implicit_reexport = true check_untyped_defs = true strict_equality = true [tool.django-stubs] django_settings_module = "project.settings" [tool.pydantic-mypy] init_forbid_extra = true init_typed = true warn_required_dynamic_aliases = true warn_untyped_fields = true # Admin files uses some patterns that are not easily typed [[tool.mypy.overrides]] module = "project.*.admin" ignore_errors = true [[tool.mypy.overrides]] module = "project.*.tests.*" ignore_errors = true [[tool.mypy.overrides]] module = "project.*.migrations.*" ignore_errors = "true" [[tool.mypy.overrides]] module = "project.*.management.*" disallow_untyped_defs = false
问题说明
已检索公开技术资料,未找到同类问题的解决方案,需要确认该问题是配置疏漏导致,还是mypy或django-stubs的版本bug。
内容的提问来源于stack exchange,提问作者DanielK
相关产品推荐
相关产品推荐

