Skip to content

Y002: Allow nested and/or in if conditions - #557

Merged
JelleZijlstra merged 1 commit into
PyCQA:mainfrom
akashchamp:y002-nested-boolop
Oct 29, 2026
Merged

JelleZijlstra merged 1 commit into
PyCQA:mainfrom
akashchamp:y002-nested-boolop

Conversation

@akashchamp

Copy link
Copy Markdown
Contributor

Fixes #542.

visit_If only flattened one level of and/or, so a parenthesised group like (sys.platform == "win32" or sys.platform == "linux") was passed to _check_if_expression as an ast.BoolOp and rejected with Y002. This moves the BoolOp handling into _check_if_expression so it recurses into nested groups; each leaf comparison is still checked individually, so if 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 on main with Y002 on the parenthesised group) plus a CHANGELOG entry.

Verified locally with pytest -vv (37 passed), black --check, isort --check-only, flake8 and mypy --python-version=3.10, all clean.

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
@github-actions

Copy link
Copy Markdown

This change has no effect on typeshed. 🤖🎉

@JelleZijlstra
JelleZijlstra merged commit 9a41c03 into PyCQA:main Oct 29, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Y002 rejects "if x and (y or z)"

2 participants