Skip to content

Commit 844bfa7

Browse files
committed
fix: WPS529 false positive
1 parent 3771f75 commit 844bfa7

4 files changed

Lines changed: 40 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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
...
4545
"""
4646

47+
else_subscripts = """
48+
if 'a' in kwargs:
49+
pass
50+
else:
51+
kwargs['a'] = other(kwargs)
52+
"""
53+
4754

4855
@pytest.mark.parametrize(
4956
'template',
@@ -105,6 +112,7 @@ def test_implicit_dict_get(
105112
('call() in some_dict', 'some_dict[call(1, 2, 3)]'),
106113
('call(1, 2, 3) in some_dict', 'some_dict[call(1, 2)]'),
107114
('some[index] in some_dict', 'some_dict[some]'),
115+
('a in kwargs',),
108116
],
109117
)
110118
def test_correct_if(
@@ -122,3 +130,24 @@ def test_correct_if(
122130
visitor.run()
123131

124132
assert_errors(visitor, [])
133+
134+
135+
@pytest.mark.parametrize(
136+
'code',
137+
[
138+
else_subscripts,
139+
],
140+
)
141+
def test_no_implicit_dict_get_in_else_branch(
142+
assert_errors,
143+
parse_ast_tree,
144+
default_options,
145+
code,
146+
):
147+
"""Testing that subscripts in `else` branch do not trigger violation."""
148+
tree = parse_ast_tree(code)
149+
150+
visitor = ImplicitDictGetVisitor(default_options, tree=tree)
151+
visitor.run()
152+
153+
assert_errors(visitor, [])

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)