[Analysis] Fix false positive unused-variable reporting in f-string format specifiers#3013
[Analysis] Fix false positive unused-variable reporting in f-string format specifiers#3013Arths17 wants to merge 6 commits intofacebook:mainfrom
Conversation
modified: crates/pyrefly_util/src/ruff_visitors.rs modified: pyrefly/lib/test/lsp/diagnostic.rs
modified: crates/pyrefly_util/src/ruff_visitors.rs
There was a problem hiding this comment.
Pull request overview
This PR fixes a false-positive unused-variable diagnostic by ensuring the AST visitor traverses nested expressions inside f-string format_spec nodes (e.g., dynamic width/precision), and adds a regression test for the reported case (Issue #3009).
Changes:
- Update Rust f-string traversal to recurse into
format_specinterpolations for bothVisitandVisitMut. - Add an LSP diagnostic regression test covering
f"{key:<{max_len}}"somax_lenis counted as “used”. - Ignore common Python virtualenv directories in
.gitignore.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
crates/pyrefly_util/src/ruff_visitors.rs |
Adds shared helpers to recurse through interpolation expressions and nested format_spec elements, fixing the missed-usage traversal. |
pyrefly/lib/test/lsp/diagnostic.rs |
Adds a regression test asserting no unused-variable diagnostics when a variable is used only in an f-string format specifier. |
.gitignore |
Adds .venv/ and venv/ to ignore local virtual environments. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| debug.json | ||
| debug.js | ||
| target/ | ||
| .venv/ |
There was a problem hiding this comment.
this change looks unrelated
There was a problem hiding this comment.
My apologies! I added those while setting up the dev environment on my Mac. I'll revert the .gitignore changes and keep the PR focused strictly on the f-string visitor fix.
There was a problem hiding this comment.
Reverted the .gitignore changes and pushed the update, Sorry about the noise.
|
I noticed mypy_primer (7) and test (ubuntu-latest) were failing, so I investigated the logs. The issue wasn't a crash, but rather a stale test expectation that my changes accidentally "fixed." What Changed:
Summary: This PR now correctly handles both unused-variable diagnostics and Go-To-Definition support for nested expressions in f-strings. All local tests and CI checks are now passing. |
|
@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D99678402. |
stroxler
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
|
@yangdanny97 merged this pull request in ea98e57. |
Summary
This PR resolves a false positive where variables used exclusively within an f-string format specifier (e.g., width or precision nested expressions) were incorrectly flagged as unused (Issue #3009).
The Root Cause
The AST visitor logic in pyrefly_util was previously limited to visiting the main interpolation expression of a FormattedValue node. It was not recursing into the format_spec attribute.
In an expression like f"{key:<{max_len}}", the Python AST structures max_len as a nested expression inside the format_spec. Because the visitor skipped this node, the usage collector never "saw" the reference to max_len, leading the analyzer to conclude the variable was defined but never used.
The Fix
I updated the f-string traversal logic in crates/pyrefly_util/src/ruff_visitors.rs to ensure that both the mutable and read-only visitors now explicitly descend into the format_spec node if it exists.
This ensures that any nested expressions used for dynamic formatting (alignment, padding, width, or precision) are correctly identified as variable usages.
Changes
Verification