Skip to content

[ty] report only dead annotation-only locals as unused#24811

Merged
MichaReiser merged 2 commits intoastral-sh:mainfrom
denyszhak:ty/refine-unused-annotation-locals
Apr 24, 2026
Merged

[ty] report only dead annotation-only locals as unused#24811
MichaReiser merged 2 commits intoastral-sh:mainfrom
denyszhak:ty/refine-unused-annotation-locals

Conversation

@denyszhak
Copy link
Copy Markdown
Contributor

Closes astral-sh/ty#3322

Summary

Fixes unused-local handling for annotation-only declarations like a: int.

Test Plan

Added tests associated with the fix.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Apr 24, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 24, 2026

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 87.94%. The percentage of expected errors that received a diagnostic held steady at 83.36%. The number of fully passing files held steady at 79/133.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 24, 2026

Memory usage report

Memory usage unchanged ✅

/// without broader reference analysis. Bare local annotations (`x: int`) are also
/// reported, but only if the symbol is neither bound nor used elsewhere in the scope.
#[salsa::tracked(returns(ref))]
pub fn unused_bindings(db: &dyn Db, file: ruff_db::files::File) -> Vec<UnusedBinding> {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name is a bit misleading now because it also reports declaration only locals, not just bindings. unused_locals would better for the current shape, but I’m leaving the rename for now to keep churn down and until we have defined the scope of any future non-local reporting.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we could skip declaration only locals from reporting entirely, but for comparison, pyright also reports them.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep reporting unused locals. Renaming it to unused_locals sounds reasonable.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 24, 2026

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@denyszhak denyszhak marked this pull request as ready for review April 24, 2026 10:28
@astral-sh-bot astral-sh-bot Bot requested a review from oconnor663 April 24, 2026 10:28
@sharkdp sharkdp added the bug Something isn't working label Apr 24, 2026
Copy link
Copy Markdown
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you

/// without broader reference analysis. Bare local annotations (`x: int`) are also
/// reported, but only if the symbol is neither bound nor used elsewhere in the scope.
#[salsa::tracked(returns(ref))]
pub fn unused_bindings(db: &dyn Db, file: ruff_db::files::File) -> Vec<UnusedBinding> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep reporting unused locals. Renaming it to unused_locals sounds reasonable.

@MichaReiser MichaReiser merged commit 80feb29 into astral-sh:main Apr 24, 2026
56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"unused" false positive on declared variable with type annotation and no assignment

4 participants