mcpField
T
ty-type-checking

ty-type-checking

github.com/laurigates测试与质量5941 次安装1 天前更新
代码仓库

关于 ty-type-checking

ty-type-checking 是一项介绍 Astral 的 ty Python 类型检查器的技能,面向配置项目或编辑器类型检查的 Python 开发者,涵盖命令、规则、LSP、CI 及从 mypy 或 Pyright 迁移。

主要功能

  1. 01检查 Python 项目和文件的类型
  2. 02设置规则级别与排除文件
  3. 03通过 ty.toml 和 pyproject.toml 配置
  4. 04用于编辑器集成的 LSP 功能
  5. 05CI 与 pre-commit 设置指导
  6. 06从 mypy 和 Pyright 迁移指导

适用场景

  1. 01检查 Python 项目或模块的类型
  2. 02将类型检查接入 CI
  3. 03在现有代码库中逐步启用检查
  4. 04配置编辑器类型检查工作流

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/laurigates/claude-plugins --skill ty-type-checking

安装后在 Claude Code 中输入/ty-type-checking确认已加载。

详细介绍

ty 类型检查

使用 Astral(uv 和 ruff 的创建者)推出的极速 Python 类型检查器 ty 的专业知识。

何时使用此技能

在以下情况下使用此技能……在以下情况下改用 basedpyright……在以下情况下改用 mypy……
想要最快的类型检查速度(快 10-100 倍)需要开箱即用的最严格默认配置需要丰富的插件生态系统
使用 Astral 工具链(uv、ruff)想要 Microsoft 支持的替代方案有旧版 mypy 配置
需要出色的诊断信息需要兼容 Pylance需要 mypy 专用插件
想要增量/监视模式团队更偏好 Pyright 约定需要 Django/Pydantic mypy 插件
正在设置新的 Python 项目已在使用 basedpyright已有 mypy 经验

核心优势

ty 的优势

  • 极速(比 mypy 和 Pyright 快 10-100 倍)
  • 使用 Rust 编写,性能出色
  • 一流的诊断信息,灵感来自 Rust 编译器
  • 针对 IDE 工作流优化的增量分析
  • 内置 LSP,支持编辑器集成
  • 一流的交叉类型支持
  • 高级类型收窄和可达性分析
  • 属于 Astral 生态系统(uv、ruff)

安装

通过 uv(推荐)
# Install globally as tool
uv tool install ty@latest

# Run without installing
uvx ty check

# Install as dev dependency
uv add --dev ty
通过 pip
pip install ty
VS Code 扩展

从 VS Code 插件市场安装 astral-sh.ty 扩展。

基本用法

类型检查
# Check current directory
ty check

# Check specific files or directories
ty check src/
ty check src/ tests/
ty check path/to/file.py

# Verbose output
ty check --verbose

# Hide progress indicators
ty check --hide-progress
输出控制
# Default output with diagnostics
ty check

# Hide progress spinners (useful for CI)
ty check --hide-progress

# Verbose mode for debugging
ty check --verbose

配置

pyproject.toml
[tool.ty]
# Python version targeting
python-version = "3.12"

# Files to exclude
exclude = [
    "**/__pycache__",
    "**/.venv",
    "**/node_modules",
    "tests/fixtures/**",
]

[tool.ty.rules]
# Configure rule severity: "error", "warn", or "ignore"
index-out-of-bounds = "error"
possibly-unbound = "warn"
unknown-type = "ignore"
ty.toml(独立配置)
# ty.toml takes precedence over pyproject.toml
python-version = "3.12"

exclude = [
    "**/__pycache__",
    "**/.venv",
]

[rules]
index-out-of-bounds = "error"
possibly-unbound = "warn"
配置优先级
  1. 命令行参数(最高优先级)
  2. 当前目录中的 ty.toml
  3. pyproject.toml 的 [tool.ty] 部分
  4. 用户配置:~/.config/ty/ty.toml(Linux)或 %APPDATA%\ty\ty.toml(Windows)
  5. ty 默认配置(最低优先级)

CLI 选项

核心选项
标志说明
--python <VERSION>要使用的 Python 环境/版本
--project <PATH>在指定项目目录中运行
--config <PATH>ty.toml 配置文件的路径
--verbose启用详细输出
--hide-progress隐藏进度旋转图标/进度条
规则配置
标志说明
--error <RULE>将规则设为错误级别
--warn <RULE>将规则设为警告级别
--ignore <RULE>完全忽略规则
文件选择
标志说明
--exclude <PATTERN>要排除文件的 Glob 模式
--respect-ignore-files遵循 .gitignore(默认)
--no-respect-ignore-files忽略 .gitignore 文件

编辑器集成

VS Code
  1. 安装扩展:astral-sh.ty
  2. ty 提供 LSP,支持:
    • 实时类型检查
    • 跳转到定义
    • 带类型提示的自动补全
    • 自动导入建议
    • 重命名符号
    • 内嵌提示
    • 语义高亮
Neovim
-- Using nvim-lspconfig
require("lspconfig").ty.setup({
  settings = {
    ty = {
      -- Configuration options
    }
  }
})
PyCharm

ty 提供 PyCharm 插件,用于 IDE 集成。

CI 集成

GitHub Actions
name: Type Check

on: [push, pull_request]

jobs:
  type-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v2
        with:
          enable-cache: true

      - name: Run ty
        run: uvx ty check --hide-progress
Pre-commit 钩子
# .pre-commit-config.yaml
repos:
  - repo: https://github.com/astral-sh/ty
    rev: v0.0.10
    hooks:
      - id: ty
GitLab CI
ty-check:
  stage: test
  image: python:3.12
  before_script:
    - pip install ty
  script:
    - ty check --hide-progress

常见模式

快速类型检查
# Fast check of current project
ty check

# Check specific module
ty check src/api/

# Check with custom Python version
ty check --python 3.11
配置规则严重级别
# Make specific rules errors
ty check --error index-out-of-bounds --error possibly-unbound

# Ignore noisy rules
ty check --ignore unknown-type

# Mix severities
ty check --error division-by-zero --warn possibly-unbound --ignore unknown-type
排除文件
# Exclude test fixtures
ty check --exclude "tests/fixtures/**"

# Exclude multiple patterns
ty check --exclude "**/*_test.py" --exclude "**/conftest.py"

# Don't respect .gitignore
ty check --no-respect-ignore-files

与其他类型检查器的比较

性能
类型检查器相对速度备注
ty1 倍(基准)最快,基于 Rust
Pyright慢 10-60 倍性能良好
mypy慢 10-100 倍速度较慢,但成熟

在编辑器中(编辑文件后):

  • ty:约 5ms 重新计算诊断信息
  • Pyright:约 400ms
  • mypy:数秒
功能对比
功能tybasedpyrightmypy
速度最快快中等
LSP内置内置dmypy
诊断优秀良好基础
插件系统有限有限丰富
交叉类型一等支持部分支持有限
生态系统Astral(uv、ruff)Microsoft独立

迁移

从 mypy 迁移
# mypy.ini / [tool.mypy]
[mypy]
python_version = 3.12
strict = true
warn_unused_ignores = true

# Equivalent ty configuration
[tool.ty]
python-version = "3.12"

[tool.ty.rules]
# Configure equivalent strictness via rules
从 Pyright/basedpyright 迁移
# [tool.pyright] or [tool.basedpyright]
[tool.basedpyright]
pythonVersion = "3.12"
typeCheckingMode = "strict"

# Equivalent ty configuration
[tool.ty]
python-version = "3.12"

[tool.ty.rules]
# Configure strictness via rules

最佳实践

1. 与 Astral 工具链搭配使用
# Combine with uv and ruff for complete workflow
uv init my-project && cd my-project
uv add --dev ty ruff
uv run ty check
uv run ruff check --fix
2. 配置项目级设置
# pyproject.toml - recommended setup
[tool.ty]
python-version = "3.12"
exclude = [
    "**/__pycache__",
    "**/.venv",
    "**/build",
    "**/dist",
]

[tool.ty.rules]
# Start with defaults, adjust as needed
3. 在 CI 中隐藏进度
# Always use --hide-progress in CI for cleaner logs
ty check --hide-progress
4. 渐进式采用
# Start by checking new code only
ty check src/new_module/

# Gradually expand coverage
ty check src/

Agentic 优化

场景命令
快速检查ty check
CI 检查ty check --hide-progress
详细调试ty check --verbose
检查模块ty check src/module/
严格错误检查ty check --error possibly-unbound
忽略规则ty check --ignore unknown-type

快速参考

必备命令
# Basic type checking
ty check                          # Check current directory
ty check src/                     # Check specific directory
ty check file.py                  # Check specific file

# Configuration
ty check --config ty.toml         # Use custom config
ty check --python 3.12            # Specify Python version

# Output control
ty check --verbose                # Verbose output
ty check --hide-progress          # No progress indicators

# Rule control
ty check --error RULE             # Treat as error
ty check --warn RULE              # Treat as warning
ty check --ignore RULE            # Ignore rule

# File selection
ty check --exclude "pattern"      # Exclude files
最简配置
# pyproject.toml
[tool.ty]
python-version = "3.12"
exclude = ["**/__pycache__", "**/.venv"]

另请参阅

  • basedpyright-type-checking - 默认设置更严格的替代类型检查器
  • ruff-linting - 来自同一生态系统的快速代码检查
  • ruff-formatting - 来自同一生态系统的快速格式化
  • python-development - 通用 Python 开发模式

参考资料

评论

支持 Markdown