Skip to content

Commit f55a9c2

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

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ Semantic versioning in our case means:
1616
But, in the future we might change the configuration names/logic,
1717
change the client facing API, change code conventions significantly, etc.
1818

19+
## WIP
20+
21+
### Bugfixes
22+
23+
- Fixes false positive `WPS457` for ``while True`` loop with ``await`` expressions, #3753
24+
1925

2026
## 1.5.0
2127

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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,14 @@ 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+
has_await = any(walk.get_subnodes_by_type(node, ast.Await))
182+
if has_await:
183+
return
184+
185+
self.add_violation(InfiniteWhileLoopViolation(node))
180186

181187

182188
@final

0 commit comments

Comments
 (0)