不兼容的返回类型[7]:预期`varible [t]

发布于 2025-02-01 12:54:23 字数 650 浏览 3 评论 0原文

PYRE类型失败,因为当我们返回T时,它假设返回类型R不正确。 当我们在另一类中使用派生类并访问从父类中键入的通用字段时,就会发生这种情况。

以下是在最后一行投诉的玩具代码。

T = TypeVar("T")
R = TypeVar("R")

@dataclass(frozen=True)
class A(Generic[T, R]):
    t: T
    r: R

class B(A[T, R]):
    ...

@dataclass(frozen=True)
class X(Generic[T, R]):
    a: A[T, R]
    b: B[T, R]

    def getfromA(self) -> T:
        return self.a.t

    def getfromB(self) -> T:
        # pyre-fixe[7] Incompatible return type [7]: Expected `Variable[T]` but got `Variable[R]`.Pyre
        return self.b.t

请注意,使用a,带有通用的基类时,它可以正常工作。 但是,当与B(派生类)一起使用时,它会反转类型。

我是想念什么还是撞到了一些已知的pyre错误?

谢谢

Pyre type fails because it assumes an incorrect return type R when we are returning T.
It happens when we use a derived class in another class and access a field that is generic typed from the parent class.

Following is the toy code for which it is complaining at the last line.

T = TypeVar("T")
R = TypeVar("R")

@dataclass(frozen=True)
class A(Generic[T, R]):
    t: T
    r: R

class B(A[T, R]):
    ...

@dataclass(frozen=True)
class X(Generic[T, R]):
    a: A[T, R]
    b: B[T, R]

    def getfromA(self) -> T:
        return self.a.t

    def getfromB(self) -> T:
        # pyre-fixe[7] Incompatible return type [7]: Expected `Variable[T]` but got `Variable[R]`.Pyre
        return self.b.t

Notice that it works correctly when using A, the base class with generic.
But when using with B, the derived class, it inverts the types.

Am I missing something or am I hitting some known pyre bug?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文