从dict继承时,mypy型统一

发布于 2025-01-24 16:56:05 字数 581 浏览 2 评论 0原文

我希望以下代码可以打字,但事实并非如此。

from typing import Iterator, Type

class Parent(dict):
    pass

class A(Parent):
    pass

class B(Parent):
    pass

x: Iterator[Type[Parent]] = (A, B).__iter__()

# error: Incompatible types in assignment (expression has type "Iterator[ABCMeta]", variable has type "Iterator[Type[Parent]]"

如果我将父班更改为,

class Parent():
    pass

则可以正常工作。

有没有更好的方法让我让Mypy看到X作为类型iterator [type [parent]]parent> parent> parent>从dict

I would expect the following code to typecheck, but it does not.

from typing import Iterator, Type

class Parent(dict):
    pass

class A(Parent):
    pass

class B(Parent):
    pass

x: Iterator[Type[Parent]] = (A, B).__iter__()

# error: Incompatible types in assignment (expression has type "Iterator[ABCMeta]", variable has type "Iterator[Type[Parent]]"

If I change the parent class to

class Parent():
    pass

It works just fine.

Is there a better way for me to get mypy to see x as a value of type Iterator[Type[Parent]] when Parent inherits from dict?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷→糖丶微凉 2025-01-31 16:56:05

我猜在两行的工作中做这

_x: tuple[type[Parent], type[Parent]] = (A, B)
x: Iterator[type[Parent]] = _x.__iter__()

件事有点过度捕食

doing it on two lines works

_x: tuple[type[Parent], type[Parent]] = (A, B)
x: Iterator[type[Parent]] = _x.__iter__()

I guess mypy's a little overeager somewhere

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文