MagicStack / immutables

A high-performance immutable mapping type for Python.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't use Map class as generic on Python <=3.6

ariebovenberg opened this issue · comments

On python <=3.6 it isn't possible to use Map as a generic type:

>>> from immutables import Map
>>> Map
<class 'immutables._map.Map'>
>>> Map[str, int]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable

On python 3.7+, the problem does not occur. The likely reason is the use of __class_getitem__, which is new in Python 3.7.

Relevant code snippet below:

immutables/immutables/_map.c

Lines 3389 to 3394 in 82e5409

{
"__class_getitem__",
(PyCFunction)map_py_class_getitem,
METH_O|METH_CLASS,
NULL
},

It looks like a metaclass will be needed to fix this. That, or inheriting directly from typing.Mapping. Not sure which is the preferred option.

The likely reason is the use of class_getitem, which is new in Python 3.7.

Yes. And there's nothing we can do here, sadly. Map is implemented in C, where __class_getitem__ is the only way to make this work.

@1st1 thanks, I realize now I forgot to check the closed issues and I found you were already aware.