Skip to content

False positive when type checking overloads with generics #9023

Closed
@ngaya-ll

Description

@ngaya-ll

Are you reporting a bug, or opening a feature request? Bug

MyPy has some strange behavior when type-checking overloaded functions with multiple generic parameters.

Example

from typing import Callable, TypeVar, Union, overload

T = TypeVar('T')
U = TypeVar('U')

@overload
def call(*, f: Callable[[], T]) -> T: ...

@overload
def call(*, u: U) -> U: ...

def call(*, f: Callable[[], T] = None, u: U = None) -> Union[T, U, None]:
    return f() if f else u

Actual behavior

$ mypy example.py
example.py:12: error: Overloaded function implementation does not accept all possible arguments of signature 2
example.py:12: error: Overloaded function implementation cannot produce return type of signature 2
Found 2 errors in 1 file (checked 1 source file)

If I swap the order of the parameters on line 12 then mypy complains about signature 1 instead of signature 2.

def call(*, u: U = None, f: Callable[[], T] = None) -> Union[T, U, None]:

I can get mypy to accept the module by adding a dummy parameter in signature 2:

def call(*, f: None = None, u: U) -> U: ...

Expected behavior

$ mypy example.py
Success: no issues found in 1 source file

Versions

$ mypy --version
mypy 0.780
$ python3 --version
Python 3.8.1

Do you see the same issue after installing mypy from Git master? Yes

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions