Add clint rule to flag except X or Y boolean operator misuse#20905
Merged
Add clint rule to flag except X or Y boolean operator misuse#20905
except X or Y boolean operator misuse#20905Conversation
35 tasks
…cept handlers Co-authored-by: harupy <17039389+harupy@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add clint rule to flag except X or Y syntax
Add clint rule to flag Feb 17, 2026
except X or Y boolean operator misuse
harupy
reviewed
Feb 17, 2026
| # Bad - or in except | ||
| try: | ||
| pass | ||
| except ValueError or KeyError: |
Member
There was a problem hiding this comment.
This is a mistake in 99.9% cases
harupy
reviewed
Feb 17, 2026
|
|
||
| class ExceptBoolOp(Rule): | ||
| def _message(self) -> str: | ||
| return "Did you mean `except (X, Y):`? Using `or`/`and` in an except handler is a bug." |
Member
There was a problem hiding this comment.
- Remove backticks around and and or.
- "is a bug" -> "is likely a mistake"
Contributor
Author
There was a problem hiding this comment.
Done in 0651ff1. Updated the message to remove backticks around or/and and changed "is a bug" to "is likely a mistake".
…ikely a mistake' Co-authored-by: harupy <17039389+harupy@users.noreply.github.com>
harupy
approved these changes
Feb 17, 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.
Related Issues/PRs
What changes are proposed in this pull request?
Python's
except ValueError or KeyError:is a common mistake—theoroperator evaluates at parse time, returning the first truthy operand (ValueError), causing subsequent exception types to be silently ignored. The correct syntax isexcept (ValueError, KeyError):.This PR adds a new clint rule
except-bool-op(MLF0006) to detect this pattern:Rule implementation
dev/clint/src/clint/rules/except_bool_op.py: AST-based check forBoolOpnodes inExceptHandler.typeorandandoperators, including chained cases likeexcept X or Y or Zexcept (X, Y),except X, or bareexcept:except (X, Y):? Using or/and in an except handler is likely a mistake."Linter integration
visit_ExceptHandlermethod toLinterclassrules/__init__.pyExample detection
The rule currently flags one existing instance at
mlflow/exceptions.py:136(being fixed in #20903).How is this PR tested?
Added
dev/clint/tests/rules/test_except_bool_op.pywith:or,and, and chained operatorsAll 236 clint tests pass.
Does this PR require documentation update?
Does this PR require updating the MLflow Skills repository?
Release Notes
Is this a user-facing change?
What component(s), interfaces, languages, and integrations does this PR affect?
Components
area/build: Build and test infrastructure for MLflowHow should the PR be classified in the release notes? Choose one:
rn/none- No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" sectionShould this PR be included in the next patch release?
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.