Skip to content

Commit 295493d

Browse files
committed
fix: WPS529 false positive
1 parent 3771f75 commit 295493d

4 files changed

Lines changed: 67 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Semantic versioning in our case means:
2121
### Bugfixes
2222

2323
- Fixes the false positive `WPS222` for nested conditions, #3630
24+
- Fixes the false positive `WPS529` for dict subscripts in the `else` branch, #3501
2425

2526

2627
## 1.6.1

tests/test_visitors/test_ast/test_subscripts/test_implicit_get.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@
4444
...
4545
"""
4646

47+
else_subscripts1 = """
48+
if 'a' in kwargs:
49+
pass
50+
else:
51+
kwargs['a'] = other()
52+
"""
53+
54+
elif_subscripts1 = """
55+
if 'b' in kwargs:
56+
pass
57+
elif 'a' in kwargs:
58+
print(kwargs['a'])
59+
"""
60+
4761

4862
@pytest.mark.parametrize(
4963
'template',
@@ -122,3 +136,45 @@ def test_correct_if(
122136
visitor.run()
123137

124138
assert_errors(visitor, [])
139+
140+
141+
@pytest.mark.parametrize(
142+
'code',
143+
[
144+
else_subscripts1,
145+
],
146+
)
147+
def test_no_implicit_dict_get_in_else_branch(
148+
assert_errors,
149+
parse_ast_tree,
150+
default_options,
151+
code,
152+
):
153+
"""Testing that subscripts in `else` branch do not trigger violation."""
154+
tree = parse_ast_tree(code)
155+
156+
visitor = ImplicitDictGetVisitor(default_options, tree=tree)
157+
visitor.run()
158+
159+
assert_errors(visitor, [])
160+
161+
162+
@pytest.mark.parametrize(
163+
'code',
164+
[
165+
elif_subscripts1,
166+
],
167+
)
168+
def test_implicit_dict_get_in_elif_branch(
169+
assert_errors,
170+
parse_ast_tree,
171+
default_options,
172+
code,
173+
):
174+
"""Testing that subscripts in `elif` branch still trigger violation."""
175+
tree = parse_ast_tree(code)
176+
177+
visitor = ImplicitDictGetVisitor(default_options, tree=tree)
178+
visitor.run()
179+
180+
assert_errors(visitor, [ImplicitDictGetViolation])

wemake_python_styleguide/violations/refactoring.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,9 @@ class ImplicitDictGetViolation(ASTViolation):
11351135
print(collection[key])
11361136
11371137
.. versionadded:: 0.13.0
1138-
1138+
.. versionadded:: 1.7.0
1139+
Excluded the `else` branch, check scope changed from
1140+
the whole ast.If to node.body.
11391141
"""
11401142

11411143
error_template = 'Found implicit `.get()` dict usage'

wemake_python_styleguide/visitors/ast/subscripts.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ast
2+
from collections.abc import Iterator
23
from typing import ClassVar, final
34

45
from wemake_python_styleguide.logic import source
@@ -127,6 +128,11 @@ def visit_If(self, node: ast.If) -> None:
127128
self._check_implicit_get(node)
128129
self.generic_visit(node)
129130

131+
def _walk_body(self, body: list[ast.stmt]) -> Iterator[ast.AST]:
132+
"""Yields all nodes strictly within a list of statements."""
133+
for stmt in body:
134+
yield from ast.walk(stmt)
135+
130136
def _check_implicit_get(self, node: ast.If) -> None:
131137
if not isinstance(node.test, ast.Compare):
132138
return
@@ -136,7 +142,7 @@ def _check_implicit_get(self, node: ast.If) -> None:
136142
checked_key = source.node_to_string(node.test.left)
137143
checked_collection = source.node_to_string(node.test.comparators[0])
138144

139-
for sub in ast.walk(node):
145+
for sub in self._walk_body(node.body):
140146
if not isinstance(sub, ast.Subscript):
141147
continue
142148

0 commit comments

Comments
 (0)