Skip to content

Commit 05e055a

Browse files
committed
fix: add checking await in async while True loop
1 parent 4053594 commit 05e055a

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Semantic versioning in our case means:
3030

3131
- Fixes `WPS226` false-positive on fstring parts, #3548
3232
- Fixes false positive `WPS412` with docstring and imports in `__init__.py`, #3569
33+
- Fixes false positive `WPS457` for while True loop with await in async function #3753
3334

3435
### Misc
3536

tests/test_visitors/test_ast/test_loops/test_loops/test_infinite_while_loops.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ def wrapper():
9999
...
100100
"""
101101

102+
correct_while7 = """
103+
async def worker():
104+
while True:
105+
await asyncio.sleep(1)
106+
"""
107+
102108
# Do raise:
103109

104110
wrong_while1 = """
@@ -166,6 +172,7 @@ def test_correct_while_loops_with_statements(
166172
correct_while4,
167173
correct_while5,
168174
correct_while6,
175+
correct_while7
169176
],
170177
)
171178
def test_correct_while_loops_with_try(

wemake_python_styleguide/visitors/ast/loops.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,16 @@ def _check_infinite_while_loop(self, node: AnyLoop) -> None:
175175

176176
with suppress(ValueError):
177177
evaled = ast.literal_eval(node.test)
178-
if not isinstance(evaled, ast.Name) and bool(evaled):
179-
self.add_violation(InfiniteWhileLoopViolation(node))
178+
if isinstance(evaled, ast.Name) or not bool(evaled):
179+
return
180+
181+
in_async_function = walk.get_closest_parent(node, ast.AsyncFunctionDef) is not None
182+
has_await = bool(walk.get_subnodes_by_type(node, ast.Await))
183+
184+
if in_async_function and has_await:
185+
return
180186

187+
self.add_violation(InfiniteWhileLoopViolation(node))
181188

182189
@final
183190
@decorators.alias(

0 commit comments

Comments
 (0)