Skip to content

Commit 3771f75

Browse files
authored
fix: WPS222 false positive for nested conditions (#3633)
1 parent 88d8755 commit 3771f75

4 files changed

Lines changed: 32 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ Semantic versioning in our case means:
1616
But, in the future we might change the configuration names/logic,
1717
change the client facing API, change code conventions significantly, etc.
1818

19+
## WIP
20+
21+
### Bugfixes
22+
23+
- Fixes the false positive `WPS222` for nested conditions, #3630
24+
1925

2026
## 1.6.1
2127

tests/test_visitors/test_ast/test_complexity/test_counts/test_condition_counts.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,48 @@
4545
print(1)
4646
"""
4747

48+
# 3 conditions, nested conditions are not counted.
49+
if_with_nested_conditions = """
50+
if (a is None or (func1(a) and b) or (func2(a) and c)):
51+
...
52+
"""
53+
4854
# Real examples:
4955

5056
complex_assignment = """
51-
some = zero and first or (second and last) or default()
57+
some = zero and first or (second and last) or default() or c or d
5258
"""
5359

5460
complex_condition = """
55-
if x == x1 and y == y1 and z == z1 or v == v1 or last():
61+
if x == x1 and y == y1 and z == z1 or v == v1 or last() or to_be() \
62+
or not_to_be():
5663
...
5764
"""
5865

66+
complex_list_comprehension = """
67+
def example(x, y):
68+
return [i for i in range(x) if i % 2 == 0 or i == y or i > 10 or \
69+
y < 4 or i != 0]
70+
"""
71+
5972
complex_while = """
60-
while (x > x1 or y < y1) or (small(z) and v) or last():
73+
while (x > x1 or y < y1) or (small(z) and v) or first() or second() or last():
6174
...
6275
"""
6376

6477
complex_match = """
6578
match some:
66-
case 1 if (x > x1 or y < y1) or (small(z) and v) or last():
79+
case 1 if (x > x1 or y < y1) or (small(z) and v) or first() or \
80+
second() or last():
6781
...
6882
"""
6983

7084
complex_gen_exp = """
7185
(
7286
...
7387
for name in []
74-
if (x > x1 or y < y1) or (small(z) and v) or last()
88+
if (x > x1 or y < y1) or (small(z) and v) or (b() and g()) or \
89+
second() or last()
7590
)
7691
"""
7792

@@ -89,6 +104,7 @@
89104
condition_with_inline_for,
90105
condition_with_simple_inline_for,
91106
while_with_condition,
107+
if_with_nested_conditions,
92108
],
93109
)
94110
def test_module_condition_counts_normal(
@@ -111,6 +127,7 @@ def test_module_condition_counts_normal(
111127
[
112128
complex_assignment,
113129
complex_condition,
130+
complex_list_comprehension,
114131
complex_while,
115132
complex_match,
116133
complex_gen_exp,
@@ -138,6 +155,7 @@ def test_module_condition_real_config(
138155
[
139156
complex_assignment,
140157
complex_condition,
158+
complex_list_comprehension,
141159
complex_while,
142160
complex_match,
143161
complex_gen_exp,

wemake_python_styleguide/violations/complexity.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,8 @@ class TooManyConditionsViolation(ASTViolation):
693693
.. versionchanged:: 0.5.0
694694
.. versionchanged:: 1.4.0
695695
Added ``--max-conditions`` configuration options.
696+
.. versionchanged:: 1.7.0
697+
Stopped recursive counting of nested ``ast.BoolOp`` nodes.
696698
"""
697699

698700
error_template = 'Found a condition with too much logic: {0}'

wemake_python_styleguide/visitors/ast/complexity/counts.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,7 @@ def visit_Compare(self, node: ast.Compare) -> None:
9191
self.generic_visit(node)
9292

9393
def _count_conditions(self, node: ast.BoolOp) -> int:
94-
counter = 0
95-
for condition in node.values:
96-
if isinstance(condition, ast.BoolOp):
97-
counter += self._count_conditions(condition)
98-
else:
99-
counter += 1
100-
return counter
94+
return len(node.values)
10195

10296
def _check_conditions(self, node: ast.BoolOp) -> None:
10397
conditions_count = self._count_conditions(node)

0 commit comments

Comments
 (0)