False positive for function that takes a `Callable` as a parameter and returns a `Tuple` of `Union`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
The following code
from typing import (
Callable,
TypeVar,
Union,
Tuple,
)
T = TypeVar("T")
def test(pred: Callable[[T], bool]) -> Tuple[Union[T, int]]:
return (1,)
def main(pred: Callable[[T], bool]) -> None:
(val,) = test(pred) # the error appears here
def f(x: int) -> bool:
return x > 1
main(f)
triggers the mypy error
Argument 1 to "test" has incompatible type "Callable[[T], bool]"; expected "Callable[[Union[T, int]], bool]"
at the line that I marked with a comment. I believe this is a bug as this correction makes no sense to me (of couse, I could be wrong).
I'm aware that this code is meaningless, but it's the simplest sample I was able to produce that causes the error.
From the tests that I've done it seems to depend both on the type variable being used and on the function test returning a Tuple[Union[...]]. So if I replace T with a regular type of if I return either Union[T, int] or Tuple[int] instead of Tuple[Union[T, int]] the error goes away. It also disappears by not upacking the the result of test(pred).
To Reproduce
Take the code above and check it with mypy. That's it.
Expected Behavior
No error triggered.
Actual Behavior
Mypy detects the error reported above.
Your Environment
- Mypy version used: 0.812
- Mypy command-line flags: --ignore-missing-imports --follow-imports=silent --show-column-numbers
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.9.2
- Operating system and version: Linux x64
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied Python snippet with mypy 0.812 and confirm the error at the unpacking call. Trace the type-variable inference for Callable, Tuple, and Union; done means the reproducer type-checks without the reported false-positive error and regression coverage protects this behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100