mirror of
https://github.com/astral-sh/ruff.git
synced 2026-10-29 11:15:35 +08:00
This PR replaces #25027. It adds an opt-in check for imports of installed distributions that the current project does not declare directly in `pyproject.toml`. ## Overview The check uses ty's existing uv workspace integration (`TY_UV=1`) to call `uv workspace metadata --frozen --active`. uv now provides module ownership without synchronizing the environment, so ty can read the metadata without installing packages or updating `uv.lock`. The metadata provides: 1. Each workspace project's runtime and optional dependencies. 2. Dependencies declared in dependency groups. 3. Importable modules and their owning distributions, including source paths for editable installs. During import inference, ty maps each resolved import to its distribution and compares it against the importing project's direct declarations. If the declaration is missing, ty emits `missing-direct-dependency`, naming the distribution to add. Each distribution is reported once per file, at its first unsuppressed import. The rule is disabled by default and requires an existing, synchronized environment. Unavailable or incompatible dependency metadata disables the check; rejected dependency metadata produces a debug message. ## Nuances - Editable installs may lack entries in uv's module-owner map. ty uses their source paths to attribute resolved imports. - Files identified as package code require runtime or optional dependencies. Non-package files, such as tests and scripts, can also use dependency groups, but those groups' transitive dependencies still need direct declarations. - For `from package import child`, a resolved child module is checked against its own distribution rather than the parent's. Imports with unknown or ambiguous ownership are skipped. - Ownership metadata is used only when the explicitly selected Python environment matches uv's environment. - The current integration covers directory checks. Explicit file arguments, `--config-file`, and standalone PEP 723 scripts do not use the workspace's dependency declarations. The schema and rule documentation are regenerated. Most of the rule-documentation diff consists of updated source-line links.