Skip to content

Fix pyright errors in node-factory.py #30205

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 2 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 @@ -11,7 +11,6 @@ exclude = [
"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",
Expand Down
31 changes: 23 additions & 8 deletions src/bindings/python/src/openvino/utils/node_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


from functools import singledispatchmethod
from typing import Any, Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Union, cast, Sequence, TypeVar
from pathlib import Path

from openvino._pyopenvino import NodeFactory as _NodeFactory
Expand Down Expand Up @@ -52,15 +52,23 @@ def create(
if attributes is None:
attributes = {}

assert arguments is not None
assert arguments is not None # Questa asserzione aiuta Pyright a capire che arguments non è None

arguments = self._arguments_as_outputs(arguments)
node = self.factory.create(op_type_name, arguments, attributes)
# Qui uso cast per dichiarare esplicitamente il tipo del risultato
output_args = self._arguments_as_outputs(arguments)
node = self.factory.create(op_type_name, output_args, attributes)

return node

@singledispatchmethod
def add_extension(self, extension: Union[Path, str, Extension, List[Extension]]) -> None:
"""Add custom operations from an extension.

Base method that raises TypeError for unknown types.

:param extension: An extension path or object.
:raises TypeError: If the extension type is not supported.
"""
raise TypeError(f"Unknown argument type: {type(extension)}")

@add_extension.register(Path)
Expand Down Expand Up @@ -117,12 +125,19 @@ def _(self, extension: Union[Extension, List[Extension]]) -> None:

@staticmethod
def _arguments_as_outputs(arguments: List[Union[Node, Output]]) -> List[Output]:
outputs = []
"""Convert a list of Nodes and Outputs to a list of only Outputs.

:param arguments: The arguments to convert.
:return: A list containing only Output objects.
"""
outputs: List[Output] = []
for argument in arguments:
if issubclass(type(argument), Output):
outputs.append(argument)
if isinstance(argument, Output): # Usa isinstance invece di issubclass(type())
outputs.append(cast(Output, argument)) # Cast esplicito
else:
outputs.extend(argument.outputs())
# Se non è un Output, deve essere un Node
node_outputs = cast(Node, argument).outputs()
outputs.extend(node_outputs)
return outputs


Expand Down
124 changes: 88 additions & 36 deletions src/bindings/python/src/openvino/utils/node_factory.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,117 @@ from openvino._pyopenvino import NodeFactory as _NodeFactory
from openvino._pyopenvino import Output
from openvino.exceptions import UserInputError
from pathlib import Path
from typing import Any
from typing import Any, Dict, List, Optional, Union, cast
import typing
import openvino._pyopenvino
__all__ = ['Any', 'DEFAULT_OPSET', 'Extension', 'Node', 'NodeFactory', 'Output', 'Path', 'UserInputError', 'singledispatchmethod']

class NodeFactory:
"""
Factory front-end to create node objects.
"""
@staticmethod
def _arguments_as_outputs(arguments: typing.List[typing.Union[openvino._pyopenvino.Node, openvino._pyopenvino.Output]]) -> typing.List[openvino._pyopenvino.Output]:
...
@staticmethod
def add_extension(*args, **kwargs) -> None:
def _arguments_as_outputs(arguments: List[Union[openvino._pyopenvino.Node, openvino._pyopenvino.Output]]) -> List[openvino._pyopenvino.Output]:
"""
Convert a list of Nodes and Outputs to a list of only Outputs.

:param arguments: The arguments to convert.
:return: A list containing only Output objects.
"""
...
def _(self, extension: typing.Union[openvino._pyopenvino.Extension, typing.List[openvino._pyopenvino.Extension]]) -> None:

@singledispatchmethod
def add_extension(self, extension: Union[Path, str, Extension, List[Extension]]) -> None:
"""
Add custom operations from extension library.
Add custom operations from an extension.

Base method that raises TypeError for unknown types.

Extends operation types available for creation by operations
loaded from prebuilt C++ library. Enables instantiation of custom
operations exposed in that library without direct use of
operation classes. Other types of extensions, e.g. conversion
extensions, if they are exposed in the library, are ignored.
In case if an extension operation type from a library match
one of existing operations registered before (from the standard
OpenVINO opset or from another extension loaded earlier), a new
operation overrides an old operation.
:param extension: An extension path or object.
:raises TypeError: If the extension type is not supported.
"""
...

@add_extension.register(Path)
@add_extension.register(str)
def _(self, lib_path: Union[Path, str]) -> None:
"""
Add custom operations from an extension.

Version of an operation is ignored: an operation with a given type and
a given version/opset will override operation with the same type but
different version/opset in the same NodeFactory instance.
Use separate libraries and NodeFactory instances to differentiate
versions/opsets.
Extends operation types available for creation by operations
loaded from prebuilt C++ library. Enables instantiation of custom
operations exposed in that library without direct use of
operation classes. Other types of extensions, e.g. conversion
extensions, if they are exposed in the library, are ignored.

In case if an extension operation type from the extension match
one of existing operations registered before (from the standard
OpenVINO opset or from another extension loaded earlier), a new
operation overrides an old operation.

Version of an operation is ignored: an operation with a given type and
a given version/opset will override operation with the same type but
different version/opset in the same NodeFactory instance.
Use separate libraries and NodeFactory instances to differentiate
versions/opsets.

:param lib_path: A path to the library with extension.
"""
...

@add_extension.register(Extension)
@add_extension.register(list)
def _(self, extension: Union[Extension, List[Extension]]) -> None:
"""
Add custom operations from extension library.

:param extension: A single Extension or list of Extensions.

Extends operation types available for creation by operations
loaded from prebuilt C++ library. Enables instantiation of custom
operations exposed in that library without direct use of
operation classes. Other types of extensions, e.g. conversion
extensions, if they are exposed in the library, are ignored.

In case if an extension operation type from a library match
one of existing operations registered before (from the standard
OpenVINO opset or from another extension loaded earlier), a new
operation overrides an old operation.

Version of an operation is ignored: an operation with a given type and
a given version/opset will override operation with the same type but
different version/opset in the same NodeFactory instance.
Use separate libraries and NodeFactory instances to differentiate
versions/opsets.

:param extension: A single Extension or list of Extensions.
"""
...

def __init__(self, opset_version: str = 'opset13') -> None:
"""
Create the NodeFactory object.

:param opset_version: The opset version the factory will use to produce ops from.

:param opset_version: The opset version the factory will use to produce ops from.
"""
def create(self, op_type_name: str, arguments: typing.Optional[typing.List[typing.Union[openvino._pyopenvino.Node, openvino._pyopenvino.Output]]] = None, attributes: typing.Optional[typing.Dict[str, typing.Any]] = None) -> openvino._pyopenvino.Node:
...

def create(self, op_type_name: str, arguments: Optional[List[Union[openvino._pyopenvino.Node, openvino._pyopenvino.Output]]] = None, attributes: Optional[Dict[str, Any]] = None) -> openvino._pyopenvino.Node:
"""
Create node object from provided description.

The user does not have to provide all node's attributes, but only required ones.

:param op_type_name: The operator type name.
:param arguments: The operator arguments.
:param attributes: The operator attributes.

:return: Node object representing requested operator with attributes set.

The user does not have to provide all node's attributes, but only required ones.

:param op_type_name: The operator type name.
:param arguments: The operator arguments.
:param attributes: The operator attributes.

:return: Node object representing requested operator with attributes set.
"""
def _get_node_factory(opset_version: typing.Optional[str] = None) -> NodeFactory:
...

def _get_node_factory(opset_version: Optional[str] = None) -> NodeFactory:
"""
Return NodeFactory configured to create operators from specified opset version.
"""
...

DEFAULT_OPSET: str = 'opset13'