facebook / facebook/pyrefly

False positive redundant-condition on use of nonlocal

Open
#3,871 1 comment 0 reactions 0 assignees View on GitHub
needs-discussion typechecking
Dominant language
Rust
Stars
7k
Forks
516
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

The following program with pyrefly 1.1.0 and `--min-severity=warn` gives the error:

` WARN String literal used as condition. It's equivalent to 'False' [redundant-condition]`

However, actually running the program shows that the condition is definitely not false.

I'm guessing the use of `nonlocal` is confusing pyrefly.

```
def bug():
dynamic = ''

def changedynamic():
nonlocal dynamic
dynamic = 'not empty'

changedynamic()
if dynamic:
print('If equivalent to false, then why are you seeing this?')

bug()
```

### Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSImMYABNgK4DmAFAJSIA66NPNmp6VAFsIAYxoBeGgHJpXLrz7UaogBYZGMfoJGi2nbot7pc6KLlGoofAcLEKjPbXfFTpJgC40YQ4h9Jy6A48ahpatrpswTQQtM66Bo48xABOEOgezNIAkrQwAI70EABuVjAZNB64NGBWcDAANJWq5TQA7qqkNKgpMDSkuPQ09TDpjM0QcAD80qzy6Awsc%2BggDSBkvWBQpIRVQlAUAMQ0AAqkm9vDGDgEKqaQjPQpqB4QpoRcxwDKMH2qHh5iHBEAB6EEbajbQi4FKMEHlEGYCxwEGie4QR7PV6mEE1GHdUrQVDYWB3dAPJ4vN7cXB%2BalwD7oMgeVSmAC0xRgKTg1MkNA4IAAzIQAIwAJgFXAA2lyUjC4ABdLj0dAQXwwjxaNmYCC9USvTl86QYky9QKeNm9Qq6rUAaxgpDZqFEongcCNbR66ECIAAvmtnQaYAAxaAwChoLB4IhkP1AA

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

_No response_

Contributor guide

Open the contributing guide

Research direction

Reproduce the warning with the linked sandbox or the provided Python program using pyrefly 1.1.0 and --min-severity=warn. Trace how the redundant-condition check handles the nonlocal assignment; done means the condition is no longer reported as equivalent to False while other redundant-condition diagnostics remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.