Skip to content

Generic[T] erroring when T = TypeVar("T", A, B) and passed subclass of A #8724

Closed
@jamesbraza

Description

@jamesbraza

I have a class that inherits from Generic[T], where is T = TypeVar("T", A, B).

When feeding in A or B, mypy raises no errors. However, when feeding in a subclass of A (called ASub), mypy errors.

In my opinion, mypy should not error when feeding in a subclass. I believe this is a bug.

Here is sample code necessary to reproduce what I am seeing.

from typing import Union, TypeVar, Generic

class A:
    pass

class ASub(A):
    pass

class B:
    pass

# Case 1... Success: no issues found
# T = TypeVar("T", bound=Union[A, B])

# Case 2... error: Value of type variable "T" of "SomeGeneric" cannot be "ASub"
T = TypeVar("T", A, B)

class SomeGeneric(Generic[T]):
    pass

class SomeGenericASub(SomeGeneric[ASub]):
    pass

I included the Union case as a comment to highlight that for some reason, Union doesn't raise an error. However, I don't see why it should not error, when using just A, B does error

I am invoking mypy from the command line with no flags. Here are my versions:

python==3.6.5
mypy==0.770

Aside

I encountered this issue, and then discovered the "minimal repro" was already asked in Stack Overflow here. However, the answer provided did not actually solve the problem, so I think this is a bug in mypy.

I apologize in advance if this is a duplicate issue.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions