diegojromerolopez / gelidum

Freeze your objects in python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

frozendict is unable to be instantiated with an iterator or generator

nolsto opened this issue · comments

{key: freeze_func(value) for key, value in mapping.items()}

The __init__ method of frozendict assumes that the class will only ever be called with a parameter of subtype collections.abc.Mapping (has an items() method).

An iteratable/iterator or generator type parameter should be handled as well.
Something along the lines of:

if isinstance(mapping, Mapping):
    super().__init__(
        {key: freeze_func(value) for key, value in mapping.items()}
    )
else:
    super().__init__(
        {key: freeze_func(value) for key, value in mapping}
    )

I'm not up on typing yet, so am hesitant to make a pull request.

Additionally, the __init__ method signature could be changed to handle dict creation by kwargs much like the builtin dict class.

def __init__(self, mapping=None, /, freeze_func=None, **kwargs):

There could be another case for this:

if isinstance(mapping, Mapping):
    ...
elif mapping is None and kwargs:
    super().__init__(
        {key: freeze_func(value) for key, value in kwargs.items()}
    )
else:
    ...

Hello @nolsto

Thank you for your comment. I'm going to take a look and prepare a PR.

Cheers.

Please, @nolsto could you take a look at #30 and see if that is good for you?

Version 0.5.8 in pypi includes this change.

Thank you @nolsto for your help. Any other issue, don't hesitate to contact me.

Cheers.