Skip to content

Commit

Permalink
[Py OV] Add deprecation warning for runtime module (#27694)
Browse files Browse the repository at this point in the history
### 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:
- #27785
- #27902
- #28007
- #28062
- #28085

Internal calls in `openvino` module also triggered warning, so they were
updated:
 - #28166 
 - #28356

### Tickets:
 - [CVS-129451](https://jira.devtools.intel.com/browse/CVS-129451)

---------

Signed-off-by: Alicja Miloszewska <alicja.miloszewska@intel.com>
Co-authored-by: Anastasia Kuporosova <anastasia.kuporosova@intel.com>
  • Loading branch information
almilosz and akuporos authored Jan 15, 2025
1 parent 40b19c8 commit 14cee3e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/bindings/python/src/openvino/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/bindings/python/src/openvino/package_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions src/bindings/python/src/openvino/runtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 15 additions & 3 deletions src/bindings/python/tests/test_runtime/test_deprecated_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion tools/benchmark_tool/openvino/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tools/ovc/openvino/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 14cee3e

Please sign in to comment.