From 14cee3ecfc58f660a974600c7d2a85990066189f Mon Sep 17 00:00:00 2001 From: Alicja Miloszewska Date: Wed, 15 Jan 2025 23:00:26 +0100 Subject: [PATCH] [Py OV] Add deprecation warning for runtime module (#27694) ### Details: Add deprecation warning for `openvino.runtime` that will be shown **ONCE** when sb will access runtime functionality for the first time. Examples: ![Screenshot 2025-01-10 114701](https://github.com/user-attachments/assets/4229ede3-3b86-418a-8dbf-f230ff91983b) ![warn_updated](https://github.com/user-attachments/assets/4b4a7c56-c5ca-4b7b-8b34-f89f3a4bc627) `openvino.runtime` funtionality was added to openvino namespace in these PRs: - https://github.com/openvinotoolkit/openvino/pull/27785 - https://github.com/openvinotoolkit/openvino/pull/27902 - https://github.com/openvinotoolkit/openvino/pull/28007 - https://github.com/openvinotoolkit/openvino/pull/28062 - https://github.com/openvinotoolkit/openvino/pull/28085 Internal calls in `openvino` module also triggered warning, so they were updated: - https://github.com/openvinotoolkit/openvino/pull/28166 - https://github.com/openvinotoolkit/openvino/pull/28356 ### Tickets: - [CVS-129451](https://jira.devtools.intel.com/browse/CVS-129451) --------- Signed-off-by: Alicja Miloszewska Co-authored-by: Anastasia Kuporosova --- src/bindings/python/src/openvino/__init__.py | 3 ++- .../python/src/openvino/package_utils.py | 18 ++++++++++++++++++ .../python/src/openvino/runtime/__init__.py | 10 ++++++++++ .../test_runtime/test_deprecated_runtime.py | 18 +++++++++++++++--- tools/benchmark_tool/openvino/__init__.py | 3 ++- tools/ovc/openvino/__init__.py | 3 ++- 6 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/bindings/python/src/openvino/__init__.py b/src/bindings/python/src/openvino/__init__.py index 70b5f372352bc8..b916a6e1c9bfc5 100644 --- a/src/bindings/python/src/openvino/__init__.py +++ b/src/bindings/python/src/openvino/__init__.py @@ -60,7 +60,8 @@ from openvino._ov_api import Op # Import all public modules -from openvino import runtime as runtime +from openvino.package_utils import lazy_import +runtime = lazy_import("openvino.runtime") from openvino import frontend as frontend from openvino import helpers as helpers from openvino import experimental as experimental diff --git a/src/bindings/python/src/openvino/package_utils.py b/src/bindings/python/src/openvino/package_utils.py index a863ef9ea7b267..feafc35c5ae50e 100644 --- a/src/bindings/python/src/openvino/package_utils.py +++ b/src/bindings/python/src/openvino/package_utils.py @@ -7,6 +7,8 @@ from functools import wraps from typing import Callable, Any from pathlib import Path +import importlib.util +from types import ModuleType def _add_openvino_libs_to_search_path() -> None: @@ -113,3 +115,19 @@ def __get__(self, obj: Any, cls: Any = None) -> Any: _patch(func, deprecated(name, version, message, stacklevel)) return func return decorator + + +def lazy_import(module_name: str) -> ModuleType: + spec = importlib.util.find_spec(module_name) + if spec is None or spec.loader is None: + raise ImportError(f"Module {module_name} not found") + + loader = importlib.util.LazyLoader(spec.loader) + module = importlib.util.module_from_spec(spec) + sys.modules[module_name] = module + + try: + loader.exec_module(module) + except Exception as e: + raise ImportError(f"Failed to load module {module_name}") from e + return module diff --git a/src/bindings/python/src/openvino/runtime/__init__.py b/src/bindings/python/src/openvino/runtime/__init__.py index ef985c66859d9d..431689eee27dea 100644 --- a/src/bindings/python/src/openvino/runtime/__init__.py +++ b/src/bindings/python/src/openvino/runtime/__init__.py @@ -5,6 +5,16 @@ """openvino module namespace, exposing factory functions for all ops and other classes.""" # noqa: F401 +import warnings +warnings.simplefilter("always", DeprecationWarning) +warnings.warn( + "The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release. " + "Please replace `openvino.runtime` with `openvino`.", + DeprecationWarning, + stacklevel=2 +) + + from openvino._pyopenvino import get_version __version__ = get_version() diff --git a/src/bindings/python/tests/test_runtime/test_deprecated_runtime.py b/src/bindings/python/tests/test_runtime/test_deprecated_runtime.py index 05d78e850cc5f5..2ae4ee1b0d167f 100644 --- a/src/bindings/python/tests/test_runtime/test_deprecated_runtime.py +++ b/src/bindings/python/tests/test_runtime/test_deprecated_runtime.py @@ -6,9 +6,12 @@ import numpy as np from pathlib import Path from contextlib import nullcontext as does_not_raise +import warnings +with pytest.warns(DeprecationWarning, match="The `openvino.runtime` module is deprecated and will be removed in the 2026.0 release."): + import openvino.runtime as ov -import openvino.runtime as ov +import openvino.runtime.opset13 as ops from openvino.runtime import ( Model, Core, @@ -19,8 +22,6 @@ serialize, Type, ) - -import openvino.runtime.opset13 as ops import openvino.runtime.opset8 as ops8 from openvino.runtime.op import Constant, Parameter from openvino.runtime import Extension @@ -42,6 +43,17 @@ ) +def test_no_warning(): + with warnings.catch_warnings(record=True) as w: + import openvino + + data = np.array([1, 2, 3]) + axis_vector = openvino.AxisVector(data) + assert np.equal(axis_vector, data).all() + + assert len(w) == 0 # No warning + + # request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request def test_read_model_from_ir(request, tmp_path): core = Core() diff --git a/tools/benchmark_tool/openvino/__init__.py b/tools/benchmark_tool/openvino/__init__.py index 70b5f372352bc8..b916a6e1c9bfc5 100644 --- a/tools/benchmark_tool/openvino/__init__.py +++ b/tools/benchmark_tool/openvino/__init__.py @@ -60,7 +60,8 @@ from openvino._ov_api import Op # Import all public modules -from openvino import runtime as runtime +from openvino.package_utils import lazy_import +runtime = lazy_import("openvino.runtime") from openvino import frontend as frontend from openvino import helpers as helpers from openvino import experimental as experimental diff --git a/tools/ovc/openvino/__init__.py b/tools/ovc/openvino/__init__.py index 70b5f372352bc8..b916a6e1c9bfc5 100644 --- a/tools/ovc/openvino/__init__.py +++ b/tools/ovc/openvino/__init__.py @@ -60,7 +60,8 @@ from openvino._ov_api import Op # Import all public modules -from openvino import runtime as runtime +from openvino.package_utils import lazy_import +runtime = lazy_import("openvino.runtime") from openvino import frontend as frontend from openvino import helpers as helpers from openvino import experimental as experimental