Skip to content

Commit

Permalink
Fixes bug with InfiniteWhileLoopViolation wemake-services#1857
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey94Dan committed Feb 12, 2021
1 parent 1f94c39 commit 15bd9c0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Semantic versioning in our case means:
- Fixes multiple `if` support for `InconsistentComprehensionViolation`
- Fixes that `NestedTernaryViolation` was not reported for a comprehension
- Fixes that `ConstantConditionViolation` was not reported for a comprehension

- Fix false positive `InfiniteWhileLoopViolation` for `try` #1857
### Misc

- Refactors how `tokenize` tests are executed, now we have an option to compile fixture code to make sure it is syntatically valid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,33 @@ def wrapper():
{1}
"""

while1 = """
while 1:
try:
...
except:
...
"""

while2 = """
while other:
while True:
try:
...
except:
...
"""


while3 = """
def wrapper():
while True:
try:
...
except:
...
"""


@pytest.mark.parametrize('template', [
template_simple,
Expand All @@ -58,6 +85,9 @@ def wrapper():
template_nested_if,
template_function,
template_other,
while1,
while2,
while3,
])
@pytest.mark.parametrize('keyword', [
'break',
Expand Down
7 changes: 7 additions & 0 deletions wemake_python_styleguide/visitors/ast/loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ def _check_infinite_while_loop(self, node: AnyLoop) -> None:
if not isinstance(node, ast.While):
return

has_try = any(
isinstance(sub_node, ast.Try)
for sub_node in ast.walk(node)
)
if has_try:
return

real_node = operators.unwrap_unary_node(node.test)
if isinstance(real_node, ast.NameConstant) and real_node.value is True:
if not loops.has_break(node, break_nodes=self._breaks):
Expand Down

0 comments on commit 15bd9c0

Please sign in to comment.