Skip to content

enh: suppress false-positive unknown-name errors after short-circuit boolean ops#3588

Open
MarcoGorelli wants to merge 1 commit into
facebook:mainfrom
MarcoGorelli:fix
Open

enh: suppress false-positive unknown-name errors after short-circuit boolean ops#3588
MarcoGorelli wants to merge 1 commit into
facebook:mainfrom
MarcoGorelli:fix

Conversation

@MarcoGorelli
Copy link
Copy Markdown
Contributor

@MarcoGorelli MarcoGorelli commented May 26, 2026

Summary

Currently, in a check like

if SOMETHING_DEFINITELY_FALSE and A:
    pass

, unknown-name is raised for A, even though it's not reached. This came up in Polars, where there's a variable defined based on the Polars version, but

if sys.version_info >= (3, 15) and A:

raises

The idea is that, if a boolean expression short-circuits based on an always-true statement (if the boolean expression is or) or an always-false one (if the boolean expression is and), then the code after it is unreachable and so unknown-name shouldn't be reported

The check for short-circuiting needs to be done:

  • for the first value in the boolean op (values.next)
  • the subsequent values (when iterating over values)

AI disclosure: I used some assistance from Claude

Fixes #3554

Test Plan

This PR includes tests. The mypy-primer also shows no effects

@github-actions

This comment has been minimized.

@github-actions
Copy link
Copy Markdown

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

@MarcoGorelli MarcoGorelli marked this pull request as ready for review May 27, 2026 10:20
@MarcoGorelli MarcoGorelli marked this pull request as draft May 27, 2026 10:20
@MarcoGorelli MarcoGorelli changed the title WIP enh: suppress false-positive unknown-name errors after short-circuit boolean ops enh: suppress false-positive unknown-name errors after short-circuit boolean ops May 27, 2026
@MarcoGorelli MarcoGorelli marked this pull request as ready for review May 27, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

short circuit based on sys.version_info in bool op

1 participant