Python ';ABCMeta&x27;对象在尝试注释哈希变量时不可下标

Python ';ABCMeta&x27;对象在尝试注释哈希变量时不可下标,python,python-3.x,annotations,Python,Python 3.x,Annotations,以下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

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]
    ...
节点
定义为:

@dataclass(eq=True, frozen=True)
class Node:
    def __str__(self) -> str:
        raise NotImplementedError
尝试使用
HashLiteral
类时,我得到错误:

pairs: Mapping[Expression, Expression]
TypeError: 'ABCMeta' object is not subscriptable

上面我对
对的注释有什么问题?

你应该使用
键入.Mapping
而不是
集合.abc.Mapping
<代码>键入
包含多种类型的通用版本,设计用于类型提示。根据,在
键入
类和
集合.abc
类之间存在一些差异,但它们不清楚这些差异到底是什么。

能否将
字段
添加到
表达式
示例中?
Node
a
dataclass
?@wwii my bad,更新示例使其更加完整!您是定义了
映射
,还是使用了
键入。映射
?@PatrickHaugh我实际上是从
集合中获得的。abc
使用了
键入
中的映射。
collections.abc
版本不知道类型暗示。这似乎是在Python的3.9版本中更改的,因此您现在可以从collections.abc导入类型。映射