import attrmap gives TypeError: 'type' object is not subscriptable

Question:

I’m trying to use the library AttrMap which is suggested as an alternative to AttrDict library. From the documentation of AttrDict:

… If either of these caveats are deal-breakers, or you don’t need
your object to be a dict, consider using AttrMap instead.

I installed it with pip install attrmap and imported it in a fresh conda environment, as usual with import attrmap.

However the import gives the following error:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import attrmap
  File "/Users/claudia/opt/anaconda3/envs/test/lib/python3.8/site-packages/attrmap/__init__.py", line 2, in <module>
    from .attrmap import AttrMap, AttributeMap, AttrMapping, AttributeMapping
  File "/Users/claudia/opt/anaconda3/envs/test/lib/python3.8/site-packages/attrmap/attrmap.py", line 12, in <module>
    class AttrMap(object):
  File "/Users/claudia/opt/anaconda3/envs/test/lib/python3.8/site-packages/attrmap/attrmap.py", line 91, in AttrMap
    def keys(self) -> list[str]:
TypeError: 'type' object is not subscriptable

I’ve already tried installing different previous versions and adding the import in a Colab notebook, all with same error.

Does anyone knows what the issue might be? I don’t think is related to my environment.

Asked By: claudia

||

Answers:

list[str] is a type annotation available since python 3.9, the traceback shows your system uses python 3.8, try upgrading your python or downgrading attrmap.

It might be worth reporting this issue to the attrmap developer.

Answered By: ljmc

Now the bug is fixed in the latest version as introduced in the github. You can try to update to the latest version. It supports python>=3.6 now.

Answered By: Yiqun Chen
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.