TypeVar bound to Callable confuses Mypy
Open
Nobody has claimed this yet.
bug
topic-type-variables
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
# test_case.py
from typing import Callable, Optional, TypeVar, Union, overload
class Foo: pass
class Bar: pass
ReturnsFooOrBarT = Callable[..., Union[Foo, Bar]]
TermT = TypeVar("TermT", bound=ReturnsFooOrBarT)
def returns_foo() -> Foo:
return Foo()
def returns_bar() -> Bar:
return Bar()
@overload
def do_something_or_something_else(
*,
something_else: Union[int, TermT] = 0,
) -> None:
...
@overload
def do_something_or_something_else(
something: TermT,
*,
something_else: Union[int, TermT] = 0,
) -> None:
...
def do_something_or_something_else(
something: Optional[TermT] = None,
*,
something_else: Union[int, TermT] = 0,
) -> None:
pass
do_something_or_something_else(something_else=1)
do_something_or_something_else(something_else=returns_bar)
do_something_or_something_else(something_else=returns_foo)
do_something_or_something_else(returns_foo, something_else=1)
do_something_or_something_else(returns_foo, something_else=returns_foo)
do_something_or_something_else(returns_bar, something_else=1)
do_something_or_something_else(returns_bar, something_else=returns_bar)
returns_foo_or_bar: ReturnsFooOrBarT
# These should all validate, no?
returns_foo_or_bar = returns_foo
do_something_or_something_else(returns_foo_or_bar, something_else=1)
do_something_or_something_else(returns_foo_or_bar, something_else=returns_bar) # <-- false negative?
do_something_or_something_else(returns_foo, something_else=returns_bar) # <-- false negative?
# These should all validate, no?
returns_foo_or_bar = returns_bar
do_something_or_something_else(returns_foo_or_bar, something_else=1)
do_something_or_something_else(returns_foo_or_bar, something_else=returns_foo) # <-- false negative?
do_something_or_something_else(returns_bar, something_else=returns_foo) # <-- false negative?
% mypy --config=/dev/null test_case.py
/dev/null: No [mypy] section in config file
test_case.py:54: error: Value of type variable "TermT" of "do_something_or_something_else" cannot be "Callable[[], object]"
test_case.py:55: error: Value of type variable "TermT" of "do_something_or_something_else" cannot be "Callable[[], object]"
test_case.py:60: error: Value of type variable "TermT" of "do_something_or_something_else" cannot be "Callable[[], object]"
test_case.py:61: error: Value of type variable "TermT" of "do_something_or_something_else" cannot be "Callable[[], object]"
Found 4 errors in 1 file (checked 1 source file)
% python --version
Python 3.9.10
% mypy --version
mypy 0.931
% mypy --config=/dev/null test_case.py
If I s/TermT/ReturnsFooOrBarT/g, the errors go away. The above is a contrived example, but having a TypeVar is helpful when returning a function of the same type that was provided (e.g., with a decorator).
Possibly related to #8922 or #9075?
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
Reproduce the reported behavior with the self-contained test_case.py example using mypy --config=/dev/null, and compare it with the version where TermT is replaced by ReturnsFooOrBarT. Trace callable TypeVar bound inference and overload checking; done means the four reported false negatives no longer occur while the example still type-checks as intended.
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
- Mostly clear
- Newbie friendliness
- 42/100