Skip to content

Commit

Permalink
🔨 avoid redundant computation in fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
mwulfman committed Jun 27, 2024
1 parent 0d27efd commit ee3ebec
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions python/jiminy_py/src/jiminy_py/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,15 @@ def _unflatten_as(data: StructNested[Any],
"""
data_type = type(data)
if issubclass_mapping(data_type): # type: ignore[arg-type]
flat_items = [(key, _unflatten_as(value, data_leaf_it))
for key, value in data.items()] # type: ignore[union-attr]
try:
return data_type([ # type: ignore[call-arg]
(key, _unflatten_as(value, data_leaf_it))
for key, value in data.items() # type: ignore[union-attr]
])
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({ # type: ignore[call-arg]
key: _unflatten_as(value, data_leaf_it)
for key, value in data.items() # type: ignore[union-attr]
})
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

0 comments on commit ee3ebec

Please sign in to comment.