Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[PyOV] Check required parameters when picking an overload from keyword arguments (#39181)

Somay kousis77929eaf3629
diff --git a/src/bindings/python/src/openvino/utils/decorators.py b/src/bindings/python/src/openvino/utils/decorators.py
index 02d21d8c643..31481f511a6 100644
--- a/src/bindings/python/src/openvino/utils/decorators.py
+++ b/src/bindings/python/src/openvino/utils/decorators.py
@@ -120,9 +120,13 @@ class MultiMethod(object):
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()}
- # if kwargs of called function are subset of overloaded function, we use this overload
- if kwarg_types.keys() <= func_signature.keys():
+ params = signature(func).parameters
+ # parameters without a default, *args and **kwargs excluded
+ required = {n for n, p in params.items()
+ if p.default is p.empty and p.kind not in (p.VAR_POSITIONAL, p.VAR_KEYWORD)}
+ # if kwargs of called function are subset of overloaded function
+ # and cover its required parameters, we use this overload
+ if kwarg_types.keys() <= params.keys() and required <= kwarg_types.keys():
key_matched = key
break
elif len(arg_types) != 0 and len(kwarg_types) != 0:
diff --git a/src/bindings/python/tests/test_graph/test_create_op.py b/src/bindings/python/tests/test_graph/test_create_op.py
index dd79a92e4e4..5625289808d 100644
--- a/src/bindings/python/tests/test_graph/test_create_op.py
+++ b/src/bindings/python/tests/test_graph/test_create_op.py
@@ -1063,6 +1063,35 @@ def test_read_value_ctors():
assert "The necessary overload for read_value was not found" in str(e.value)
[email protected]("kwargs", [
+ {"variable_id": "v1"},
+ {"variable_id": "v1", "variable_type": np.float32, "variable_shape": [1, 64]},
+ {"variable_id": "v1", "variable_type": np.float32, "variable_shape": [1, 64], "name": "read_value"},
+])
+def test_read_value_variable_id_overload_with_kwargs_only(kwargs):
+ # Keywords alone must select the overload without init_value, as the same call does positionally
+ read_value = ov.read_value(**kwargs)
+
+ assert read_value.get_type_name() == "ReadValue"
+ assert read_value.get_variable_id() == "v1"
+ assert read_value.get_input_size() == 0
+ if "variable_shape" in kwargs:
+ assert read_value.get_output_partial_shape(0) == PartialShape([1, 64])
+ assert read_value.get_output_element_type(0) == Type.f32
+ if "name" in kwargs:
+ assert read_value.friendly_name == "read_value"
+
+
[email protected]("kwargs", [
+ {"name": "read_value"},
+ {"variable_type": np.float32, "variable_shape": [1, 64]},
+])
+def test_read_value_kwargs_only_without_required_args(kwargs):
+ # No overload gets all of its required parameters from these keywords
+ with pytest.raises(TypeError, match="The necessary overload for read_value was not found"):
+ ov.read_value(**kwargs)
+
+
def test_read_value_dyn_variable_pshape():
init_value = ov.parameter([2, 2], name="init_value", dtype=np.int32)