#4825·pyrefly

false `bad-return` for a generator function with `Iterator[X] | T` return annotation

Author: MarcoGorelliCreated Sep 5, 2026Updated Sep 20, 2026
Labelsstalequansight

Describe the Bug

python
from collections.abc import Iterator


def gen(x: int) -> Iterator[int] | int:
    yield from range(4)
    return 5
bash
ERROR sandbox.py:6:12-13: Returned type `Literal[5]` is not assignable to declared return type `None` [[bad-return](https://pyrefly.org/en/docs/error-kinds/#bad-return)]

All either type checkers are fine with this, but Pyrefly emits the error above

Noticed in huggingface-hub

https://github.com/huggingface/huggingface_hub/blob/deb97775d91cb1ce2a4c254b29ad30081044257d/src/huggingface_hub/_hot_reload/client.py#L70-L79

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.w1tk4ihFYCUGKC4gSTY9NU-jwgoYH5BEDlMQDRSIBSZcIAUNuEpQUaEANrwoMS89VcMEGFVIic0UM1SVFQIgAgpQBwKdBYyO9FKgFUCX6cXkKQMLrFSFjJKSgmIrfX2Y9vyidP3UPP2U_ORifRQd-gqgBJRYlpgJLgtRjVPIB6eCYqCxlESnOiQ21fFFpjooLoEq8MYPAA

(Only applicable for extension issues) IDE Information

No response