Skip to content

Commit 403b325

Browse files
committed
fix: WPS529 false positive
1 parent 3771f75 commit 403b325

4 files changed

Lines changed: 39 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: 28 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',
@@ -122,3 +129,24 @@ def test_correct_if(
122129
visitor.run()
123130

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