Skip to content

False positive when type checking overloads with generics #9023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ngaya-ll opened this issue Jun 19, 2020 · 2 comments · May be fixed by #15320
Closed

False positive when type checking overloads with generics #9023

ngaya-ll opened this issue Jun 19, 2020 · 2 comments · May be fixed by #15320
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-overloads topic-type-variables

Comments

@ngaya-ll
Copy link
Contributor

ngaya-ll commented Jun 19, 2020

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

@JukkaL JukkaL added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-overloads topic-type-variables labels Jun 19, 2020
@JukkaL
Copy link
Collaborator

JukkaL commented Jun 19, 2020

This looks like a mypy bug.

@ilevkivskyi
Copy link
Member

This now typechecks without errors (but you need to either use --implicit-optional or add explicit | None in argument annotations)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-overloads topic-type-variables
Projects
None yet
3 participants