asottile / pyupgrade

A tool (and pre-commit hook) to automatically upgrade syntax for newer versions of the language.
MIT License
3.5k stars 177 forks source link

Rewriting to `yield from` may introduce type errors when using pyright #917

Closed Popkornium18 closed 10 months ago

Popkornium18 commented 10 months ago

Consider the following snippet:

# pyright: strict
import subprocess

def _capture_process_output(command: list[str]):
    popen = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
    )

    for line in iter(popen.stdout.readline, ""):
        yield line

    popen.stdout.close()
    return_code = popen.wait()
    if return_code:
        raise Exception(f"Process failed with exit code {return_code}")

Pyright is fine with that function and reports its return type as Generator[str, Any, None].

However, after reweriting it to yield from iter(... pyright complains about the return type being partially unknown and reports the return type as Generator[str, Unknown, None].

asottile commented 10 months ago

report it to pyright then I don't know what you want me to do about it

Popkornium18 commented 10 months ago

Ok, I'll do that, thanks