Skip to content

Fix pyright issue #30147 #30192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/bindings/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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",
Expand Down
58 changes: 21 additions & 37 deletions src/bindings/python/src/openvino/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""Functions related to converting between Python and numpy types and openvino types."""

import logging
from typing import List, Union, Optional
from typing import List, Union, Optional, cast

import numpy as np

Expand All @@ -19,7 +19,7 @@
NumericData = Union[int, float, np.ndarray]
NumericType = Union[type, np.dtype]
ScalarData = Union[int, float]
NodeInput = Union[Node, NumericData]
NodeInput = Union[Node, Output, NumericData]

openvino_to_numpy_types_map = [
(Type.boolean, bool),
Expand Down Expand Up @@ -73,12 +73,9 @@ def get_element_type(data_type: NumericType) -> Type:
log.warning("Converting float type of undefined bitwidth to 32-bit ngraph float.")
return Type.f32

ov_type = next(
(ov_type for (ov_type, np_type) in openvino_to_numpy_types_map if np_type == data_type),
None,
)
if ov_type:
return ov_type
for ov_type, np_type in openvino_to_numpy_types_map:
if np_type == data_type:
return ov_type

raise OVTypeError("Unidentified data type %s", data_type)

Expand All @@ -93,59 +90,48 @@ def get_element_type_str(data_type: NumericType) -> str:
log.warning("Converting float type of undefined bitwidth to 32-bit ngraph float.")
return "f32"

ov_type = next(
(ov_type for (ov_type, np_type) in openvino_to_numpy_types_str_map if np_type == data_type),
None,
)
if ov_type:
return ov_type
for ov_type, np_type in openvino_to_numpy_types_str_map:
if np_type == data_type:
return ov_type

raise OVTypeError("Unidentified data type %s", data_type)


def get_dtype(openvino_type: Type) -> np.dtype:
"""Return a numpy.dtype for an openvino element type."""
np_type = next(
(np_type for (ov_type, np_type) in openvino_to_numpy_types_map if ov_type == openvino_type),
None,
)

if np_type:
return np.dtype(np_type)
for ov_type, np_type in openvino_to_numpy_types_map:
if ov_type == openvino_type:
return np.dtype(np_type)

raise OVTypeError("Unidentified data type %s", openvino_type)


def get_numpy_ctype(openvino_type: Type) -> type:
"""Return numpy ctype for an openvino element type."""
np_type = next(
(np_type for (ov_type, np_type) in openvino_to_numpy_types_map if ov_type == openvino_type),
None,
)

if np_type:
return np_type
for ov_type, np_type in openvino_to_numpy_types_map:
if ov_type == openvino_type:
return np_type

raise OVTypeError("Unidentified data type %s", openvino_type)


def get_ndarray(data: NumericData) -> np.ndarray:
"""Wrap data into a numpy ndarray."""
if isinstance(data, np.ndarray):
return data # type: ignore
return data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary change

return np.array(data)


def get_shape(data: NumericData) -> TensorShape:
"""Return a shape of NumericData."""
if isinstance(data, np.ndarray):
return data.shape # type: ignore
return data.shape
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary change

if isinstance(data, list):
return [len(data)] # type: ignore
return [len(data)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary change

return []


def make_constant_node(value: NumericData, dtype: Union[NumericType, Type] = None, *, name: Optional[str] = None) -> Constant:
def make_constant_node(value: NumericData, dtype: Optional[Union[NumericType, Type]] = None, *, name: Optional[str] = None) -> Constant:
"""Return an openvino Constant node with the specified value."""
ndarray = get_ndarray(value)
if dtype is not None:
Expand All @@ -163,13 +149,11 @@ def make_constant_node(value: NumericData, dtype: Union[NumericType, Type] = Non

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
if issubclass(type(input_value), Output):
if isinstance(input_value, (Node, Output)):
return input_value
return make_constant_node(input_value, name=name)
return cast(Node, make_constant_node(input_value, name=name))


def as_nodes(*input_values: NodeInput, name: Optional[str] = None) -> List[Node]:
"""Return input values as nodes. Scalars will be converted to Constant nodes."""
return [as_node(input_value, name=name) for input_value in input_values]
return cast(List[Node], [as_node(input_value, name=name) for input_value in input_values])