Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError when using SparseEnum sparsely with default() #4

Closed
jonemo opened this issue Sep 16, 2017 · 3 comments
Closed

KeyError when using SparseEnum sparsely with default() #4

jonemo opened this issue Sep 16, 2017 · 3 comments

Comments

@jonemo
Copy link

jonemo commented Sep 16, 2017

Using the default() feature introduced in version 1.3.0 (via #1) renders breaks SparseEnumap, omitted keys trigger a KeyError:

In [1]: from enumap import SparseEnumap, default

In [2]: from enum import auto

In [4]: class Box(SparseEnumap):
   ...:     a: str = auto()
   ...:     b: str = default('b')
   ...:

In [5]: Box.tuple(b='bb')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Users/jonas/.virtualenvs/cadsift/lib/python3.6/site-packages/enumap.py in tuple(cls, *values, **named_values)
     61         try:
---> 62             return tuple_class(*values, **named_values)
     63         except TypeError:

TypeError: __new__() missing 1 required positional argument: 'a'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-5-ff78554e8514> in <module>()
----> 1 Box.tuple(b='bb')

/Users/jonas/.virtualenvs/cadsift/lib/python3.6/site-packages/enumap.py in tuple(cls, *values, **named_values)
     62             return tuple_class(*values, **named_values)
     63         except TypeError:
---> 64             mapping = cls._make_checked_mapping(*values, **named_values)
     65             return tuple_class(**mapping)
     66

/Users/jonas/.virtualenvs/cadsift/lib/python3.6/site-packages/enumap.py in _make_checked_mapping(cls, *values, **named_values)
    236             mapping = dict(zip(names, values), **named_values)
    237             missing = names_set - set(mapping)
--> 238             mapping.update(((k, defaults[k]) for k in missing))
    239         else:
    240             mapping = dict(zip_longest(names, values), **named_values)

/Users/jonas/.virtualenvs/cadsift/lib/python3.6/site-packages/enumap.py in <genexpr>(.0)
    236             mapping = dict(zip(names, values), **named_values)
    237             missing = names_set - set(mapping)
--> 238             mapping.update(((k, defaults[k]) for k in missing))
    239         else:
    240             mapping = dict(zip_longest(names, values), **named_values)

KeyError: 'a'

When not using default(), the SparseEnumap behaves as expected and as before:

In [6]: class Box(SparseEnumap):
   ...:     a: str = auto()
   ...:     b: str = auto()
   ...:

In [7]: Box.tuple(b='bb')
Out[7]: Box_tuple(a=None, b='bb')
@jonemo
Copy link
Author

jonemo commented Sep 16, 2017

Solution idea: Should https://github.com/TadLeonard/enumap/blob/master/enumap.py#L219 be

-             member_defaults = dict(_iter_member_defaults(members))
+             member_defaults = cls.map(**dict(_iter_member_defaults(members)))

@TadLeonard
Copy link
Owner

Thank you! I'll push out a 1.3.1 ASAP.

@TadLeonard
Copy link
Owner

Fixed by #5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants