Skip to content

Commit

Permalink
apply suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
mwulfman committed Jun 27, 2024
1 parent 7325f65 commit 0d27efd
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions python/jiminy_py/src/jiminy_py/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from functools import lru_cache
from itertools import chain, starmap
from collections.abc import (Mapping, ValuesView, Sequence, Set)
from collections import OrderedDict
from typing import (
Any, Union, Mapping as MappingT, Iterable, Iterator as Iterator, Tuple,
TypeVar, Callable, Type)
Expand Down Expand Up @@ -174,10 +173,19 @@ def _unflatten_as(data: StructNested[Any],
"""
data_type = type(data)
if issubclass_mapping(data_type): # type: ignore[arg-type]
return data_type(OrderedDict({ # type: ignore[call-arg]
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]
])
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]
})
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 0d27efd

Please sign in to comment.