对 Generic class 的 subclass 使用不同的 TypeVar
Using different TypeVar for subclass of a Generic class
Pycharm 在最后一行给出了 Expected type X, got X instead
类型的警告。如果我对超类和子类使用相同的 TypeVar,问题就会消失,但考虑到这些 类 将在不同的文件中,而子类将使用 bound
TypeVar,这是不可能的。
我是不是漏了什么?还是我应该将其报告为错误?这是 python 3.7 在 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
我发了一个issue on the Pycharm issue tracker,bug在2020.1版本修复
Pycharm 在最后一行给出了 Expected type X, got X instead
类型的警告。如果我对超类和子类使用相同的 TypeVar,问题就会消失,但考虑到这些 类 将在不同的文件中,而子类将使用 bound
TypeVar,这是不可能的。
我是不是漏了什么?还是我应该将其报告为错误?这是 python 3.7 在 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
我发了一个issue on the Pycharm issue tracker,bug在2020.1版本修复