mypy's inference gets confused about unions with typevartuple #18407
Open
Description
Bug Report
In some cases involving matching an argument against a union with type var tuples, mypy infers Never
instead of something better.
To Reproduce
from typing import TypeVarTuple, Unpack, Generic
from collections.abc import Callable
Args = TypeVarTuple("Args")
class Built(Generic[Unpack[Args]]):
pass
def example(n: Built[Unpack[Args]] | Callable[[Unpack[Args]], None]) -> Built[Unpack[Args]]: ...
reveal_type(example) # N: Revealed type is "def [Args] (n: Union[__main__.Built[Unpack[Args`-1]], def (*Unpack[Args`-1])]) -> __main__.Built[Unpack[Args`-1]]"
@example # E: Argument 1 to "example" has incompatible type "Callable[[], None]"; expected "Built[*tuple[Never, ...]] | Callable[[VarArg(Never)], None]"
def command1() -> None:
return
Expected Behavior
I expect no error.
Your Environment
Checked in mypy playground.
- Mypy version used: v1.14
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini
(and other config files): N/A - Python version used: 3.12