-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Labels
Description
Bug Report
Mypy reports an error (as expected) when a contravariant TypeVar is used as a return type. However, if it is wrapped in a tuple, no error is reported (unexpected, I think). The same issue is present when using list here instead of tuple.
This issue is not present when using the 3.12+ type parameter syntax (i.e., the variance is inferred correctly as not-contravariant).
To Reproduce
from typing import TypeVar
T = TypeVar("T", contravariant=True)
def f(x: T) -> tuple[T]:
return (x,)Expected Behavior
Mypy reports an error.
Actual Behavior
No error reported.
A longer reproducible example
- gist: https://gist.github.com/mypy-play/489aaad37715e215ecab49ee128553d5
- playground: https://mypy-play.net/?mypy=latest&python=3.13&flags=strict&gist=489aaad37715e215ecab49ee128553d5
This covers the contravariant variable being (i) used properly, (ii) used as output directly, (iii) wrapped in a tuple in the output, under the old and new typing syntaxes.
Environment
- Mypy version used: mypy 1.18.2 (compiled: yes)
- Mypy command-line flags:
--strict - Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: Python 3.13.8