gpt4 book ai didi

python - 尝试注释哈希变量时,“ABCMeta”对象不可下标

转载 作者:行者123 更新时间:2023-12-03 15:07:55 30 4
gpt4 key购买 nike

以下dataclass :

from abc import ABC
from collections.abc import Mapping
from dataclasses import dataclass, field

@dataclass(eq=True, order=True, frozen=True)
class Expression(Node, ABC):
def node(self):
raise NotImplementedError

用作基类:
@dataclass(eq=True, frozen=True)
class HashLiteral(Expression):
pairs: Mapping[Expression, Expression]
...
Node定义为:
@dataclass(eq=True, frozen=True)
class Node:
def __str__(self) -> str:
raise NotImplementedError

尝试使用 HashLiteral 时类我收到错误:
pairs: Mapping[Expression, Expression]
TypeError: 'ABCMeta' object is not subscriptable

我的 pairs 注释有什么问题以上?

最佳答案

您应该使用 typing.Mapping而不是 collections.abc.Mapping . typing包含各种类型的许多通用版本,旨在用于类型提示。根据 mypy documentation , typing 之间存在一些差异类和 collections.abc类,但他们不清楚这些差异到底是什么。

关于python - 尝试注释哈希变量时,“ABCMeta”对象不可下标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59955751/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com