ty-skills
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesety-skills
ty-skills
Master Python type checking with ty - the extremely fast type checker written in Rust by Astral (creators of uv and Ruff).
掌握基于ty的Python类型检查——这是由Astral(uv和Ruff的开发者)用Rust编写的超快速类型检查工具。
When to Use This Skill
何时使用本技能
- Adding type annotations to Python code
- Fixing type errors and diagnostics from ty
- Configuring ty rules and severity levels
- Migrating from mypy or pyright to ty
- Understanding advanced type patterns (intersection types, protocols, generics)
- Setting up ty language server in your editor
- 为Python代码添加类型注解
- 修复ty报告的类型错误和诊断信息
- 配置ty的规则和严重级别
- 从mypy或pyright迁移至ty
- 理解高级类型模式(交集类型、协议、泛型)
- 在编辑器中配置ty语言服务器
Quick Start
快速开始
bash
undefinedbash
undefinedInstall
Install
uv tool install ty
uv tool install ty
or: pip install ty
or: pip install ty
Check current directory
Check current directory
ty check
ty check
Check specific files
Check specific files
ty check src/
ty check src/
Full diagnostics
Full diagnostics
ty check --output-format full
undefinedty check --output-format full
undefinedConfiguration
配置
Configure via :
pyproject.tomltoml
[tool.ty.environment]
python-version = "3.12"
python = "./.venv"
python-platform = "linux"
root = ["./src"]
extra-paths = ["./typings"]
[tool.ty.rules]通过进行配置:
pyproject.tomltoml
[tool.ty.environment]
python-version = "3.12"
python = "./.venv"
python-platform = "linux"
root = ["./src"]
extra-paths = ["./typings"]
[tool.ty.rules]error: fail CI, warn: report, ignore: disable
error: 导致CI失败, warn: 仅报告, ignore: 禁用
possibly-unresolved-reference = "error"
invalid-argument-type = "error"
division-by-zero = "warn"
unused-ignore-comment = "warn"
[tool.ty.src]
include = ["src", "tests"]
exclude = ["src/migrations/"]
possibly-unresolved-reference = "error"
invalid-argument-type = "error"
division-by-zero = "warn"
unused-ignore-comment = "warn"
[tool.ty.src]
include = ["src", "tests"]
exclude = ["src/migrations/"]
Per-file overrides
按文件覆盖配置
[[tool.ty.overrides]]
include = ["tests/**"]
[tool.ty.overrides.rules]
possibly-unresolved-reference = "warn"
undefined[[tool.ty.overrides]]
include = ["tests/**"]
[tool.ty.overrides.rules]
possibly-unresolved-reference = "warn"
undefinedRules Quick Reference
规则速查
| Rule | Default | Description |
|---|---|---|
| error | Variable might not be defined |
| error | Argument type mismatch |
| error | Assigned value incompatible |
| error | Required argument missing |
| error | Operator not supported for types |
| error | Return type mismatch |
| warn | Potential division by zero |
| warn | Suppression not needed |
| warn | Cast has no effect |
| warn | Attribute might not exist |
| warn | Index might be out of range |
| 规则 | 默认级别 | 描述 |
|---|---|---|
| error | 变量可能未定义 |
| error | 参数类型不匹配 |
| error | 赋值类型不兼容 |
| error | 缺少必填参数 |
| error | 类型不支持该运算符 |
| error | 返回类型不匹配 |
| warn | 可能存在除零错误 |
| warn | 抑制注释无必要 |
| warn | 类型转换无意义 |
| warn | 属性可能不存在 |
| warn | 索引可能越界 |
Intersection Types (ty Exclusive)
交集类型(ty专属特性)
ty has first-class intersection type support:
python
def output_as_json(obj: Serializable) -> str:
if isinstance(obj, Versioned):
reveal_type(obj) # reveals: Serializable & Versioned
return str({
"data": obj.serialize_json(), # From Serializable
"version": obj.version # From Versioned
})
return obj.serialize_json()ty原生支持交集类型:
python
def output_as_json(obj: Serializable) -> str:
if isinstance(obj, Versioned):
reveal_type(obj) # 显示类型: Serializable & Versioned
return str({
"data": obj.serialize_json(), # 来自Serializable
"version": obj.version # 来自Versioned
})
return obj.serialize_json()Suppression Comments
抑制注释
python
undefinedpython
undefinedSuppress single rule
抑制单个规则
x: int = "hello" # type: ignore[incompatible-assignment]
x: int = "hello" # type: ignore[incompatible-assignment]
Suppress multiple
抑制多个规则
y = risky() # type: ignore[possibly-unresolved-reference, invalid-argument-type]
undefinedy = risky() # type: ignore[possibly-unresolved-reference, invalid-argument-type]
undefinedReference Documents
参考文档
For detailed information, see:
| Document | Content |
|---|---|
| All rules with examples and fixes |
| Python typing module quick reference |
| Protocols, generics, type guards, variance |
| mypy/pyright → ty migration |
| Error solutions with examples |
| VS Code, Cursor, Neovim setup |
如需详细信息,请参阅:
| 文档 | 内容 |
|---|---|
| 所有规则及示例与修复方案 |
| Python typing模块速查 |
| 协议、泛型、类型守卫、方差 |
| mypy/pyright → ty迁移指南 |
| 常见错误解决方案及示例 |
| VS Code、Cursor、Neovim配置指南 |