fix: resolve walrus operator name lookup in comprehension filter (fix…
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
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅
@rchen152 has imported this pull request. If you are a Meta employee, you can view this in D105671274.
@rchen152 merged this pull request in facebook/pyrefly@15c4a2913be18266f80181e1cbb5c62aa6dfbd7f.