Skip to content

Invariance not enforced for class-scoped TypeVar used in base class expression #13713

Description

@erictraut

Mypy does not detect the case where a covariant or contravariant class (i.e. a class that is parameterized by a class-scoped covariant or contravariant TypeVar) derives from an invariant class. This creates a type correctness hole as shown in the following code sample.

fromtypingimportTypeVarT=TypeVar("T", covariant=True)
classMyList(list[T]): passdefappend_float(y: MyList[float]):
y.append(1.0)
x: MyList[int] =MyList([1, 2, 3])
append_float(x)

Pyright likewise did not previously catch this case. I just added the check here.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions