diff --git a/python/jiminy_py/src/jiminy_py/tree.py b/python/jiminy_py/src/jiminy_py/tree.py index 9b33940e8..219029120 100644 --- a/python/jiminy_py/src/jiminy_py/tree.py +++ b/python/jiminy_py/src/jiminy_py/tree.py @@ -173,10 +173,20 @@ def _unflatten_as(data: StructNested[Any], """ data_type = type(data) if issubclass_mapping(data_type): # type: ignore[arg-type] - return data_type({ # type: ignore[call-arg] - key: _unflatten_as(value, data_leaf_it) + flat_items = [ + (key, _unflatten_as(value, data_leaf_it)) for key, value in data.items() # type: ignore[union-attr] - }) + ] + try: + # Initialisation from dict cannot be the default path as + # `gym.spaces.Dict` would sort keys in this specific scenario, + # which must be avoided. + return data_type(flat_items) # type: ignore[call-arg] + except (ValueError, RuntimeError): + # Fallback to initialisation from dict in the rare event of + # a container type not supporting initialisation from a + # sequence of key-value pairs. + return data_type(dict(flat_items)) # type: ignore[call-arg] if issubclass_sequence(data_type): # type: ignore[arg-type] return data_type(tuple( # type: ignore[call-arg] _unflatten_as(value, data_leaf_it) for value in data