Skip to content

Moving an overloaded generic function into a class can sometimes introduce "incompatible return type" errors #5510

Closed
@Michael0x2a

Description

@Michael0x2a

Consider the following function:

from typing import *

T = TypeVar('T')

@overload
def f1(x: int) -> int: ...
@overload
def f1(x: T) -> T: ...
def f1(*args, **kwargs): pass

This program, after #5476 is merged, will type-check with no errors.

But if we move that function into a class like so, it'll cause an "Overloaded function signatures 1 and 2 overlap with incompatible return types" errors:

class Wrapper(Generic[T]):
    @overload
    def f2(self, x: int) -> int: ...
    @overload
    def f2(self, x: T) -> T: ...
    def f2(self, *args, **kwargs): pass

It seems this is because mypy is inferring f2 to basically be structured like below:

class Dummy(Generic[T]): pass

@overload
def f3(d: Dummy[T], x: int) -> int: ...
@overload
def f3(d: Dummy[T], x: T) -> T: ...
def f3(*args, **kwargs): pass

Basically, it's surprising that converting the function/tweaking the generics in what seems like a harmless way introduces a new error message. This is probably a bug?

In any case, it's unclear what the right thing to do here is -- I'm creating this issue so we can keep track of this issue + link to it in in the overload tests.

Also see #5280 (comment) for a little more context.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions