尝试注释哈希变量时,“ABCMeta"对象不可下标 [英] 'ABCMeta' object is not subscriptable when trying to annotate a hash variable
本文介绍了尝试注释哈希变量时,“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
类时出现错误:
When trying to use the HashLiteral
class I get the error:
pairs: Mapping[Expression, Expression]
TypeError: 'ABCMeta' object is not subscriptable
我上面的 pairs
注释有什么问题?
What is wrong with my annotation of pairs
above?
推荐答案
您应该使用 typing.Mapping
而不是 collections.abc.Mapping
.typing
包含各种类型的许多通用版本,旨在用于类型提示.根据 mypy
文档,存在一些差异typing
类和 collections.abc
类之间的区别,但他们不清楚这些区别到底是什么.
You should use typing.Mapping
instead of collections.abc.Mapping
. typing
contains many generic versions of various types, which are designed to be used in type hints. According to the mypy
documentation, there are some differences between the typing
classes and the collections.abc
classes, but they're unclear on exactly what those differences are.
这篇关于尝试注释哈希变量时,“ABCMeta"对象不可下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文