## Summary This PR adds a new `project-manager` table: ```toml [project-manager] project = "uv" | "auto" | "none" project-mode = "sync" | "metadata" script = "uv" | "none" ``` This PR only implements the script part, but I listed all options to outline where we want to go with this in the future. `script = "uv"` opts in to installing PEP 723 script dependencies with uv (to reduce the need for `TY_UV`). For now, `project-manager.script` defaults to `"none"`, but I plan to change this to `"uv"` in the near future. The new option follows the same configuration precedence as other script settings: * A script with its own `tool.ty` section uses the value from its own table to control whether it uses uv to sync its dependencies or not. * Scripts without a `tool.ty` section use the project level `project-manager.script` For now, overriding the setting based on file patterns isn't supported (I have no strong reason against it, it's mainly to cut scope). `TY_UV` takes precedence over any setting in the configuration (similar to other CLI options that support environment variables). The recommended way to check untrusted code is to set `TY_UV=off`, which overrides `project-manager.script`. For now, we keep supporting `experimental.useUv` for scripts, but the in-editor `configuration.project-manager.script` takes precedence over `experimental.useUv`. ## Why is the setting not under `environment. For projects, setting `project-manager.project = "metadata"` also changes how we discover the project (we prefer the workspace over the closest ty project), and `metadata` doesn't initialize the Python environment. ## LSP watch mode Changing `project-manager.script` at the project level requires saving any open script (or closing and reopening) for the sync to start. This simplified the implementation a fair bit and seems an okay trade off.
3.4 KiB
What it does
Checks for imports from installable packages that the current project or PEP 723 script does not declare as direct dependencies.
The name used in dependency declarations can differ from the import name: for example, the pillow
package is imported as PIL.
Why is this bad?
A dependency can be installed because another package requires it. Importing that dependency without declaring it makes your code rely on another package's dependency list. If that package removes the dependency, your imports can fail.
Declare the packages that provide your imports in project.dependencies or
project.optional-dependencies in pyproject.toml. Non-package files, such as tests and
development scripts, can also use dependencies declared in dependency groups.
See uv's guide to managing dependencies for how to add these declarations.
Rule status
This rule is disabled by default and requires uv integration.
For projects, enable uv workspace integration (TY_UV=1) and use an existing, synchronized
environment. Running uv check synchronizes
the environment automatically before invoking ty, unless --no-sync is passed. For these checks, ty
reads the dependency graph and module ownership returned by uv workspace metadata without changing
installed packages. uv may update the lockfile to match the current dependency declarations. uv
0.12.3 or later is required.
For PEP 723 scripts, set project-manager.script = "uv" in your ty configuration. ty synchronizes
each script's environment and checks imports against its inline dependencies list. Declarations
and environments from the enclosing workspace or other scripts do not apply.
Known limitations
Imports guarded by TYPE_CHECKING are not reported because they are not executed at runtime. They
can use development-only dependencies, such as type stub packages, without requiring those packages
as runtime dependencies.
Standard-library imports and imports whose owning package cannot be identified unambiguously are also not reported.
Imports of namespace packages
themselves, such as import ns, are not reported: the namespace can contain modules from several
installable packages. Imports of their submodules, such as import ns.child, are checked when the
owning package is known. An __init__.pyi stub does not change this distinction.
Native packages that ty can resolve only as namespace packages at runtime are also skipped. For other native modules, ty can use stubs to resolve the import and uv's ownership map to identify which package to declare.
Some editable installations add the whole project directory to Python's import path, making both
package code and files such as tests/test_app.py importable. If uv does not identify which modules
belong to the installable package, ty allows dependency-group imports throughout that directory,
including in package code, to avoid incorrectly flagging imports in tests and scripts.
Examples
With requests as a direct dependency, urllib3 may also be installed because requests depends
on it:
import requests
import urllib3 # error: [missing-direct-dependency]
Add urllib3 to project.dependencies if your code imports it directly.