Skip to content

mypy's inference gets confused about unions with typevartuple #18407

Open
@A5rocks

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

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-pep-646PEP 646 (TypeVarTuple, Unpack)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions