Skip to content

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

[PyOV] Add `pyright` check to GHA to prevent Python stubs regressions (#30129)

Przemyslaw Wysockie82c37788183
diff --git a/.github/workflows/py_checks.yml b/.github/workflows/py_checks.yml
index 3d55e654131..ac05cb23498 100644
--- a/.github/workflows/py_checks.yml
+++ b/.github/workflows/py_checks.yml
@@ -103,6 +103,11 @@ jobs:
- name: Run mypy on Python API
run: python -m mypy ./src/openvino --config-file ./setup.cfg
working-directory: src/bindings/python
+
+ # Run pyright to assert no regressions in type hinting
+ - name: Run pyright on Python API
+ run: python -m pyright ./src/openvino -p ./pyproject.toml
+ working-directory: src/bindings/python
# layer_tests Flake code-style
- name: Run flake8 on python tests in openvino/tests/layer_tests
diff --git a/src/bindings/python/pyproject.toml b/src/bindings/python/pyproject.toml
index bd7f29cc59c..b42c01e41e4 100644
--- a/src/bindings/python/pyproject.toml
+++ b/src/bindings/python/pyproject.toml
@@ -1,3 +1,24 @@
[tool.black]
line-length = 160
include = '\.pyi?$'
+
+[tool.pyright]
+exclude = [
+ "src/openvino/utils/data_helpers/data_dispatcher.py",
+ "src/openvino/utils/types.py",
+ "src/openvino/**/ops.py",
+ "src/openvino/utils/decorators.py",
+ "src/openvino/frontend/frontend.py",
+ "src/openvino/_ov_api.py",
+ "src/openvino/_op_base.py",
+ "src/openvino/utils/node_factory.py",
+ "src/openvino/frontend/tensorflow",
+ "src/openvino/frontend/pytorch",
+ "src/openvino/frontend/jax",
+]
+reportInvalidTypeForm = false
+reportMissingModuleSource = false
+reportFunctionMemberAccess = false
+reportAttributeAccessIssue = false
+reportMissingImports = false
+reportIncompatibleMethodOverride = false
diff --git a/src/bindings/python/requirements_test.txt b/src/bindings/python/requirements_test.txt
index c3425f6e06c..b65ec38c7f2 100644
--- a/src/bindings/python/requirements_test.txt
+++ b/src/bindings/python/requirements_test.txt
@@ -38,3 +38,4 @@ types-setuptools
wheel
pybind11-stubgen<2.6
pre-commit<4.3
+pyright<=1.1.399
diff --git a/src/bindings/python/src/openvino/_ov_api.py b/src/bindings/python/src/openvino/_ov_api.py
index ef1e928d4f1..0a2f4396c05 100644
--- a/src/bindings/python/src/openvino/_ov_api.py
+++ b/src/bindings/python/src/openvino/_ov_api.py
@@ -4,7 +4,7 @@
import io
from types import TracebackType
-from typing import Any, Iterable, Union, Optional, Dict
+from typing import Any, Iterator, Union, Optional, Dict
from typing import Type as TypingType
from pathlib import Path
import traceback # noqa: F811
@@ -442,7 +442,7 @@ class AsyncInferQueue(AsyncInferQueueBase):
a simple pipeline.
"""
- def __iter__(self) -> Iterable[InferRequest]:
+ def __iter__(self) -> Iterator[InferRequest]:
"""Allows to iterate over AsyncInferQueue.
Resulting objects are guaranteed to work with read-only methods like getting tensors.
diff --git a/src/bindings/python/src/openvino/_ov_api.pyi b/src/bindings/python/src/openvino/_ov_api.pyi
index 522e62c4ba2..e85847937b2 100644
--- a/src/bindings/python/src/openvino/_ov_api.pyi
+++ b/src/bindings/python/src/openvino/_ov_api.pyi
@@ -48,7 +48,7 @@ class AsyncInferQueue(openvino._pyopenvino.AsyncInferQueue):
:rtype: openvino.InferRequest
"""
- def __iter__(self) -> typing.Iterable[openvino._ov_api.InferRequest]:
+ def __iter__(self) -> typing.Iterator[openvino._ov_api.InferRequest]:
"""
Allows to iterate over AsyncInferQueue.
diff --git a/src/bindings/python/src/openvino/frontend/tensorflow/node_decoder.py b/src/bindings/python/src/openvino/frontend/tensorflow/node_decoder.py
index 5fa69b99fb1..eb116722c1a 100644
--- a/src/bindings/python/src/openvino/frontend/tensorflow/node_decoder.py
+++ b/src/bindings/python/src/openvino/frontend/tensorflow/node_decoder.py
@@ -175,7 +175,7 @@ class TFGraphNodeDecoder(DecoderBase):
if name == "value":
if self.m_data_type == 'string':
return OVAny(Tensor(self.m_parsed_content))
- if self.m_parsed_content.size == 1:
+ if self.m_parsed_content.size == 1: # type: ignore
if isinstance(self.m_parsed_content, np.ndarray):
return OVAny(Tensor(self.m_parsed_content))
self.m_parsed_content = np.array(self.m_parsed_content)
diff --git a/src/bindings/python/src/openvino/package_utils.py b/src/bindings/python/src/openvino/package_utils.py
index 13262a09372..fabb0cec2a6 100644
--- a/src/bindings/python/src/openvino/package_utils.py
+++ b/src/bindings/python/src/openvino/package_utils.py
@@ -93,7 +93,7 @@ class _ClassPropertyDescriptor(object):
def classproperty(func: Any) -> _ClassPropertyDescriptor:
if not isinstance(func, (classmethod, staticmethod)):
func = classmethod(func)
- return _ClassPropertyDescriptor(func)
+ return _ClassPropertyDescriptor(func) # type: ignore
def deprecatedclassproperty(name: Any = None, version: str = "", message: str = "", stacklevel: int = 2) -> Callable[[Any], _ClassPropertyDescriptor]:
diff --git a/src/bindings/python/src/openvino/preprocess/torchvision/torchvision_preprocessing.py b/src/bindings/python/src/openvino/preprocess/torchvision/torchvision_preprocessing.py
index 44a80bd48b2..f56dec47a75 100644
--- a/src/bindings/python/src/openvino/preprocess/torchvision/torchvision_preprocessing.py
+++ b/src/bindings/python/src/openvino/preprocess/torchvision/torchvision_preprocessing.py
@@ -167,7 +167,7 @@ class _(TransformConverterBase):
@custom_preprocess_function
def broadcast_node(output: Output) -> Callable: # type: ignore[name-defined]
- return ops.broadcast(
+ return ops.broadcast( # type: ignore
data=output,
target_shape=input_shape,
)
@@ -223,7 +223,7 @@ class _(TransformConverterBase):
@custom_preprocess_function
def pad_node(output: Output) -> Callable:
- return ops.pad(
+ return ops.pad( # type: ignore
output,
pad_mode=pad_mode,
pads_begin=pads_begin,
diff --git a/src/bindings/python/src/openvino/utils/postponed_constant.py b/src/bindings/python/src/openvino/utils/postponed_constant.py
index 20fb74b79fd..3c0dc280286 100644
--- a/src/bindings/python/src/openvino/utils/postponed_constant.py
+++ b/src/bindings/python/src/openvino/utils/postponed_constant.py
@@ -19,7 +19,7 @@ class PostponedConstant(Op):
self.friendly_name = name
self.constructor_validate_and_infer_types()
- def evaluate(self, outputs: List[Tensor], _: List[Tensor]) -> bool:
+ def evaluate(self, outputs: List[Tensor], _: List[Tensor]) -> bool: # type: ignore
self.m_maker(outputs[0])
return True
diff --git a/src/bindings/python/src/openvino/utils/types.py b/src/bindings/python/src/openvino/utils/types.py
index ed7d29aa535..3db86f1ab8a 100644
--- a/src/bindings/python/src/openvino/utils/types.py
+++ b/src/bindings/python/src/openvino/utils/types.py
@@ -161,7 +161,7 @@ def make_constant_node(value: NumericData, dtype: Union[NumericType, Type] = Non
return const
-def as_node(input_value: NodeInput, name: Optional[str] = None) -> Node:
+def as_node(input_value: NodeInput, name: Optional[str] = None) -> Union[Node, Output]:
"""Return input values as nodes. Scalars will be converted to Constant nodes."""
if issubclass(type(input_value), Node):
return input_value
diff --git a/src/bindings/python/src/openvino/utils/types.pyi b/src/bindings/python/src/openvino/utils/types.pyi
index 6dff2339038..d9591cd8137 100644
--- a/src/bindings/python/src/openvino/utils/types.pyi
+++ b/src/bindings/python/src/openvino/utils/types.pyi
@@ -16,7 +16,7 @@ import typing
Functions related to converting between Python and numpy types and openvino types.
"""
__all__ = ['Constant', 'Node', 'NodeInput', 'NumericData', 'NumericType', 'OVTypeError', 'Output', 'ScalarData', 'Shape', 'TensorShape', 'Type', 'as_node', 'as_nodes', 'get_dtype', 'get_element_type', 'get_element_type_str', 'get_ndarray', 'get_numpy_ctype', 'get_shape', 'log', 'logging', 'make_constant_node', 'np', 'openvino_to_numpy_types_map', 'openvino_to_numpy_types_str_map']
-def as_node(input_value: typing.Union[openvino._pyopenvino.Node, int, float, numpy.ndarray], name: typing.Optional[str] = None) -> openvino._pyopenvino.Node:
+def as_node(input_value: typing.Union[openvino._pyopenvino.Node, int, float, numpy.ndarray], name: typing.Optional[str] = None) -> typing.Union[openvino._pyopenvino.Node, openvino._pyopenvino.Output]:
"""
Return input values as nodes. Scalars will be converted to Constant nodes.
"""