重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
fix-types by saaspegasus/django-skills
npx skills add https://github.com/saaspegasus/django-skills --skill fix-types要修复类型问题,请按以下步骤操作。
首先运行类型检查器以查看存在的问题:
uv run mypy .
将发现的错误按逻辑分组。
对于每一组错误,逐个检查错误,告诉我你打算应用的修复方法,然后在继续之前询问我是否有任何问题或建议。
只有在我批准后,才应用修复并转到组中的下一个错误。
完成一组后,询问我是否要继续处理下一组。
cast() 而非 type: ignore当 mypy 无法推断出正确的类型时,优先使用 cast() 而不是 # type: ignore:
# 推荐 - 记录了预期的类型
choices = cast(list[tuple[str, str]], field.choices)
# 在可以使用 cast 时应避免 - 这只是静默了错误
choices = list(field.choices) # type: ignore[arg-type]
原因: cast() 明确地记录了你期望的类型,使代码更具可读性和可维护性。它也不会静默同一行上其他潜在的错误。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
当添加空值检查以满足 mypy 时,优先引发适当的异常而非使用 assert:
# 推荐 - 适当的错误处理
if obj.related_field is None:
raise ValueError("对象必须有一个关联字段")
result = obj.related_field.some_method()
# 避免 - 断言可以通过 -O 标志禁用
assert obj.related_field is not None
result = obj.related_field.some_method()
原因: 在生产环境中,断言可以通过 python -O 禁用,这使得它们对于运行时验证不可靠。适当的异常确保检查始终运行并提供更好的错误处理。
type: ignore 时,添加注释如果必须使用 type: ignore(例如,mypy 对有效代码存在限制),请务必添加简短说明:
# 良好 - 解释了为何需要忽略
self.tier = tier # type: ignore[misc] # mypy 无法处理带有自定义 __init__ 的 Enum 元组值
# 不佳 - 没有解释
self.tier = tier # type: ignore[misc]
当在类型提示中使用 Django 的惰性翻译字符串 (gettext_lazy) 时,使用以下模式以避免 mypy 错误,同时保持代码在生产环境(未安装 django-stubs-ext)中正常工作:
from __future__ import annotations # 必须是第一个导入
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from django_stubs_ext import StrOrPromise
# 然后在类型提示中使用 StrOrPromise
def my_function(name: StrOrPromise) -> StrOrPromise:
...
class MyData:
title: StrOrPromise
description: StrOrPromise
为何使用此模式:
from __future__ import annotations 使得类型注解在运行时成为字符串(不被求值)if TYPE_CHECKING: 确保导入仅在类型检查时发生,而不是在运行时django-stubs-ext 是一个开发依赖项,在生产环境中不可用每周安装次数
54
代码仓库
GitHub 星标数
13
首次出现
2026年2月5日
安全审计
安装于
gemini-cli52
kimi-cli52
codex52
github-copilot52
amp52
opencode52
To fix types, do the following.
First run the type checker to see what the issues are:
uv run mypy .
Group the errors you find in to logical buckets.
For each bucket of errors, go through the errors one at a time, tell me the fix you want to apply, and then ask if I have any questions or suggestions before proceeding.
Only once I approve, apply the fix and move onto the next error in the bucket.
Once you've completed a bucket, ask me if I'd like to move on to the next bucket.
cast() over type: ignoreWhen mypy can't infer the correct type, prefer using cast() over # type: ignore:
# Preferred - documents the expected type
choices = cast(list[tuple[str, str]], field.choices)
# Avoid when cast is possible - just silences the error
choices = list(field.choices) # type: ignore[arg-type]
Why: cast() explicitly documents what type you expect, making the code more readable and maintainable. It also doesn't silence other potential errors on the same line.
When adding null checks to satisfy mypy, prefer raising proper exceptions over using assert:
# Preferred - proper error handling
if obj.related_field is None:
raise ValueError("Object must have a related field")
result = obj.related_field.some_method()
# Avoid - assertions can be disabled with -O flag
assert obj.related_field is not None
result = obj.related_field.some_method()
Why: Assertions can be disabled in production with python -O, making them unreliable for runtime validation. Proper exceptions ensure the check always runs and provides better error handling.
type: ignore, add a commentIf type: ignore is necessary (e.g., mypy limitation with valid code), always add a short explanation:
# Good - explains why the ignore is needed
self.tier = tier # type: ignore[misc] # mypy can't handle Enum tuple values with custom __init__
# Bad - no explanation
self.tier = tier # type: ignore[misc]
When using type hints with Django's lazy translation strings (gettext_lazy), use the following pattern to avoid mypy errors while keeping the code working in production (where django-stubs-ext is not installed):
from __future__ import annotations # Must be the first import
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from django_stubs_ext import StrOrPromise
# Then use StrOrPromise in type hints
def my_function(name: StrOrPromise) -> StrOrPromise:
...
class MyData:
title: StrOrPromise
description: StrOrPromise
Why this pattern:
from __future__ import annotations makes type annotations strings at runtime (not evaluated)if TYPE_CHECKING: ensures the import only happens during type checking, not at runtimedjango-stubs-ext is a dev dependency and won't be available in productionWeekly Installs
54
Repository
GitHub Stars
13
First Seen
Feb 5, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykPass
Installed on
gemini-cli52
kimi-cli52
codex52
github-copilot52
amp52
opencode52
Caveman-Review:AI代码审查工具,提升PR评论效率与代码质量
7,700 周安装