-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
sanleo-wq
wants to merge
4
commits into
openvinotoolkit:master
Choose a base branch
from
sanleo-wq:fix-30147
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+21
−38
Open
Fix pyright issue #30147 #30192
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
027142e
Fix #30147: Fix Pyright errors in utils/types.py
sanleo-wq 68cc642
Fix #30147: Fix Pyright errors in utils/types.py
sanleo-wq 50a15fb
Merge branch 'openvinotoolkit:master' into fix-30147
sanleo-wq 7da4c84
fix the errors in types.py
sanleo-wq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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), | ||
|
@@ -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) | ||
|
||
|
@@ -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 | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
@@ -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]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary change