facebook / facebook/pyrefly

Low-priority feature request: narrow using dynamic `locals()` function

Open
#2,438 0 comments 0 reactions 0 assignees View on GitHub
narrowing needs-discussion
Dominant language
Rust
Stars
7k
Forks
516
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

I would like to request the feature that pyrefly understands "if "variable" in locals()" and does not give unbound-name errors in such cases (see below):

```python
def fun(arg):
if arg==True:
variable=1
if "variable" in locals():
print(variable) # gives unbound-name error: `variable` may be uninitialized
```

### Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSImMYABGAK7oAUqATgOYCUiAOujQJoRabdgF4xAFVb0YvfoMUA3NhFTZYYgIx9FwmjxArWajTEND%2BUXAGNUUOE267Fg4ifQAXJsdOxONADENOwQSvA0jHiMmAC06KgAtjA0MKysuKyINAAGvuqwOTSJqKQ02CmMEOgQnmpQEABeMJggADQg9HUOJOSIIMEAqt21ZQzoNnW46HB8fFS0YJklngD66PSJFaxM%2BNnVngGxAHw0cJ5ZLgKsMJ70rPxghgBym9vZwPgAvoZ87SBkG5gKCkQieXCJKAUYIABVIQJBZwwOAINBs00g7HuqCm6EIfGCAGUYCkABaeTzEOCIAD0NMB1BBhEy7BpMHQNMwtjgNPR6Ex2NxNLomRoqBU0AKKT5AtYOIg0xouGIuLg%2BPQZE8pOmsXCrDgCv4YgMIAAzIQtAAmX7oEBfDqoSZhGAAMWgMAoaCweCIZDtQA

### (Only applicable for extension issues) IDE Information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.