Skip to content

intentionally empty generator marked as unreachable #15345

Closed

Description

@asottile

Bug Report

this seems to be a common pattern:

def f() -> Generator[None, None, None]:
    return
    yield

it can instead be rewritten as:

def f() -> Generator[None, None, None]:
    yield from ()

To Reproduce

see above

Expected Behavior

(no errors)

Actual Behavior

$ mypy --warn-unreachable t.py
t.py:5: error: Statement is unreachable  [unreachable]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.3.0
  • Mypy command-line flags: --warn-unreachable
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.10.6

Activity

  1. Hayashi-Yudai commented on Jun 2, 2023

    @Hayashi-Yudai

    Uum... I couldn't reproduce this bug.

    • mypy: 1.3.0
    • with --warn-unreachable flag
    • w/o mypy.ini
    • python 3.10.5
    ❯ mypy --warn-unreachable t.py
    Success: no issues found in 1 source file
    
    ❯ cat t.py
    from typing import Generator
    
    
    def f() -> Generator[None, None, None]:
        yield from ()
    
  2. asottile commented on Jun 2, 2023

    @asottile
    ContributorAuthor

    yes you've used the workaround.

  3. ikonst commented on Jun 2, 2023

    @ikonst
    Contributor

    What could be a good heuristic here? That an empty yield statement (without expression) is explicitly allowed to trail a return statement?

  4. asottile commented on Jun 2, 2023

    @asottile
    ContributorAuthor

    I'm not sure -- I think the only case is specifically the one in the issue -- I can't think of others since if it were conditional then there'd probably be a yield in the other arm. maybe this is specifically a cutout for return; yield as the total body?

  5. ikonst commented on Jun 2, 2023

    @ikonst
    Contributor

    How about every function having a budget for one bare yield that doesn't get flagged?

  6. ikonst commented on Jun 7, 2023

    @ikonst
    Contributor

    Planning to address it on top of #15386.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-reachabilityDetecting unreachable code

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions