Skip to content

Generic[T_co] erroring when T_co = TypeVar("T_co", A, B, covariant=True) and passed subclass of A #8806

Open
@jamesbraza

Description

@jamesbraza

I am reporting what I believe is a bug in mypy's interpretation of TypeVar when passed two constraining types and also covariant=True.

I think the below code sample is valid (per Liskov Substitution Principle), and is erroneously being reported as a type error by mypy. Here is my working example:

from typing import TypeVar, Generic, Union

class A:
    pass

class ASub(A):
    pass

class B:
    pass

# Case 1... Success: no issues found
# T_co = TypeVar("T_co", covariant=True)

# Case 2... error: Value of type variable "T_co" of "SomeGeneric" cannot be "ASub"  [type-var]
T_co = TypeVar("T_co", A, B, covariant=True)

# Case 3... Success: no issues found
# T_co = TypeVar("T_co", bound=Union[A, B], covariant=True)


class SomeGeneric(Generic[T_co]):
    pass

class SomeGenericASub(SomeGeneric[ASub]):
    pass

Potentially Related Issue

I may be re-asking the contents of #8611, but to be honest, I get confused by the core of the issue

non-empty covariant generic subclass of a contravariant base


Versions

python==3.6.5
mypy==0.770

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions