Skip to content

WPS332: allow walrus operator#3592

Merged
sobolevn merged 5 commits intowemake-services:masterfrom
j2cry:issue-3505
Feb 3, 2026
Merged

WPS332: allow walrus operator#3592
sobolevn merged 5 commits intowemake-services:masterfrom
j2cry:issue-3505

Conversation

@j2cry
Copy link
Copy Markdown
Contributor

@j2cry j2cry commented Feb 1, 2026

I have made things!

Checklist

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made
  • I have added my changes to the CHANGELOG.md

Related issues

Closes #3505

class WalrusViolation(ASTViolation):
"""
Forbid the use of the walrus operator (`:=`) outside of comprehensions.
Forbid the use of the walrus operator (`:=`) in most cases.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, make a list below when we allow to use :=. Just a simple sentence for a case.

Comment on lines +1318 to +1319
Avoid using the walrus operator outside comprehensions
or ``while`` conditions.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Avoid using the walrus operator outside comprehensions
or ``while`` conditions.
Avoid using the walrus operator outside
of specific places where it fits.

Comment on lines +1329 to +1332
# Correct, but with care
while some := call():
print(some)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Correct, but with care
while some := call():
print(some)

This is not needed :)


error_template = 'Found walrus operator outside a comprehension'
error_template = (
'Found walrus operator outside a comprehension or `while` condition'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'Found walrus operator outside a comprehension or `while` condition'
'Found improper use of a walrus operator'

if some:
...
"""
correct_walrus_while_condition = """
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add a test that := is not allowed inside while's body.

...
"""
correct_walrus_while_condition2 = """
while any(some := call()):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not right :)
We only allow top-level use.

Please, move this example to the "wrong" category.


Walrus operator is allowed inside:
- comprehensions
- ``while`` conditions
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- ``while`` conditions
- top level ``while`` conditions

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (5198e40) to head (c87ee91).
⚠️ Report is 41 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #3592   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          369       369           
  Lines        12290     12296    +6     
  Branches       848       849    +1     
=========================================
+ Hits         12290     12296    +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines +231 to +236
_allowed_parents: ClassVar[AnyNodes] = (
ast.ListComp,
ast.SetComp,
ast.DictComp,
ast.GeneratorExp,
ast.While,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_allowed_parents: ClassVar[AnyNodes] = (
ast.ListComp,
ast.SetComp,
ast.DictComp,
ast.GeneratorExp,
ast.While,
_comprehensions: ClassVar[AnyNodes] = (
ast.ListComp,
ast.SetComp,
ast.DictComp,
ast.GeneratorExp,

Comment on lines +251 to +256
allowed_parent = walk.get_closest_parent(node, self._allowed_parents)
if not allowed_parent or (
isinstance(allowed_parent, ast.While)
and node is not allowed_parent.test
):
self.add_violation(consistency.WalrusViolation(node))
Copy link
Copy Markdown
Member

@sobolevn sobolevn Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
allowed_parent = walk.get_closest_parent(node, self._allowed_parents)
if not allowed_parent or (
isinstance(allowed_parent, ast.While)
and node is not allowed_parent.test
):
self.add_violation(consistency.WalrusViolation(node))
is_comprension = walk.get_closest_parent(node, self._comprehensions)
if is_comprension:
return
is_while_cond = isinstance(get_parent(node), ast.While) and node is allowed_parent.test
if is_while_cond:
return
self.add_violation(consistency.WalrusViolation(node))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allowed_parent is unknown in your suggestion. But I agree, separate conditions look better.

Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff, thanks a lot!

@sobolevn sobolevn closed this Feb 2, 2026
@sobolevn sobolevn reopened this Feb 2, 2026
@sobolevn
Copy link
Copy Markdown
Member

sobolevn commented Feb 2, 2026

(CI is stuck)

@j2cry
Copy link
Copy Markdown
Contributor Author

j2cry commented Feb 3, 2026

Thanks for your help!

@sobolevn sobolevn closed this Feb 3, 2026
@sobolevn sobolevn reopened this Feb 3, 2026
@sobolevn sobolevn merged commit 459c7a3 into wemake-services:master Feb 3, 2026
7 of 17 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.

Allow the walrus operator to be used in the while loop in WPS332

2 participants