Marco-Sulla / python-frozendict

A simple immutable dictionary for Python
https://github.com/Marco-Sulla/python-frozendict
GNU Lesser General Public License v3.0
133 stars 16 forks source link

[FEATURE] Please add generics #81

Open spacether opened 1 year ago

spacether commented 1 year ago

Welcome! Before you write your Feature Request, please read below:

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like Please add generics to frozendict One should be able to define frozendict key and value types like so:

>>> import frozendict
>>> f = frozendict.frozendict[str, int]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable

this works

>>> import typing
>>> f = typing.Dict[str, int]

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

Related:

Marco-Sulla commented 1 year ago

frozendict is a C extension, so support for generics is for Python 3.9+:

marco@buzz:~/sources/python-frozendict$ . venv_3_8/bin/activate
(venv_3_8) marco@buzz:~/sources/python-frozendict$ python
Python 3.8.17 (tags/v3.8.17-dirty:9a2d5311d1, Jun 24 2023, 14:15:31) 
[GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import frozendict
>>> frozendict.frozendict[str, int]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
>>> dict[str, int]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
>>> 
(venv_3_8) marco@buzz:~/sources/python-frozendict$ deactivate 
marco@buzz:~/sources/python-frozendict$ . venv_3_9/bin/activate
(venv_3_9) marco@buzz:~/sources/python-frozendict$ python
Python 3.9.17 (tags/v3.9.17-dirty:0d3cd4eb66, Jun 24 2023, 14:33:23) 
[GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import frozendictint
>>> frozendict.frozendict[str, int]
frozendict.frozendict[str, int]
>>> dict[str, int]
dict[str, int]
>>>

immutabledict is a pure py implementation. frozendict has also a pure py implementation, and I can add generics to it for all supported py versions.

The question is: is it worth it? Python 3.8 EOL is in 14 Oct 2024. Consider to upgrade your Python. Python 3.10-3.11 are really faster.

If you can't, I can consider to add it to my TODO list (after implementing C extension for 3.11 and deepfreeze).