"Overloaded function implementation does not accept all arguments" error when using TypeVar with restricted types.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
I believe this may be similar to
https://github.com/python/mypy/issues/11004: mypy thinks **kwargs: Union[None, int, str] overload isn't accepted by **kwargs: Any
https://github.com/python/mypy/issues/9420: mypy thinks Type[T1] isn't accepted by Union[Type[T1], Type[T2]]
https://github.com/python/mypy/issues/9023: same bug with two different type variables
https://github.com/python/mypy/issues/11391: Overloaded function implementation does not accept all possible arguments
When using a TypeVar with restricted values in an overload for a function signature which does not contain generics, mypy seems to indicate an error where there shouldn't be one.
To Reproduce
from typing import TypeVar, Sequence, overload
T = TypeVar("T", int, str)
@overload
def foo(a: Sequence[int]):
...
@overload
def foo(a: Sequence[T]):
...
def foo(a: Sequence[int] | Sequence[str]):
...
Expected Behavior
Because T is restricted to be either an int or a str (or some subtype of either of them, but not of int | str), the upper-bounds for Sequence[T] in overload signature 2 should be Sequence[int] or Sequence[str]. The implementation accepts either a Sequence[int] or Sequence[str] for parameter a, so either of the overload's upper-bounds would satisfy the acceptable input types to the implementation.
Someone please tell me I'm just being a smoothbrain here, I've spent the last hour narrowing down my actual code to this small reproducible example and it feels like I'm just doing something dumb or I don't understand TypeVars even after reading all the documentation on them, but I can't see it.
Actual Behavior
mypycheck.py:13: error: Overloaded function implementation does not accept all possible arguments of signature 2 [misc]
Your Environment
- Mypy version used: 1.6.1 (compiled: yes) AND 1.11.1. Tested both.
- Mypy command-line flags: --config=mypy.ini
- Mypy configuration options from
mypy.ini(and other config files):
[mypy]
python_version = 3.12
cache_dir = .mypy_cache
allow_redefinition = true
check_untyped_defs = true
disallow_any_generics = true
implicit_reexport = false
no_implicit_optional = true
scripts_are_modules = true
strict_equality = true
warn_redundant_casts = true
warn_return_any = true
warn_unused_configs = true
warn_unused_ignores = true
warn_unreachable = true
show_error_context = true
show_column_numbers = true
show_error_codes = true
pretty = true
- Python version used: 3.12.4
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 minimal example in mypycheck.py with mypy 1.11.1 and the reported Python 3.12 configuration. Trace the overload implementation compatibility check for restricted TypeVars. Done means the example no longer reports the error and a regression test covers this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100