Loading...
Loading...
Compare original and translation side by side
uv run mypy .uv run mypy .cast()type: ignorecast()type: ignorecast()# type: ignoreundefinedcast()# type: ignoreundefined
**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.
**原因**:`cast()`会明确记录你期望的类型,让代码更具可读性和可维护性。它也不会掩盖同一行上的其他潜在错误。assertundefinedassertundefined
**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.
**原因**:断言可以通过`python -O`在生产环境中禁用,因此对于运行时验证来说并不可靠。恰当的异常能确保检查始终执行,并提供更好的错误处理。type: ignoretype: ignoretype: ignoreundefinedtype: ignoreundefinedundefinedundefinedgettext_lazydjango-stubs-extfrom __future__ import annotations # Must be the first import
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from django_stubs_ext import StrOrPromisegettext_lazydjango-stubs-extfrom __future__ import annotations # 必须是第一个导入语句
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from django_stubs_ext import 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 runtime
- `django-stubs-ext` is a dev dependency and won't be available in production
- This pattern allows both regular strings and lazy translation strings to be accepted
**为什么使用此模式**:
- `from __future__ import annotations`使类型注解在运行时以字符串形式存在(不被求值)
- `if TYPE_CHECKING:`确保仅在类型检查时导入,而非运行时
- `django-stubs-ext`是开发依赖,在生产环境中不可用
- 此模式允许同时接受常规字符串和延迟翻译字符串