Skip to content

Commit

Permalink
[Py OV] Fix openvino.Model mocked object parameters (openvinotoolkit#…
Browse files Browse the repository at this point in the history
…28313)

### Details:
- Overload `dir()` method for class objects. Add meta class `ModelMeta`
and overload `dir()`
 - Add test for checking model attributes usind `dir()`

### Tickets:
 - [CVS-160178](https://jira.devtools.intel.com/browse/CVS-160178)

---------

Signed-off-by: Alicja Miloszewska <alicja.miloszewska@intel.com>
  • Loading branch information
almilosz authored Jan 9, 2025
1 parent 1694ea8 commit c839bf0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/bindings/python/src/openvino/_ov_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
)


class Model:
class ModelMeta(type):
def __dir__(cls) -> list:
return list(set(cls.__dict__.keys()) | set(dir(ModelBase)))


class Model(object, metaclass=ModelMeta):
def __init__(self, *args: Any, **kwargs: Any) -> None:
if args and not kwargs:
if isinstance(args[0], ModelBase):
Expand Down Expand Up @@ -65,6 +70,10 @@ def __exit__(self, exc_type: Type[BaseException], exc_value: BaseException, trac
def __repr__(self) -> str:
return self.__model.__repr__()

def __dir__(self) -> list:
wrapper_methods = ["__copy__", "__deepcopy__", "__dict__", "__enter__", "__exit__", "__getattr__", "__weakref__"]
return dir(self.__model) + wrapper_methods


class InferRequest(_InferRequestWrapper):
"""InferRequest class represents infer request which can be run in asynchronous or synchronous manners."""
Expand Down
8 changes: 8 additions & 0 deletions src/bindings/python/tests/test_runtime/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,3 +849,11 @@ def test_tempdir_save_load_error():
with tempfile.TemporaryDirectory() as model_save_dir:
save_model(mem_model, f"{model_save_dir}/model.xml")
_ = Core().read_model(f"{model_save_dir}/model.xml")


def test_model_dir():
model = generate_add_model()
num_of_attrs = 83

assert type(dir(model)) == list
assert len(dir(model)) >= num_of_attrs

0 comments on commit c839bf0

Please sign in to comment.