对通用类的子类使用不同的TypeVar

问题描述 投票:0回答:1

Pycharm在最后一行给出Expected type X, got X instead类型警告。如果我对超类和子类都使用相同的TypeVar,问题就消失了,但是考虑到这些类将位于不同的文件中,并且子类将使用bound TypeVar,这是不可能的。

我想念什么吗?还是应该将其报告为错误?这是Pycharm 2019.2.6上的python 3.7。

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
pycharm type-hinting python-typing
1个回答
0
投票

我发布了issue on the Pycharm issue tracker,该错误已在版本2020.1中修复

© www.soinside.com 2019 - 2024. All rights reserved.