Skip to content

Commit 1d89a60

Browse files
committed
fix bug: dict_items handling in mogrify (combo with curry macro and frozendict)
1 parent 0d1f67d commit 1d89a60

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

unpythonic/collections.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def get_abcs(cls):
2727
# TODO: allow multiple input container args in mogrify, like map does (also support longest, fillvalue)
2828
# OTOH, that's assuming an ordered iterable... so maybe not for general containers?
2929
# TODO: move to unpythonic.it? This is a spork...
30+
_dictitems_type = type({}.items())
3031
def mogrify(func, container):
3132
"""In-place recursive map for mutable containers.
3233
@@ -115,7 +116,8 @@ def doit(x):
115116
ctor = cls._make if hasattr(cls, "_make") else cls
116117
return ctor(doit(elt) for elt in x)
117118
elif isinstance(x, Set):
118-
ctor = type(x)
119+
# dict_items cannot be instantiated, so return a regular set if we're asked to mogrify one
120+
ctor = type(x) if not isinstance(x, _dictitems_type) else set
119121
return ctor({doit(elt) for elt in x})
120122
return func(x) # atom
121123
return doit(container)

unpythonic/syntax/test/test_curry.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from ...fold import foldr
77
from ...fun import composerc as compose
88
from ...llist import cons, nil, ll
9+
from ...collections import frozendict
910

1011
def test():
1112
with curry:
@@ -70,6 +71,13 @@ def add3(a, b, c):
7071
acc if n == 0 else jump(self, n - 1, n * acc))))
7172
assert fact(5) == 120
7273

74+
# dict_items handling in mogrify
75+
with curry:
76+
d1 = frozendict(foo='bar', bar='tavern')
77+
d2 = frozendict(d1, bar='pub')
78+
assert tuple(sorted(d1.items())) == (('bar', 'tavern'), ('foo', 'bar'))
79+
assert tuple(sorted(d2.items())) == (('bar', 'pub'), ('foo', 'bar'))
80+
7381
print("All tests PASSED")
7482

7583
if __name__ == '__main__':

0 commit comments

Comments
 (0)