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

[jiminy_py] Fix 'tree.unflatten_as' mixing up key order for 'gym.spaces.Dict'. #819

Merged
merged 6 commits into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions python/jiminy_py/src/jiminy_py/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading