Bringing uv and ty together
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Bringing uv and ty together
TL;DR: Experimental uv-ty integration is now available, enabling first-class support for Python scripts (on the CLI and in the editor) and a unified uv check command. This is a step towards an integrated Astral toolchain, one that uses a shared understanding of your project to enable entirely new capabilities.
简而言之:实验性的 uv-ty 集成现已推出,支持对 Python 脚本的一流支持(在 CLI 和编辑器中)以及统一的 uv check 命令。这是迈向集成 Astral 工具链的一步,该工具链利用对项目的共享理解来实现全新的功能。
At Astral, we build high-performance tools for the Python ecosystem: uv, our package manager; Ruff, our linter and formatter; and ty, our type checker and language server. Historically, each Astral tool has focused on making a different component of Python development more productive. But we see an opportunity to further Python by unifying traditionally separate capabilities into an integrated toolchain.
在 Astral,我们为 Python 生态系统构建高性能工具:我们的包管理器 uv、代码检查和格式化工具 Ruff,以及类型检查器和语言服务器 ty。从历史上看,每个 Astral 工具都专注于提高 Python 开发中不同组件的生产力。但我们看到了通过将传统上独立的功能统一为集成工具链来进一步推动 Python 的机会。
Our vision for the Astral toolchain is simple: while each tool should be individually usable, useful, and interoperable with the rest of the ecosystem, they should be even more capable when used together. Today, we’re announcing an experimental integration between uv and ty, available in preview. uv understands your Python project’s environment and dependencies, while ty understands your code. By combining the two, we can unlock new functionality beyond what either tool can provide individually.
我们对 Astral 工具链的愿景很简单:虽然每个工具都应该能够单独使用、有用并与生态系统的其他部分互操作,但它们在结合使用时应该更加强大。今天,我们宣布 uv 和 ty 之间进行实验性集成,现已提供预览版。uv 了解您的 Python 项目环境和依赖项,而 ty 了解您的代码。通过将两者结合,我们可以解锁超出任何单一工具所能提供的全新功能。
First-class support for Python scripts
对 Python 脚本的一流支持
PEP 723 enables Python scripts to declare their dependencies and Python version requirements through inline metadata. uv already uses this metadata to prepare an environment when running a script. Now, ty can use this metadata to understand your scripts. On both the command line and in the editor, ty now uses inline metadata to discover scripts throughout your project and leverages uv to prepare an environment for each script.
PEP 723 允许 Python 脚本通过内联元数据声明其依赖项和 Python 版本要求。uv 在运行脚本时已经使用此元数据来准备环境。现在,ty 可以使用此元数据来理解您的脚本。无论是在命令行还是编辑器中,ty 现在都使用内联元数据来发现整个项目中的脚本,并利用 uv 为每个脚本准备环境。
These environments allow ty to check your scripts against their declared requirements. Scripts can have different requirements within the same repository. Each script is checked against its own dependency declarations, even if those dependencies or Python requirements differ from the surrounding project.
这些环境允许 ty 根据脚本声明的要求对其进行检查。同一存储库中的脚本可以有不同的要求。每个脚本都根据其自身的依赖声明进行检查,即使这些依赖项或 Python 要求与周围的项目不同。
When the integration is enabled in your editor, ty automatically invokes uv to prepare the script’s environment in the background, which it uses to provide type checking, code navigation, and completions based on the script’s declared requirements. Over time, we plan to enable script support by default, and enable automatic environment setup for standard Python projects.
当在编辑器中启用集成时,ty 会自动在后台调用 uv 来准备脚本环境,并利用该环境根据脚本声明的要求提供类型检查、代码导航和补全功能。随着时间的推移,我们计划默认启用脚本支持,并为标准 Python 项目启用自动环境设置。
A unified checking command
统一的检查命令
uv check introduces a unified diagnostic interface to uv, starting with support for ty. Over time, we plan to support Ruff in uv check to unify linting and type checking into a single command. Running uv check will automatically prepare your project’s environment, install the required dependencies, and check your project with ty. It “just works.”
uv check 为 uv 引入了一个统一的诊断接口,首先从支持 ty 开始。随着时间的推移,我们计划在 uv check 中支持 Ruff,将代码检查和类型检查统一为一个命令。运行 uv check 将自动准备您的项目环境、安装所需的依赖项,并使用 ty 检查您的项目。它“开箱即用”。
(If you never run uv check, nothing changes for your uv experience; the ty binary is installed separately on first invocation.) With uv check, you can check individual packages, or an entire workspace. In a workspace, use uv check —package
(如果您从不运行 uv check,您的 uv 使用体验不会有任何改变;ty 二进制文件在首次调用时会单独安装。)使用 uv check,您可以检查单个包或整个工作区。在工作区中,使用 uv check —package
Standalone PEP 723 scripts have independent environments and are checked separately. Use uv check —script example.py to check a script using its declared requirements. uv check automatically uses the ty version recorded in your project’s lockfile. Commit your project configuration and lockfile, and developers and CI running uv check will share the same ty version without separately maintained pins.
独立的 PEP 723 脚本拥有独立的环境并被单独检查。使用 uv check —script example.py 可以根据其声明的要求检查脚本。uv check 会自动使用您项目锁文件中记录的 ty 版本。提交您的项目配置和锁文件后,运行 uv check 的开发人员和 CI 将共享相同的 ty 版本,而无需单独维护版本锁定。
The ty pre-commit hook also uses uv check under the hood to run ty before each commit over your Python project. By using uv check, the hook automatically sets up the environment for ty from your project’s dependency declarations.
ty 的 pre-commit 钩子也在底层使用 uv check,以便在每次提交 Python 项目之前运行 ty。通过使用 uv check,该钩子会自动根据项目的依赖声明为 ty 设置环境。
How it works
工作原理
The uv-ty integration is powered by uv workspace metadata, which gives ty a structured view of your project or script’s packages, dependencies, and Python environment. ty then uses this information to provide type checking and editor features that reflect the configured environment. For PEP 723 scripts, ty invokes uv to prepare the script’s environment, which includes installing declared dependencies.
uv-ty 集成由 uv 工作区元数据驱动,它为 ty 提供了项目或脚本的包、依赖项和 Python 环境的结构化视图。然后,ty 使用此信息提供反映配置环境的类型检查和编辑器功能。对于 PEP 723 脚本,ty 会调用 uv 来准备脚本环境,其中包括安装声明的依赖项。
As dependency installation can require execution of third-party Python code, automatic environment setup is currently opt-in. To try the integration for PEP 723 scripts in VS Code, install uv and the ty VS Code extension, then enable the uv integration in your VS Code settings.json.
由于依赖项安装可能需要执行第三方 Python 代码,因此自动环境设置目前需要手动开启。要在 VS Code 中尝试 PEP 723 脚本的集成,请安装 uv 和 ty VS Code 扩展,然后在 VS Code 的 settings.json 中启用 uv 集成。
{ “ty.experimental.useUv”: “on” }
{ “ty.experimental.useUv”: “on” }
You can also check your project and its PEP 723 scripts from the command line with: TY_UV=scripts ty check. Running uv check enables the integration automatically and requires no additional configuration. Support for PEP 723 scripts and the uv check command requires uv 0.12.3 or later, and ty 0.0.76 or later.
您还可以通过命令行使用 TY_UV=scripts ty check 来检查您的项目及其 PEP 723 脚本。运行 uv check 会自动启用集成,无需额外配置。支持 PEP 723 脚本和 uv check 命令需要 uv 0.12.3 或更高版本,以及 ty 0.0.76 或更高版本。
Up next
下一步计划
This integration is experimental, and we’d love your feedback as we refine the experience. While this initial support largely leverages uv’s environment capabilities to power ty execution, we’re also working on a new category of checks that combine uv’s knowledge of your project’s dependencies with ty’s understanding of your code (e.g., to detect unused dependencies), which we’ll cover in a future post. And, in parallel, we’re continuing toward ty’s stable release. Thank you for building with us!
此集成尚处于实验阶段,我们期待您在完善体验过程中的反馈。虽然最初的支持主要利用 uv 的环境能力来驱动 ty 的执行,但我们也在开发一类新的检查功能,将 uv 对项目依赖项的了解与 ty 对代码的理解相结合(例如,检测未使用的依赖项),我们将在以后的文章中介绍。同时,我们也在继续推进 ty 的稳定版本发布。感谢与我们共同构建!