pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

fix: resolve walrus operator name lookup in comprehension filter (fix…

Open f1sherFM opened this issue 6 months ago • 2 comments

Summary

Fixed false positive "X is uninitialized" errors when walrus operator (:=) in comprehension filter reads a variable from the enclosing function scope.

Root cause: In look_up_name_for_read, when a name was found via static lookup in the comprehension scope (added via add_lvalue_to_current_static), the function returned InitializedInFlow::No without checking enclosing scopes. This caused false positive errors for parameters or local variables from enclosing scopes.

Fix: For comprehension scopes, check enclosing scopes via flow lookup before returning static lookup result. If the name exists in an enclosing scope's flow, use that instead of the comprehension scope's static info.

Fixes #3398

Test Plan

Added test test_walrus_in_comprehension_reads_enclosing_scope_param in pyrefly/lib/test/scope.rs reproducing the bug All existing scope and comprehension tests pass Ran python3 test.py --no-test --no-conformance --no-jsonschema — formatting and linting passed successfully

f1sherFM avatar May 16 '26 16:05 f1sherFM

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

github-actions[bot] avatar May 19 '26 08:05 github-actions[bot]

@rchen152 has imported this pull request. If you are a Meta employee, you can view this in D105671274.

meta-codesync[bot] avatar May 19 '26 08:05 meta-codesync[bot]

@rchen152 merged this pull request in facebook/pyrefly@15c4a2913be18266f80181e1cbb5c62aa6dfbd7f.

meta-codesync[bot] avatar May 19 '26 19:05 meta-codesync[bot]