Skip to content

Syntax errors in async generators aren't reported in unreachable code #5750

Todo

Description

@MarcoGorelli

Describe the Bug

async def return_value():
    yield 1
    return
    return 42  # SyntaxError: 'return' with value in async generator

async def yield_from():
    return
    yield from [1]  # SyntaxError: 'yield from' inside async function

Pyrefly reports nothing here, even though there are runtime issues

(scratch) mgorelli@marcoslaptop:~/scratch$ mypy t.py
t.py:8: error: "yield from" in async function
Found 1 error in 1 file (errors prevented further checking)
(scratch) mgorelli@marcoslaptop:~/scratch$ ty check t.py
error[invalid-syntax]: `return` with value in async generator
 --> t.py:4:5
  |
4 |     return 42  # SyntaxError: 'return' with value in async generator
  |     ^^^^^^^^^

error[invalid-syntax]: `yield from` statement in async function; use `async for` instead
 --> t.py:8:5
  |
8 |     yield from [1]  # SyntaxError: 'yield from' inside async function
  |     ^^^^^^^^^^^^^^

Found 2 diagnostics
(scratch) mgorelli@marcoslaptop:~/scratch$ pyrefly check t.py
 INFO 0 errors

i think this would be a good precursor to #5678

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.pZVNDoIwEIWvMgmLusEG44pTmLhUQxppgE0xtqjc3pkplTZENuxI5-c1M18f_yxzXqTHoGIMdoGPkZ48FNOIOCP-huMBIIPzaJz6MBwlCB8S8O5cG1ilcZNQQ3ZLLpRCxDIVWdBPORbzt2CHuhS3peIcFyhlu1pPcuGJLXeYwckfwDQOdC9cfjPg7bBgfzUoojW0zj1sKWUo75-N1EbW_d3KpEIC4apeqmPnTdtBz8xZbLsFHuHZEWvoCCKHM3JlRkziPeE5N8fAYAaLRfQbyX2_OLoK0Rc

(Only applicable for extension issues) IDE Information

No response

Activity

  1. stroxler commented on Nov 24, 2026

    @stroxler
    Contributor

    Do you want to take this, since it overlaps with #5678 (and I think to some of my concerns on that change)?

  2. MarcoGorelli commented on Nov 24, 2026

    @MarcoGorelli
    CollaboratorAuthor

    i'm going to be short on time this week, so feel free to take it (else i'll try to get back to it next week)

  3. stroxler commented on Nov 24, 2026

    @stroxler
    Contributor

    I'll be short on time too, so it's yours :) I don't think there's any huge rush, especially given this is a holiday week in the US

    Feel free to un-assign if you decide you don't have time but I think it makes sense given the open PR. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions