Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[Py OV] Support pathlib.Path as ov::Any (#33843)

Alicja Miloszewskae18b3496a271
diff --git a/src/bindings/python/src/pyopenvino/utils/utils.cpp b/src/bindings/python/src/pyopenvino/utils/utils.cpp
index ed720e77e80..dc795a9702e 100644
--- a/src/bindings/python/src/pyopenvino/utils/utils.cpp
+++ b/src/bindings/python/src/pyopenvino/utils/utils.cpp
@@ -453,8 +453,9 @@ std::tuple<Args...> tuple_from_py_tuple(const py::tuple& py_tuple) {
ov::Any py_object_to_any(const py::object& py_obj) {
// Python types
py::object float_32_type = py::module_::import("numpy").attr("float32");
- if (py::isinstance<py::str>(py_obj)) {
- return py_obj.cast<std::string>();
+ py::object Path = py::module_::import("pathlib").attr("Path");
+ if (py::isinstance<py::str>(py_obj) || py::isinstance(py_obj, Path)) {
+ return py::str(py_obj).cast<std::string>();
} else if (py::isinstance<py::bool_>(py_obj)) {
return py_obj.cast<bool>();
} else if (py::isinstance<py::bytes>(py_obj)) {
diff --git a/src/bindings/python/tests/test_runtime/test_core.py b/src/bindings/python/tests/test_runtime/test_core.py
index fc0e994eea4..eb0c88f247e 100644
--- a/src/bindings/python/tests/test_runtime/test_core.py
+++ b/src/bindings/python/tests/test_runtime/test_core.py
@@ -209,7 +209,7 @@ def test_read_model_as_path_with_user_config(request, tmp_path):
core_cache_dir = core.get_property("CACHE_DIR")
cache_path = tmp_path / Path("cache_as_path")
- model = core.read_model(Path(xml_path), Path(bin_path), config={"CACHE_DIR": f"{cache_path}"})
+ model = core.read_model(Path(xml_path), Path(bin_path), config={"CACHE_DIR": cache_path})
assert isinstance(model, Model)
assert core_cache_dir == core.get_property("CACHE_DIR")
diff --git a/src/bindings/python/tests/test_runtime/test_properties.py b/src/bindings/python/tests/test_runtime/test_properties.py
index f7617212bd2..48e82234b26 100644
--- a/src/bindings/python/tests/test_runtime/test_properties.py
+++ b/src/bindings/python/tests/test_runtime/test_properties.py
@@ -5,6 +5,7 @@
import pytest
import numpy as np
import os
+from pathlib import Path
import openvino as ov
import openvino.properties as props
@@ -646,6 +647,13 @@ def test_single_property_setting(device):
assert isinstance(core.get_property(device, streams.num()), int)
+def test_property_pathlib_path(device):
+ core = Core()
+
+ core.set_property(device, {"CACHE_DIR": Path("./test_cache")})
+ assert core.get_property(device, props.cache_dir) == str(Path("./test_cache"))
+
+
@pytest.mark.skipif(
os.environ.get("TEST_DEVICE", "CPU") != "CPU",
reason=f"Cannot run test on device {os.environ.get('TEST_DEVICE')}, Plugin specific test"