Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unreachable yield should still make a function a generator #5972

Closed
kepler-5 opened this issue Sep 18, 2023 · 2 comments
Closed

Unreachable yield should still make a function a generator #5972

kepler-5 opened this issue Sep 18, 2023 · 2 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@kepler-5
Copy link

Describe the bug
According to https://docs.python.org/3/reference/expressions.html#yieldexpr, when a function contains a yield expression, it becomes a generator function, regardless of the reachability of that yield expression. But in the code below, an unconditional return makes the yield unreachable and pyright seems to stop type checking as soon as it sees the return.

def foo() -> None:
    return
    yield 42

This function signature is accepted by pyright but seems incorrect, since the presence of the yield keyword, even if unreachable, turns this function into a generator function. Calling foo() as written gives me <generator object foo at 0x12dfd54e0> rather than the promised None.

Removing the return, I get the expected error message:

Return type of generator function must be compatible with "Generator[Literal[42], Any, Any]"
  Type cannot be assigned to type "None"

I believe this to be a bug and I couldn't find a duplicate, but I'm definitely no expert so I'm sorry if I'm off about something here.

VS Code extension or command-line
Pylance v2023.9.10

@kepler-5 kepler-5 added the bug Something isn't working label Sep 18, 2023
erictraut pushed a commit that referenced this issue Sep 18, 2023
…as performed only for `yield` statements, but it's possible to define a generator function that has no reachable yield statements. This addresses #5972.
erictraut pushed a commit that referenced this issue Sep 18, 2023
…as performed only for `yield` statements, but it's possible to define a generator function that has no reachable yield statements. This addresses #5972.
erictraut added a commit that referenced this issue Sep 18, 2023
…as performed only for `yield` statements, but it's possible to define a generator function that has no reachable yield statements. This addresses #5972. (#5975)

Co-authored-by: Eric Traut <erictr@microsoft.com>
@erictraut
Copy link
Collaborator

Pyright validates that the annotated return type of a generator function is compatible with Generator (or AsyncGenerator), but it currently does this only when type checkin a yield or yield to statement. In your example, the yield statement was determined to be unreachable, and errors detected when analyzing unreachable statements are suppressed.

I've enhanced the logic that validates the annotated return address for a generator so it is performed regardless of whether a yield or yield from statement is evaluated. This will be included in the next release.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Sep 18, 2023
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.328, which I just published. It will also be included in a future release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants