-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Changes from 1 commit
38c7574
9e0c770
a30b5f7
452537e
6b259e9
78705da
0144e45
6b41701
c18453c
da2946b
e86952d
cf539c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
if "name" in kwargs: | ||
return kwargs["name"] | ||
return None | ||
|
@@ -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 | ||
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. it could be one-line condition, elif itself makes no sense here, imho, nesting for sure |
||
return False | ||
|
||
def matches_optional(self, optional_type, actual_type) -> bool: # type: ignore | ||
def matches_optional(self, optional_type: Any, actual_type: Any) -> bool: | ||
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 actual_type is None or self.matches_union(optional_type, actual_type) | ||
|
||
# Checks whether there is overloading which matches invoked argument types | ||
|
@@ -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()} | ||
|
||
|
@@ -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()} | ||
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. 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()} | ||
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. 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 | ||
|
@@ -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: | ||
|
@@ -153,4 +155,4 @@ def register(function: Callable) -> MultiMethod: | |
mm = registry[name] = MultiMethod(name) | ||
mm.register(types, function) | ||
return mm | ||
return register | ||
return register |
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.
if needed, comments should be in english, please :)