Add UseWalrusOperator linting rule to detect assignment-then-truthiness patterns#19110
Merged
Add UseWalrusOperator linting rule to detect assignment-then-truthiness patterns#19110
Conversation
Co-authored-by: harupy <17039389+harupy@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Update documentation files for project clarity
Add UseWalrusOperator linting rule to detect assignment-then-truthiness patterns
Nov 29, 2025
- Simplify UseWalrusOperator rule by removing nested scope handling - Remove test_variable_used_after_in_nested_scope test case - Apply walrus operator pattern to 58 locations across the codebase 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: harupy <17039389+harupy@users.noreply.github.com>
Contributor
|
Documentation preview for 53b07e3 is available at: More info
|
Signed-off-by: harupy <17039389+harupy@users.noreply.github.com>
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: harupy <17039389+harupy@users.noreply.github.com>
f46837a to
837e983
Compare
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: harupy <17039389+harupy@users.noreply.github.com>
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: harupy <17039389+harupy@users.noreply.github.com>
- Refactor rule to use WalrusOperatorVisitor for cleaner code structure - Add support for nested if, for, while, with, try blocks and match statements - Add visit_AsyncFor for async for loops - Add 5 new tests for nested block scenarios - Fix 50 new violations found across the codebase 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: harupy <17039389+harupy@users.noreply.github.com>
harupy
approved these changes
Nov 29, 2025
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.
🛠 DevTools 🛠
Install mlflow from this PR
For Databricks, use the following command:
What changes are proposed in this pull request?
Implements a new clint linting rule
UseWalrusOperator(MLF0046) that detects patterns where a variable is assigned and immediately tested for truthiness in the followingifstatement, suggesting the use of Python's walrus operator (:=) instead.Pattern detected:
Also supports
if/elif/else- flags when the variable is only used in theifbody:Implementation:
use_walrus_operator.py: Rule class with AST-based pattern detectionlinter.py: Added_check_stmts_for_walrus()method, integrated intovisit_FunctionDef,visit_AsyncFunctionDefrules/__init__.py: Registered new ruleHow is this PR tested?
25 test cases covering positive matches and edge cases (elif/else with/without var usage, variable reuse, line length, etc.)
Does this PR require documentation update?
Release Notes
Is this a user-facing change?
How 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?