Repository navigation
Y002: Allow nested and/or in if conditions - #557
Merged
Merged
Conversation
Recurse into nested BoolOps when checking if tests, so that conditions like `if sys.version_info >= (3, 15) and (sys.platform == "win32" or sys.platform == "linux")` are accepted. Each leaf comparison is still checked individually. Fixes PyCQA#542
|
This change has no effect on typeshed. 🤖🎉 |
JelleZijlstra
approved these changes
Oct 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #542.
visit_Ifonly flattened one level ofand/or, so a parenthesised group like(sys.platform == "win32" or sys.platform == "linux")was passed to_check_if_expressionas anast.BoolOpand rejected with Y002. This moves theBoolOphandling into_check_if_expressionso it recurses into nested groups; each leaf comparison is still checked individually, soif A and (B or sys.maxsize == 42)keeps reporting Y002 on the bad leaf only.Added the cases from the issue to
tests/comparisons.pyi(they fail onmainwith Y002 on the parenthesised group) plus a CHANGELOG entry.Verified locally with
pytest -vv(37 passed),black --check,isort --check-only,flake8andmypy --python-version=3.10, all clean.