ty-skills

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

ty-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
undefined
bash
undefined

Install

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
undefined
ty check --output-format full
undefined

Configuration

配置

Configure via
pyproject.toml
:
toml
[tool.ty.environment]
python-version = "3.12"
python = "./.venv"
python-platform = "linux"
root = ["./src"]
extra-paths = ["./typings"]

[tool.ty.rules]
通过
pyproject.toml
进行配置:
toml
[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"
undefined

Rules Quick Reference

规则速查

RuleDefaultDescription
possibly-unresolved-reference
errorVariable might not be defined
invalid-argument-type
errorArgument type mismatch
incompatible-assignment
errorAssigned value incompatible
missing-argument
errorRequired argument missing
unsupported-operator
errorOperator not supported for types
invalid-return-type
errorReturn type mismatch
division-by-zero
warnPotential division by zero
unused-ignore-comment
warnSuppression not needed
redundant-cast
warnCast has no effect
possibly-unbound-attribute
warnAttribute might not exist
index-out-of-bounds
warnIndex might be out of range
规则默认级别描述
possibly-unresolved-reference
error变量可能未定义
invalid-argument-type
error参数类型不匹配
incompatible-assignment
error赋值类型不兼容
missing-argument
error缺少必填参数
unsupported-operator
error类型不支持该运算符
invalid-return-type
error返回类型不匹配
division-by-zero
warn可能存在除零错误
unused-ignore-comment
warn抑制注释无必要
redundant-cast
warn类型转换无意义
possibly-unbound-attribute
warn属性可能不存在
index-out-of-bounds
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
undefined
python
undefined

Suppress 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]
undefined
y = risky() # type: ignore[possibly-unresolved-reference, invalid-argument-type]
undefined

Reference Documents

参考文档

For detailed information, see:
DocumentContent
references/ty_rules_reference.md
All rules with examples and fixes
references/typing_cheatsheet.md
Python typing module quick reference
references/advanced_patterns.md
Protocols, generics, type guards, variance
references/migration_guide.md
mypy/pyright → ty migration
references/common_errors.md
Error solutions with examples
references/editor_setup/
VS Code, Cursor, Neovim setup
如需详细信息,请参阅:
文档内容
references/ty_rules_reference.md
所有规则及示例与修复方案
references/typing_cheatsheet.md
Python typing模块速查
references/advanced_patterns.md
协议、泛型、类型守卫、方差
references/migration_guide.md
mypy/pyright → ty迁移指南
references/common_errors.md
常见错误解决方案及示例
references/editor_setup/
VS Code、Cursor、Neovim配置指南

Resources

资源