@@ -81,10 +81,12 @@ def __init__(
81
81
82
82
if spaces is None :
83
83
spaces = spaces_kwargs
84
- if isinstance (spaces , dict ) and not isinstance (spaces , OrderedDict ):
84
+ if isinstance (spaces , Mapping ) and not isinstance (spaces , OrderedDict ):
85
85
try :
86
86
spaces = OrderedDict (sorted (spaces .items ()))
87
- except TypeError : # raise when sort by different types of keys
87
+ except TypeError :
88
+ # Incomparable types (e.g. `int` vs. `str`, or user-defined types) found.
89
+ # The keys remain in the insertion order.
88
90
spaces = OrderedDict (spaces .items ())
89
91
if isinstance (spaces , Sequence ):
90
92
spaces = OrderedDict (spaces )
@@ -196,7 +198,17 @@ def __len__(self) -> int:
196
198
197
199
def __repr__ (self ) -> str :
198
200
"""Gives a string representation of this space."""
199
- return "Dict(" + ", " .join ([f"{ k } : { s } " for k , s in self .spaces .items ()]) + ")"
201
+ return (
202
+ "Dict(" + ", " .join ([f"{ k !r} : { s } " for k , s in self .spaces .items ()]) + ")"
203
+ )
204
+
205
+ def __eq__ (self , other ) -> bool :
206
+ """Check whether `other` is equivalent to this instance."""
207
+ return (
208
+ isinstance (other , Dict )
209
+ # Comparison of `OrderedDict`s is order-sensitive
210
+ and self .spaces == other .spaces # OrderedDict.__eq__
211
+ )
200
212
201
213
def to_jsonable (self , sample_n : list ) -> dict :
202
214
"""Convert a batch of samples from this space to a JSONable data type."""
0 commit comments