Skip to content

Commit bffa736

Browse files
authored
don't sort dicts in freeze with python>=3.7
1 parent fe9f2c0 commit bffa736

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

unification/utils.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import sys
12
from collections import deque
2-
from collections.abc import Mapping, Set
3+
from collections.abc import Mapping, Sequence, Set
34
from contextlib import suppress
45

6+
__PY37 = sys.version_info >= (3, 7)
7+
58

69
def transitive_get(key, d):
710
"""Get a value for a dict key in a transitive fashion.
@@ -90,9 +93,13 @@ def freeze(d):
9093
((1, 2),)
9194
"""
9295
if isinstance(d, Mapping):
93-
return tuple(map(freeze, sorted(d.items(), key=lambda x: hash(x[0]))))
96+
if __PY37:
97+
items = d.items()
98+
else:
99+
items = sorted(d.items(), key=lambda x: hash(x[0]))
100+
return tuple(map(freeze, items))
94101
if isinstance(d, Set):
95102
return tuple(map(freeze, sorted(d, key=hash)))
96-
if isinstance(d, (tuple, list)):
103+
if isinstance(d, Sequence):
97104
return tuple(map(freeze, d))
98105
return d

0 commit comments

Comments
 (0)