mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Plugging a contravariant Typevar into an invariant generic can make mypy miss problems

Open mikolajz opened this issue 3 years ago • 0 comments

Bug Report

In the code below, plugging the contravariant TypeVar T_contra into the invariant slot in generic Data makes mypy forget about the contravariance and the error "Cannot use a contravariant type variable as return type" is not generated. The code fails at runtime.

To Reproduce

from typing import Generic, TypeVar, Type

T = TypeVar('T')
T_contra = TypeVar('T_contra', contravariant=True)


class Data(Generic[T]):
    d: T


class Test(Generic[T_contra]):
    def __init__(self, some_type: Type[T_contra]) -> None:
        self._type = some_type

    def f(self) -> Data[T_contra]:
        result = Data[T_contra]()
        result.d = self._type()
        return result


t: Test[str] = Test[object](object)
assert isinstance(t.f(), str)

Expected Behavior

Report a similar error as when I try to return T_contra instead of Data[T_contra] ("Cannot use a contravariant type variable as return type")

Actual Behavior

The code passes type checking, but fails at runtime.

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: no extra flags
  • Mypy configuration options from mypy.ini (and other config files): no configuration file
  • Python version used: 3.8.16

mikolajz avatar Jan 17 '23 17:01 mikolajz