Skip to content

[PyOV] Return a shallow copy from OVDict.to_dict - #39179

Open
Somay-kousis wants to merge 1 commit into
openvinotoolkit:masterfrom
Somay-kousis:fix/ovdict-to-dict-copy
Open

Somay-kousis wants to merge 1 commit into
openvinotoolkit:masterfrom
Somay-kousis:fix/ovdict-to-dict-copy

Conversation

@Somay-kousis

Copy link
Copy Markdown
Contributor

Details:

  • What a user sees: result.to_dict().clear() empties result, because to_dict() returns the OVDict's own dictionary. The docstrings say OVDict is immutable and to_dict returns a shallow copy.

    master this PR
    result.to_dict() is result.to_dict() True False
    d = result.to_dict(); d.clear(); len(result) 0 number of outputs
    result.to_dict()[port] is result[port] (same array) True True
  • Fix: to_dict returns dict(self._dict). The arrays are still shared, so writing into an array in place still shows in the OVDict, as the docstring says ("modifications to returned values may affect this class"). The summary line now says it returns a shallow copy, in wrappers.py and wrappers.pyi.

  • Behaviour change: code that added or removed keys in to_dict() to change the OVDict no longer does. I found no such use in openvinotoolkit repos (git grep over master, GitHub code search).

  • New test test_ovdict_to_dict_returns_shallow_copy in tests/test_runtime/test_ovdict.py.

Tickets:

AI Assistance:

  • AI assistance used: yes
  • AI tools helped find the bug and write the fix and the test; I reviewed every line. Validation on macOS arm64 with the 2026.5.0 nightly wheel, using this branch's wrappers.py:
    • The 2 new test cases pass; with master's wrappers.py both fail (len(result) == 0 after clear()). A deep copy also fails them, since the test checks the arrays are the same objects.
    • test_ovdict.py: 22 passed. test_sync_infer_request.py, test_async_infer_request.py, test_compiled_model.py: 221 passed, 7 skipped.
    • flake8 (max-line-length 120) on wrappers.py: clean.

Signed-off-by: Somay-kousis <somaykaush@gmail.com>
@Somay-kousis
Somay-kousis requested a review from a team as a code owner October 24, 2026 23:38
@github-actions github-actions Bot added the category: Python API OpenVINO Python bindings label Oct 24, 2026
@sys-openvino-ci sys-openvino-ci added the ExternalPR External contributor label Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: Python API OpenVINO Python bindings ExternalPR External contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: [PyOV] OVDict.to_dict() returns the internal dict, so editing it changes the result

2 participants