Skip to content

Add UseWalrusOperator linting rule to detect assignment-then-truthiness patterns#19110

Merged
harupy merged 8 commits intomasterfrom
copilot/update-documentation-files
Nov 29, 2025
Merged

Add UseWalrusOperator linting rule to detect assignment-then-truthiness patterns#19110
harupy merged 8 commits intomasterfrom
copilot/update-documentation-files

Conversation

Copy link
Contributor

Copilot AI commented Nov 29, 2025

🛠 DevTools 🛠

Open in GitHub Codespaces

Install mlflow from this PR

# mlflow
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/19110/merge
# mlflow-skinny
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/19110/merge#subdirectory=libs/skinny

For Databricks, use the following command:

%sh curl -LsSf https://raw.githubusercontent.com/mlflow/mlflow/HEAD/dev/install-skinny.sh | sh -s pull/19110/merge

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 following if statement, suggesting the use of Python's walrus operator (:=) instead.

Pattern detected:

# Before (flagged)
result = func()
if result:
    use(result)

# After (suggested)
if result := func():
    use(result)

Also supports if/elif/else - flags when the variable is only used in the if body:

# Flagged (var not used in else)
if result := func():
    use(result)
else:
    do_other()

Implementation:

  • use_walrus_operator.py: Rule class with AST-based pattern detection
    • Skips comparisons, tuple unpacking, multi-line assignments
    • Skips if variable is used in elif/else branches or after the if statement
    • Respects 100-char line limit
    • Only checks inside functions (not module-level) for performance
  • linter.py: Added _check_stmts_for_walrus() method, integrated into visit_FunctionDef, visit_AsyncFunctionDef
  • rules/__init__.py: Registered new rule
  • Applied walrus operator pattern to 67 locations across the codebase

How is this PR tested?

  • Existing unit/integration tests
  • New unit/integration tests

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?

  • No. You can skip the rest of this section.

Release Notes

Is this a user-facing change?

  • No. You can skip the rest of this section.

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" section

Should this PR be included in the next patch release?

  • Yes (this PR will be cherry-picked and included in the next patch release)
  • No (this PR will be included in the next minor release)

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
Copilot AI requested a review from harupy November 29, 2025 07:19
@harupy harupy marked this pull request as ready for review November 29, 2025 07:20
@github-actions github-actions bot added the rn/none List under Small Changes in Changelogs. label Nov 29, 2025
@harupy harupy added the only-latest If applied, only test the latest version of each group in cross-version tests. label 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>
@github-actions
Copy link
Contributor

github-actions bot commented Nov 29, 2025

Documentation preview for 53b07e3 is available at:

More info
  • Ignore this comment if this PR does not change the documentation.
  • The preview is updated when a new commit is pushed to this PR.
  • This comment was created by this workflow run.
  • The documentation was built by this workflow run.

harupy and others added 2 commits November 29, 2025 16:44
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>
@harupy harupy force-pushed the copilot/update-documentation-files branch from f46837a to 837e983 Compare November 29, 2025 08:22
harupy and others added 3 commits November 29, 2025 17:26
🤖 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 harupy merged commit 24d3357 into master Nov 29, 2025
117 of 121 checks passed
@harupy harupy deleted the copilot/update-documentation-files branch November 29, 2025 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

only-latest If applied, only test the latest version of each group in cross-version tests. rn/none List under Small Changes in Changelogs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants