Skip to content

Commit 400fa8a

Browse files
committed
fix: WPS222 false positive for nested conditions
1 parent 88d8755 commit 400fa8a

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Semantic versioning in our case means:
2424
- Fixes false positive `WPS366` allowing the use
2525
of a single constant in `or`, #3610
2626
- Fixes the false positive `WPS330` when alternating unary operators, #3594
27+
- Fixes the false positive `WPS222` for nested conditions, #3630
2728

2829

2930
## 1.6.0

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,39 @@
4848
# Real examples:
4949

5050
complex_assignment = """
51-
some = zero and first or (second and last) or default()
51+
some = zero and first or (second and last) or default() or c or d
5252
"""
5353

5454
complex_condition = """
55-
if x == x1 and y == y1 and z == z1 or v == v1 or last():
55+
if x == x1 and y == y1 and z == z1 or v == v1 or last() or to_be() \
56+
or not_to_be():
5657
...
5758
"""
5859

60+
complex_list_comprehension = """
61+
def example(x, y):
62+
return [i for i in range(x) if i % 2 == 0 or i == y or i > 10 or \
63+
y < 4 or i != 0]
64+
"""
65+
5966
complex_while = """
60-
while (x > x1 or y < y1) or (small(z) and v) or last():
67+
while (x > x1 or y < y1) or (small(z) and v) or first() or second() or last():
6168
...
6269
"""
6370

6471
complex_match = """
6572
match some:
66-
case 1 if (x > x1 or y < y1) or (small(z) and v) or last():
73+
case 1 if (x > x1 or y < y1) or (small(z) and v) or first() or \
74+
second() or last():
6775
...
6876
"""
6977

7078
complex_gen_exp = """
7179
(
7280
...
7381
for name in []
74-
if (x > x1 or y < y1) or (small(z) and v) or last()
82+
if (x > x1 or y < y1) or (small(z) and v) or (b() and g()) or \
83+
second() or last()
7584
)
7685
"""
7786

@@ -111,6 +120,7 @@ def test_module_condition_counts_normal(
111120
[
112121
complex_assignment,
113122
complex_condition,
123+
complex_list_comprehension,
114124
complex_while,
115125
complex_match,
116126
complex_gen_exp,
@@ -138,6 +148,7 @@ def test_module_condition_real_config(
138148
[
139149
complex_assignment,
140150
complex_condition,
151+
complex_list_comprehension,
141152
complex_while,
142153
complex_match,
143154
complex_gen_exp,

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)