Skip to content

[ty] Add an unused-dependency rule - #28192

Draft
zsol wants to merge 6 commits into
zsol/ty-shared-manifest-diagnosticsfrom
zsol/ty-unused-dependency
Draft

zsol wants to merge 6 commits into
zsol/ty-shared-manifest-diagnosticsfrom
zsol/ty-unused-dependency

Conversation

@zsol

@zsol zsol commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Summary

Add an opt-in unused-dependency rule that reports dependencies left in pyproject.toml or a PEP 723 script after their last import is removed. Enable it with --warn unused-dependency and uv integration. ty uses uv's metadata to match imports to the distributions that provide them.

The PR caches dependency usage separately from diagnostic reporting, reuses the existing mdtest assertions and snapshots with shared uv fixtures, and updates the rule documentation and schema.

Stacked on #29674.

Rule behavior

  • Project files. ty counts imports in every file selected by src.include, src.exclude, and ignore settings, including files that have never been opened in the editor. A dependency used only in an excluded file is reported as unused, even if that file is open or an included file imports it.
  • Partial checks. If app/ is a workspace member, ty check app/foo skips unused-dependency for app/pyproject.toml because other files in the member may import the dependency. Checking a single Python file also skips the project rule. ty check app runs it even when src.include selects only foo/. We might want to tweak this later based on feedback
  • Scripts. ty counts imports in a PEP 723 script and in any local modules it imports (directly or indirectly). The author hereby officially declares his contempt for PEP 723 scripts that import local modules
  • Dependency declarations. ty checks runtime and optional dependencies and skips dependency groups (We might want to tweak this later). It skips a package if uv lists no runtime modules for it. A stub-only package can therefore stay declared without a warning after you remove the last import of the library it describes (We might want to tweak this, too, later).
  • Alternative versions or sources. When a dependency resolves to different versions (e.g. based on platform or python version), ty skips the declaration because uv only reports which modules the installed alternative provides. We should improve this in the future
  • Dynamic imports. If ty recognizes a dynamic import but cannot tell which module it loads, it suppresses all unused-dependency warnings for that workspace member or script.
  • Editors. A background project check updates warnings on closed pyproject.toml files when Python source changes. When two editor folders share a pyproject.toml, unsaved imports from either folder count toward its declarations. A workspace diagnostic request combines the warnings into one report for the shared manifest.

@zsol zsol added the ty The ty type checker label Aug 31, 2026
@astral-sh-bot

astral-sh-bot Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 98.24%. The percentage of expected errors that received a diagnostic held steady at 98.24%. The number of fully passing files held steady at 134/146.

@astral-sh-bot

astral-sh-bot Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@astral-sh-bot

astral-sh-bot Bot commented Aug 31, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Aug 31, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@zsol
zsol force-pushed the zsol/ty-unused-dependency branch from 03593e6 to 08ac708 Compare September 4, 2026 12:35
zsol added a commit to astral-sh/uv that referenced this pull request Oct 15, 2026
`uv workspace metadata` omits installed packages outside the selected
resolution. For example, a package can remain installed after it is
removed from the dependency graph; this is especially important for
astral-sh/ruff#28192.

This PR adds observed distributions in `environment.packages`, including
their installed versions, metadata paths, and editable status. The
inventory includes installed packages outside the locked resolution.

Installed distributions are matched to selected non-virtual packages by
name (i.e. they're matched even when their versions differ). Matched
entries reuse the selected resolution node ID and report the actual
installed version separately. Only unmanaged entries get generated IDs
based on their installed name, version, and metadata path. If multiple
installations match the same resolution node, uv keeps the first record
and warns.

The affected fields, expressed as TypeScript types:

```ts
type NodeId = string; // Opaque identifier.

type InstalledPackage = {
  name: string;
  version: string; // Actual installed version.
  path: string; // Absolute path to installed metadata.
  editable: boolean;
};

type Metadata = {
  // ... (other metadata fields omitted)
  environment?: {
    // Existing root and python fields omitted.
    packages: Record<NodeId, InstalledPackage>; // New; present even when empty.
  };

  // Existing. Module name -> owners; omitted when empty.
  // package_id indexes environment.packages;
  // matched IDs also index resolution.
  module_owners?: Record<string, { package_id: NodeId }[]>;
};
```

Unmanaged packages are entries in `environment.packages` whose IDs are
absent from `resolution`. Use `module_owners` to find the modules they
provide. Unmatched packages without discovered modules appear only in
`environment.packages`.

---------

Co-authored-by: Zsolt Dollenstein <[email protected]>
Co-authored-by: zaniebot <[email protected]>
@zsol
zsol force-pushed the zsol/ty-unused-dependency branch from 08ac708 to 5be4142 Compare October 16, 2026 16:27
@zsol
zsol force-pushed the zsol/ty-unused-dependency branch from 5be4142 to c3fe243 Compare October 19, 2026 14:29
@zsol
zsol force-pushed the zsol/ty-unused-dependency branch 3 times, most recently from 66436a9 to c5c24c8 Compare October 20, 2026 16:35
@zsol
zsol force-pushed the zsol/ty-unused-dependency branch from c5c24c8 to d164e4a Compare October 21, 2026 17:09
@zsol
zsol force-pushed the zsol/ty-unused-dependency branch from d164e4a to d474221 Compare October 23, 2026 16:15
@zsol
zsol changed the base branch from main to zsol/ty-shared-manifest-diagnostics October 23, 2026 16:15
@astral-automations-bot

astral-automations-bot Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 98.60%. The percentage of expected errors that received a diagnostic held steady at 98.86%. The number of fully passing files held steady at 137/146.

Results for b5484f7 · Workflow run

@astral-automations-bot

astral-automations-bot Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Memory usage report

Memory usage unchanged ✅


Results for b5484f7 · Workflow run

@astral-automations-bot

astral-automations-bot Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff · Timing results


Results for b5484f7 · Workflow run

@astral-automations-bot

astral-automations-bot Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Linter (stable) · Linter (preview) · Formatter (stable) · Formatter (preview)


Results for b5484f7 · Workflow run

@zsol
zsol added this pull request to stack #29734 October 26, 2026 16:59
@zsol
zsol force-pushed the zsol/ty-unused-dependency branch 2 times, most recently from 1c9afd8 to a67f83d Compare October 28, 2026 05:18
@zsol
zsol force-pushed the zsol/ty-unused-dependency branch from a67f83d to b5484f7 Compare October 29, 2026 05:21

This branch was previously deployed

1 inactive deployment
automations — b5484f78 Deployed Oct 29, 2026 by zsol via security-review / security review #96164
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant