From 0d27efd25d9083e41a99d40f065293aae51c21a6 Mon Sep 17 00:00:00 2001 From: Mathias Wulfman Date: Thu, 27 Jun 2024 16:01:42 +0200 Subject: [PATCH] apply suggestion --- python/jiminy_py/src/jiminy_py/tree.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/python/jiminy_py/src/jiminy_py/tree.py b/python/jiminy_py/src/jiminy_py/tree.py index 6d0892808..41c812add 100644 --- a/python/jiminy_py/src/jiminy_py/tree.py +++ b/python/jiminy_py/src/jiminy_py/tree.py @@ -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) @@ -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