From 752edcd9db5aac8ea3a206f562c5e490840b5f5f Mon Sep 17 00:00:00 2001 From: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> Date: Wed, 25 Nov 2026 23:59:43 +0000 Subject: [PATCH 1/3] Preserve iterable backend selections in nested dictionaries Fixes #9238. Signed-off-by: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> --- CONTRIBUTORS.md | 1 + .../nested-backend-iterator.fixed.rst | 1 + source/isaaclab/isaaclab/utils/dict.py | 2 ++ source/isaaclab/test/utils/test_dict.py | 22 ++++++++++++++----- 4 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 source/isaaclab/changelog.d/nested-backend-iterator.fixed.rst diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 63c581fd6dac..06a9de465bdb 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -224,6 +224,7 @@ Guidelines for modifications: * Stefan Van de Mosselaer * Stephan Pleines * Sujal +* Sylvester Kaczmarek * Tiffany Chen * Trushant Adeshara * Tsz Ki GAO diff --git a/source/isaaclab/changelog.d/nested-backend-iterator.fixed.rst b/source/isaaclab/changelog.d/nested-backend-iterator.fixed.rst new file mode 100644 index 000000000000..cf5b2a622605 --- /dev/null +++ b/source/isaaclab/changelog.d/nested-backend-iterator.fixed.rst @@ -0,0 +1 @@ +* Fixed nested dictionary backend conversion when the requested array types are supplied as a one-shot iterable. diff --git a/source/isaaclab/isaaclab/utils/dict.py b/source/isaaclab/isaaclab/utils/dict.py index 0869b73d08e6..655ca2290097 100644 --- a/source/isaaclab/isaaclab/utils/dict.py +++ b/source/isaaclab/isaaclab/utils/dict.py @@ -246,6 +246,8 @@ def convert_dict_to_backend( # Define the conversion functions for each backend. tensor_type_conversions = TENSOR_TYPE_CONVERSIONS[backend] + # Materialize one-shot iterables before recursively processing nested dictionaries. + array_types = tuple(array_types) # Parse the array types and convert them to the corresponding types: "numpy" -> np.ndarray, etc. parsed_types = [] for t in array_types: diff --git a/source/isaaclab/test/utils/test_dict.py b/source/isaaclab/test/utils/test_dict.py index 86140c4e5656..a344d284d044 100644 --- a/source/isaaclab/test/utils/test_dict.py +++ b/source/isaaclab/test/utils/test_dict.py @@ -171,14 +171,24 @@ def test_update_class_from_dict_restores_enums_from_values(): assert all(isinstance(el, _Flavors) for el in cfg.cone) -def test_nested_conversion_preserves_requested_backend(): - """Nested conversions should preserve the backend selected by the caller.""" - data = {"outer": {"values": np.array([1.0, 2.0, 3.0], dtype=np.float32)}} +@pytest.mark.parametrize("type_selection", [tuple, iter], ids=["tuple", "iterator"]) +def test_nested_conversion_preserves_requested_backend(type_selection): + """Every branch must use the caller's original conversion selection.""" + data = { + "values": np.array([1.0], dtype=np.float32), + "outer": {"values": np.array([2.0], dtype=np.float32)}, + "sibling": {"inner": {"values": np.array([3.0], dtype=np.float32)}}, + } - converted = dict_utils.convert_dict_to_backend(data, backend="torch", array_types=("numpy",)) + converted = dict_utils.convert_dict_to_backend(data, backend="torch", array_types=type_selection(("numpy",))) - assert isinstance(converted["outer"]["values"], torch.Tensor) - torch.testing.assert_close(converted["outer"]["values"], torch.tensor([1.0, 2.0, 3.0])) + for value, expected in ( + (converted["values"], 1.0), + (converted["outer"]["values"], 2.0), + (converted["sibling"]["inner"]["values"], 3.0), + ): + assert isinstance(value, torch.Tensor) + torch.testing.assert_close(value, torch.tensor([expected])) def test_slice_round_trip_preserves_tuple_containers(): From 10697bd9967aa7744dff2ec182cc709ac7c5ef7f Mon Sep 17 00:00:00 2001 From: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:48:10 +0000 Subject: [PATCH 2/3] Validate iterator entries before collecting backend selections --- source/isaaclab/isaaclab/utils/dict.py | 7 ++++--- source/isaaclab/test/utils/test_dict.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/source/isaaclab/isaaclab/utils/dict.py b/source/isaaclab/isaaclab/utils/dict.py index 655ca2290097..c4067a2423ea 100644 --- a/source/isaaclab/isaaclab/utils/dict.py +++ b/source/isaaclab/isaaclab/utils/dict.py @@ -246,14 +246,15 @@ def convert_dict_to_backend( # Define the conversion functions for each backend. tensor_type_conversions = TENSOR_TYPE_CONVERSIONS[backend] - # Materialize one-shot iterables before recursively processing nested dictionaries. - array_types = tuple(array_types) + # Collect validated selections for recursion without exhausting an invalid iterator. + selected_types = [] # Parse the array types and convert them to the corresponding types: "numpy" -> np.ndarray, etc. parsed_types = [] for t in array_types: # Check type is valid. if t not in TENSOR_TYPES: raise ValueError(f"Unknown array type: '{t}'. Supported array types are 'numpy', 'torch', and 'warp'.") + selected_types.append(t) # Exclude types that match the backend, since we do not need to convert these. if t == backend: continue @@ -274,7 +275,7 @@ def convert_dict_to_backend( output_dict[key] = tensor_type_conversions[data_type](value) # -- nested dictionaries elif isinstance(value, dict): - output_dict[key] = convert_dict_to_backend(value, backend=backend, array_types=array_types) + output_dict[key] = convert_dict_to_backend(value, backend=backend, array_types=selected_types) # -- everything else else: output_dict[key] = value diff --git a/source/isaaclab/test/utils/test_dict.py b/source/isaaclab/test/utils/test_dict.py index a344d284d044..97590e81a31c 100644 --- a/source/isaaclab/test/utils/test_dict.py +++ b/source/isaaclab/test/utils/test_dict.py @@ -191,6 +191,16 @@ def test_nested_conversion_preserves_requested_backend(type_selection): torch.testing.assert_close(value, torch.tensor([expected])) +def test_conversion_rejects_unknown_type_before_advancing_iterator() -> None: + """An invalid selection must raise ValueError before reading later entries.""" + def selections(): + yield "unknown" + raise AssertionError("Invalid selection should stop iteration") + + with pytest.raises(ValueError, match="Unknown array type"): + dict_utils.convert_dict_to_backend({}, array_types=selections()) + + def test_slice_round_trip_preserves_tuple_containers(): """Slices nested in tuples should serialize and deserialize without changing container types.""" data = { From c29fae7f116d9fb1360b01eb769cb5bb0b834d7e Mon Sep 17 00:00:00 2001 From: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> Date: Wed, 2 Dec 2026 23:02:44 +0000 Subject: [PATCH 3/3] Format the iterator validation regression Signed-off-by: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> --- source/isaaclab/test/utils/test_dict.py | 1 + 1 file changed, 1 insertion(+) diff --git a/source/isaaclab/test/utils/test_dict.py b/source/isaaclab/test/utils/test_dict.py index 97590e81a31c..3d307de0ab23 100644 --- a/source/isaaclab/test/utils/test_dict.py +++ b/source/isaaclab/test/utils/test_dict.py @@ -193,6 +193,7 @@ def test_nested_conversion_preserves_requested_backend(type_selection): def test_conversion_rejects_unknown_type_before_advancing_iterator() -> None: """An invalid selection must raise ValueError before reading later entries.""" + def selections(): yield "unknown" raise AssertionError("Invalid selection should stop iteration")