Alexandre Cox
Alexandre Cox

Reputation: 518

Using different TypeVar for subclass of a Generic class

Pycharm gives an Expected type X, got X instead type warning on the last line. The issue goes away if I use the same TypeVar for both the superclass and the subclass, but considering these classes will be in different files and the subclass will use a bound TypeVar, that is not possible.

I'm I missing something? Or should I report it as a bug? This is with python 3.7 on Pycharm 2019.2.6.

from typing import Generic, TypeVar

U = TypeVar("U")


class A(Generic[U]):
    def __init__(self, model: U):
        pass

    def func(self, b: U) -> U:
        return b


T = TypeVar("T")


class B(A[T]):
    def __init__(self, model: T):
        super().__init__(model)


B("").func("")  #Expected type 'str' (matched generic type 'U'), got 'str' instead

Upvotes: 1

Views: 798

Answers (1)

Alexandre Cox
Alexandre Cox

Reputation: 518

I posted an issue on the Pycharm issue tracker, the bug is fixed in version 2020.1

Upvotes: 1

Related Questions