Skip to content

Overloads with type variables and generic types are incorrectly rejected #9420

Closed
@catern

Description

@catern

🐛 Bug Report

If there are multiple overloads for a function, each of which take different type variables, and which return a generic type parameterized on the type variable, mypy will incorrectly report that the overload is incorrect.

To Reproduce

from typing import TypeVar, overload, Type, Tuple, Union

class Foo:
    pass

class Bar:
    pass

T1 = TypeVar('T1', bound=Foo)
T2 = TypeVar('T2', bound=Bar)

@overload
def f(cls: Type[T1]) -> Tuple[T1]:
    pass
@overload
def f(cls: Type[T2]) -> Tuple[T2]:
    pass

def f(cls: Union[
        Type[T1],
        Type[T2],
]) -> Union[
    Tuple[T1],
    Tuple[T2],
]:
    pass

Expected Behavior

mypy succeeds with no errors.

Actual Behavior

mypy fails with

overload.py:19: error: Overloaded function implementation does not accept all possible arguments of signature 1
overload.py:19: error: Overloaded function implementation cannot produce return type of signature 1
overload.py:19: error: Overloaded function implementation does not accept all possible arguments of signature 2
overload.py:19: error: Overloaded function implementation cannot produce return type of signature 2

The same error message is printed also with the Foo/Bar bounds removed, plus an error about overlapping overloads. So I don't think the bounds are an essential part.

If I remove the Tuple wrapping the return values, it succeeds.

Your Environment

  • Mypy version used: 0.782
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.7.8
  • Operating system and version: NixOS 19.09

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions