OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[PyOV] Return a shallow copy from OVDict.to_dict (#39179)
Somay kousis98d02b15965f
diff --git a/src/bindings/python/src/openvino/utils/data_helpers/wrappers.py b/src/bindings/python/src/openvino/utils/data_helpers/wrappers.pyindex e68ca8b6fa7..202cee7b2c9 100644--- a/src/bindings/python/src/openvino/utils/data_helpers/wrappers.py+++ b/src/bindings/python/src/openvino/utils/data_helpers/wrappers.py@@ -137,12 +137,12 @@ class OVDict(Mapping):return tuple(self._names.values())def to_dict(self) -> dict[ConstOutput, np.ndarray]:- """Return underlaying native dictionary.+ """Return a shallow copy of the underlying native dictionary.Function performs shallow copy, thus any modifications toreturned values may affect this class as well."""- return self._dict+ return dict(self._dict)def to_tuple(self) -> tuple:"""Convert values of this dictionary to a tuple."""diff --git a/src/bindings/python/src/openvino/utils/data_helpers/wrappers.pyi b/src/bindings/python/src/openvino/utils/data_helpers/wrappers.pyiindex 88ba18b6bd1..0e42b354cb4 100644--- a/src/bindings/python/src/openvino/utils/data_helpers/wrappers.pyi+++ b/src/bindings/python/src/openvino/utils/data_helpers/wrappers.pyi@@ -94,7 +94,7 @@ class OVDict(collections.abc.Mapping):"""def to_dict(self) -> dict[openvino._pyopenvino.ConstOutput, numpy.ndarray]:"""- Return underlaying native dictionary.+ Return a shallow copy of the underlying native dictionary.Function performs shallow copy, thus any modifications toreturned values may affect this class as well.diff --git a/src/bindings/python/tests/test_runtime/test_ovdict.py b/src/bindings/python/tests/test_runtime/test_ovdict.pyindex 24b88e42b4f..c870f1ea118 100644--- a/src/bindings/python/tests/test_runtime/test_ovdict.py+++ b/src/bindings/python/tests/test_runtime/test_ovdict.py@@ -210,6 +210,26 @@ def test_ovdict_single_output_dict(device, is_direct, use_function):assert "0" in str(e.value)[email protected]("is_direct", [True, False])+def test_ovdict_to_dict_returns_shallow_copy(device, is_direct):+ result, obj = _get_ovdict(+ device,+ multi_output=False,+ direct_infer=is_direct,+ )+ outs = obj.model_outputs if isinstance(obj, InferRequest) else obj.outputs+ value = result[outs[0]]++ native_dict = result.to_dict()+ # Shallow copy: same arrays, but changing the dictionary must not change the OVDict+ assert native_dict[outs[0]] is value+ native_dict.clear()++ assert len(result) == 1+ assert result[outs[0]] is value+ assert result.to_dict() is not result.to_dict()++@pytest.mark.parametrize("is_direct", [True, False])def test_ovdict_multi_output_basic(device, is_direct):output_names = ["output_0", "output_1", "output_2", "output_3", "output_4"]