尝试注释散列变量时,“ABCMeta”对象不可下标

以下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?


慕尼黑5688855
浏览 122回答 1
1回答

临摹微笑

您应该使用typing.Mapping而不是collections.abc.Mapping. typing包含许多不同类型的通用版本,旨在用于类型提示。根据mypy文档typing,类和类之间存在一些差异collections.abc,但他们不清楚这些差异到底是什么。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python