Skip to content

Commit d2e6504

Browse files
committed
Fix false 91x alarm on await in genexp target.
1 parent dd8dd3c commit d2e6504

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

flake8_trio/visitors/visitor91x.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,9 +769,11 @@ def visit_CompFor(self, node: cst.CompFor):
769769

770770
return False
771771

772-
# We don't have any logic on if generators are guaranteed to unroll, so always
773-
# ignore their content by not visiting subnodes.
772+
# The generator target will be immediately evaluated, but the other
773+
# elements will be lazily evaluated as the generator is consumed so we don't
774+
# visit them as any checkpoints in them are not guaranteed to execute.
774775
def visit_GeneratorExp(self, node: cst.GeneratorExp):
776+
node.for_in.iter.visit(self)
775777
return False
776778

777779
def visit_Import(self, node: cst.Import):

tests/autofix_files/trio910.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,3 +583,19 @@ async def foo_comprehension_2(): # error: 0, "exit", Statement("function defini
583583

584584
async def foo_comprehension_3():
585585
[... async for x in bar()]
586+
587+
588+
# Issue #714
589+
# (await x async for y in await z)
590+
# ^ ^ ^ this always runs!
591+
# ^ ^ this might not run
592+
# ^ this might not run
593+
594+
595+
async def await_in_gen_target():
596+
(print(x) for x in await foo())
597+
598+
599+
async def await_everywhere_except_gen_target(): # error: 0, "exit", Statement("function definition", lineno)
600+
(await x async for x in bar())
601+
await trio.lowlevel.checkpoint()

tests/autofix_files/trio910.py.diff

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,8 @@
205205

206206

207207
async def foo_comprehension_3():
208+
@@ x,3 x,4 @@
209+
210+
async def await_everywhere_except_gen_target(): # error: 0, "exit", Statement("function definition", lineno)
211+
(await x async for x in bar())
212+
+ await trio.lowlevel.checkpoint()

tests/eval_files/trio910.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,18 @@ async def foo_comprehension_2(): # error: 0, "exit", Statement("function defini
555555

556556
async def foo_comprehension_3():
557557
[... async for x in bar()]
558+
559+
560+
# Issue #714
561+
# (await x async for y in await z)
562+
# ^ ^ ^ this always runs!
563+
# ^ ^ this might not run
564+
# ^ this might not run
565+
566+
567+
async def await_in_gen_target():
568+
(print(x) for x in await foo())
569+
570+
571+
async def await_everywhere_except_gen_target(): # error: 0, "exit", Statement("function definition", lineno)
572+
(await x async for x in bar())

0 commit comments

Comments
 (0)