mypy icon indicating copy to clipboard operation
mypy copied to clipboard

No error reported when a contravariant TypeVar is used as a return type within a tuple

Open prasanthcakewalk opened this issue 1 month ago • 1 comments

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

prasanthcakewalk avatar Nov 20 '25 13:11 prasanthcakewalk

Type parameter variance only applies to class-scoped type parameters. So neither contravariance nor covariance have any meaning for function-scoped type parameters, like the one in this example.

But note that with the PEP 695 Python 3.12 generic syntax this is no longer relevant.

jorenham avatar Dec 02 '25 16:12 jorenham

@jorenham Thanks for the response!

Type parameter variance only applies to class-scoped type parameters. So neither contravariance nor covariance have any meaning for function-scoped type parameters, like the one in this example.

Mypy does report an error on the following:

from typing import TypeVar

T = TypeVar("T", contravariant=True)

def f(x: T) -> T:
    return x

Mypy reports: error: Cannot use a contravariant type variable as return type [misc]

So contravariance does have a meaning in this case. Is this just an optional coverage provided by mypy and not part of any PEP (before the 3.12+ syntax)?

prasanthcakewalk avatar Dec 19 '25 13:12 prasanthcakewalk

So contravariance does have a meaning in this case.

Mypy rightly reports that contravariance should not be used here because, like I said, the concept of generic variance does not apply to function-bound type parameters. Mypy is apparently just inconsistent in reporting this.

jorenham avatar Dec 19 '25 13:12 jorenham