WPS332: allow walrus operator#3592
Conversation
| class WalrusViolation(ASTViolation): | ||
| """ | ||
| Forbid the use of the walrus operator (`:=`) outside of comprehensions. | ||
| Forbid the use of the walrus operator (`:=`) in most cases. |
There was a problem hiding this comment.
Please, make a list below when we allow to use :=. Just a simple sentence for a case.
| Avoid using the walrus operator outside comprehensions | ||
| or ``while`` conditions. |
There was a problem hiding this comment.
| Avoid using the walrus operator outside comprehensions | |
| or ``while`` conditions. | |
| Avoid using the walrus operator outside | |
| of specific places where it fits. |
| # Correct, but with care | ||
| while some := call(): | ||
| print(some) | ||
|
|
There was a problem hiding this comment.
| # 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' |
There was a problem hiding this comment.
| 'Found walrus operator outside a comprehension or `while` condition' | |
| 'Found improper use of a walrus operator' |
| if some: | ||
| ... | ||
| """ | ||
| correct_walrus_while_condition = """ |
There was a problem hiding this comment.
Please, add a test that := is not allowed inside while's body.
| ... | ||
| """ | ||
| correct_walrus_while_condition2 = """ | ||
| while any(some := call()): |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
| - ``while`` conditions | |
| - top level ``while`` conditions |
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
| _allowed_parents: ClassVar[AnyNodes] = ( | ||
| ast.ListComp, | ||
| ast.SetComp, | ||
| ast.DictComp, | ||
| ast.GeneratorExp, | ||
| ast.While, |
There was a problem hiding this comment.
| _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, |
| 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)) |
There was a problem hiding this comment.
| 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)) |
There was a problem hiding this comment.
allowed_parent is unknown in your suggestion. But I agree, separate conditions look better.
|
(CI is stuck) |
|
Thanks for your help! |
I have made things!
Checklist
CHANGELOG.mdRelated issues
Closes #3505