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..c4067a2423ea 100644 --- a/source/isaaclab/isaaclab/utils/dict.py +++ b/source/isaaclab/isaaclab/utils/dict.py @@ -246,12 +246,15 @@ def convert_dict_to_backend( # Define the conversion functions for each backend. tensor_type_conversions = TENSOR_TYPE_CONVERSIONS[backend] + # 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 @@ -272,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 86140c4e5656..3d307de0ab23 100644 --- a/source/isaaclab/test/utils/test_dict.py +++ b/source/isaaclab/test/utils/test_dict.py @@ -171,14 +171,35 @@ 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=type_selection(("numpy",))) + + 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_conversion_rejects_unknown_type_before_advancing_iterator() -> None: + """An invalid selection must raise ValueError before reading later entries.""" - converted = dict_utils.convert_dict_to_backend(data, backend="torch", array_types=("numpy",)) + def selections(): + yield "unknown" + raise AssertionError("Invalid selection should stop iteration") - assert isinstance(converted["outer"]["values"], torch.Tensor) - torch.testing.assert_close(converted["outer"]["values"], torch.tensor([1.0, 2.0, 3.0])) + 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():