'Abcmeta' Object Is Not Subscriptable When Trying to Annotate a Hash Variable

'Abcmeta' Object Is Not Subscriptable When Trying to Annotate a Hash Variable

The following 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

is used as a base class for:

@dataclass(eq=True, frozen=True)
class HashLiteral(Expression):
    pairs: Mapping[Expression, Expression]
    ...

Node is defined as:

@dataclass(eq=True, frozen=True)
class Node:
    def __str__(self) -> str:
        raise NotImplementedError

When trying to use the HashLiteral class I get the error:

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

What is wrong with my annotation of pairs above?

7

1 Answer

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.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.