-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Labels
bugmypy got something wrongmypy got something wrongtopic-type-contextType context / bidirectional inferenceType context / bidirectional inference
Description
Bug Report
When calling a generic function mypy unexpectedly introduces optionality in the TypeVar when the result is unpacked and raises an error on a perfectly valid function call:
from typing import Iterable
def fun[T](values: Iterable[T], value: list[T]) -> tuple[T | None, T | None]:
...
return None, None
values: Iterable[int]
my_list: list[int]
result = fun(values, my_list) # works fine
m, n = result
a, b = fun(values, my_list) # raises error in mypyMypy error:
main.py:15: error: Argument 2 to "fun" has incompatible type "list[int]"; expected "list[int | None]" [arg-type]
This has something to do with the return type of the function being a tuple of optional T's. If the return type is not a union with None or not a tuple, the error disappears.
To reproduce
Here is playground gist of the example: gist
Expected Behavior
In the example I expect both calls to be approved by mypy. Also, I don't expect unpacking to influence what arguments can given in a function call, at least not in this case.
Actual Behavior
mypy raises an error on the second call.
Your Environment
I used mypy playground to confirm this error in various versions:
- Mypy version used:
1.17.11.18.2master branch - Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used:
3.123.133.14
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-type-contextType context / bidirectional inferenceType context / bidirectional inference