Skip to content

Fix #30149: Fix Pyright errors in utils/decorators.py #30194

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

Closed
wants to merge 12 commits into from
Closed
1 change: 0 additions & 1 deletion src/bindings/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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",
Expand Down
26 changes: 14 additions & 12 deletions src/bindings/python/src/openvino/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

from functools import wraps
from inspect import signature
from typing import Any, Callable, Dict, Optional, Union, get_origin, get_args
from typing import Any, Callable, Dict, Optional, Union, get_origin, get_args, Type, TypeVar, cast

from openvino import Node, Output
from openvino.utils.types import NodeInput, as_node, as_nodes


def _get_name(**kwargs: Any) -> Node:
def _get_name(**kwargs: Any) -> Optional[str]: # Corretto il tipo di ritorno
Copy link
Contributor

Choose a reason for hiding this comment

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

if needed, comments should be in english, please :)

if "name" in kwargs:
return kwargs["name"]
return None
Expand Down Expand Up @@ -76,16 +76,16 @@ def __init__(self, name: str):
self.typemap: Dict[tuple, Callable] = {}

# Checks if actual_type is a subclass of any type in the union
def matches_union(self, union_type, actual_type) -> bool: # type: ignore
def matches_union(self, union_type: Any, actual_type: Type) -> bool:
for type_arg in get_args(union_type):
if isinstance(type_arg, type) and issubclass(actual_type, type_arg):
if isinstance(type_arg, type) and isinstance(actual_type, type) and issubclass(actual_type, type_arg):
return True
elif get_origin(type_arg) == list:
if issubclass(actual_type, list):
if isinstance(actual_type, type) and issubclass(actual_type, list):
return True
Copy link
Contributor

@mlukasze mlukasze Apr 18, 2025

Choose a reason for hiding this comment

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

it could be one-line condition, elif itself makes no sense here, imho, nesting for sure
@akuporos what's your opinion?

return False

def matches_optional(self, optional_type, actual_type) -> bool: # type: ignore
def matches_optional(self, optional_type: Any, actual_type: Any) -> bool:
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 actual_type is None or self.matches_union(optional_type, actual_type)

# Checks whether there is overloading which matches invoked argument types
Expand All @@ -98,11 +98,11 @@ def check_invoked_types_in_overloaded_funcs(self, tuple_to_check: tuple, key_str
elif origin is Optional:
if not self.matches_optional(expected_type, actual_type):
return False
elif not issubclass(actual_type, expected_type):
elif isinstance(actual_type, type) and isinstance(expected_type, type) and not issubclass(actual_type, expected_type):
return False
return True

def __call__(self, *args, **kwargs) -> Any: # type: ignore
def __call__(self, *args: Any, **kwargs: Any) -> Any:
arg_types = tuple(arg.__class__ for arg in args)
kwarg_types = {key: type(value) for key, value in kwargs.items()}

Expand All @@ -115,14 +115,14 @@ def __call__(self, *args, **kwargs) -> Any: # type: ignore
break
elif len(arg_types) == 0 and len(kwarg_types) != 0:
for key, func in self.typemap.items():
func_signature = {arg_name: types.annotation for arg_name, types in signature(func).parameters.items()}
func_signature = {arg_name: param.annotation for arg_name, param in signature(func).parameters.items()}
Copy link
Contributor

Choose a reason for hiding this comment

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

why did you rename variables here?

# if kwargs of called function are subset of overloaded function, we use this overload
if kwarg_types.keys() <= func_signature.keys():
key_matched = key
break
elif len(arg_types) != 0 and len(kwarg_types) != 0:
for key, func in self.typemap.items():
func_signature = {arg_name: types.annotation for arg_name, types in signature(func).parameters.items()}
func_signature = {arg_name: param.annotation for arg_name, param in signature(func).parameters.items()}
Copy link
Contributor

Choose a reason for hiding this comment

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

there is still param instead of types

# compare types of called function with overloads
if self.check_invoked_types_in_overloaded_funcs(arg_types, tuple(func_signature.values())):
# if kwargs of called function are subset of overloaded function, we use this overload
Expand All @@ -134,7 +134,9 @@ def __call__(self, *args, **kwargs) -> Any: # type: ignore
raise TypeError(f"The necessary overload for {self.name} was not found")

function = self.typemap.get(key_matched)
return function(*args, **kwargs) # type: ignore
if function:
return function(*args, **kwargs)
raise TypeError(f"Implementation not found for {self.name} with given types")

def register(self, types: tuple, function: Callable) -> None:
if types in self.typemap:
Expand All @@ -153,4 +155,4 @@ def register(function: Callable) -> MultiMethod:
mm = registry[name] = MultiMethod(name)
mm.register(types, function)
return mm
return register
return register
Loading