Skip to content

Invalid infer generic type for contravariant TypeVar inside function call #6482

Open
@unknownlighter

Description

@unknownlighter
from typing import Generic, TypeVar


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


class Animal:
    pass


class Cat(Animal):
    pass


class AnimalBox(Generic[T_contra]):
    def __init__(self, animal: T_contra):
        pass


def print_box(a: AnimalBox[Cat]):
    pass


animal = Animal()

print_box(AnimalBox(animal))  # error: Argument 1 to "AnimalBox" has incompatible type "Animal"; expected "Cat"

box = AnimalBox(animal)
print_box(box)  # OK

Reproduces only on contravariants. Covariants seems to be OK.
Tested on: mypy-0.680+dev.69eaf88a0b03e9f491cc22534c2d30f1d21832b2

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions