Skip to content

Commit

Permalink
[PYTHON API] Check new config (openvinotoolkit#11402)
Browse files Browse the repository at this point in the history
Improve code-style in Python API and reduce number of ignored hints.

Co-authored-by: Anastasia Kuporosova <[email protected]>
  • Loading branch information
bszmelcz and akuporos authored Jun 13, 2022
1 parent 9fe27be commit 93839f8
Show file tree
Hide file tree
Showing 125 changed files with 2,307 additions and 1,910 deletions.
3 changes: 3 additions & 0 deletions src/bindings/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool.black]
line-length = 160
include = '\.pyi?$'
24 changes: 17 additions & 7 deletions src/bindings/python/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,29 @@ deps = -rrequirements.txt

[flake8]
# ignore:
# A001 variable "var_name" is shadowing a python builtin
# A002 argument "..." is shadowing a python builtin
# CCE001 ClassName.method1 should be after ClassName.method2
# D100 - Missing docstring in public module
# D101 - Missing docstring in public class
# D102 - Missing docstring in public method
# D103 - Missing docstring in public function
# D104 - Missing docstring in public package
# D105 - Missing docstring in magic method
# D107 - Missing docstring in __init__
# D412 - No blank lines allowed between a section header and its content
# F401 - module imported but unused
# N803 - argument name '...' should be lowercase
# T001 - print found
# W503 - line break before binary operator (prefer line breaks before op, not after)
ignore=D100,D102,D103,D104,D105,D107,D412,F401,W503
# RST301 - Unexpected indentation
# TAE002 - too complex annotation
# PT007 - wrong values type @pytest.mark.parametrize, expected list of tuples
# PT012 - pytest.raises() block should contain a single simple statement
# VNE001 - single letter variable names like 'X' are not allowed
# VNE003 - variable names that shadow builtins are not allowed

ignore=A001,A002,CCE001,D100,D101,D102,D103,D104,D105,D107,D412,E402,F401,N803,RST301,TAE002,T001,W503,PT007,PT012
inline-quotes = double
filename = *.py
max-line-length = 160
Expand All @@ -51,12 +64,9 @@ show_source = True
docstring-convention = google
enable-extensions = G
per-file-ignores =
# todo: will be fixed as part of 81803
tests/*: A001,C101,C812,C815,C816,C819,CCE001,D100,D101,D102,D103,D104,D105,D107,D212,E800,ECE001,N400,N802,N806,P101,P103,PT001,PT005,PT006,PT007,PT011,PT012,PT019,PT023,RST201,RST301,S001,T001,VNE001,VNE002,VNE003,W503
tests_compatibility/test_ngraph/*: A001,C101,C812,C815,C816,C819,CCE001,D100,D101,D102,D103,D104,D105,D107,D212,E800,ECE001,N400,N802,N806,P101,P103,PT001,PT005,PT006,PT007,PT011,PT012,PT019,PT023,RST201,RST301,S001,T001,VNE001,VNE002,VNE003,W503
src/compatibility/ngraph/*: A001,A002,C101,C812,C819,CCE001,E800,N803,N806,P101,RST201,RST202,RST203,RST206,RST301,T001,TAE002,VNE001,VNE003
# todo: will be fixed as part of 81803
src/openvino/*: A001,A002,C101,C812,C819,CCE001,E402,E800,N803,N806,P101,RST201,RST202,RST203,RST205,RST206,RST299,RST301,T001,TAE002,VNE001,VNE002,VNE003
src/openvino/runtime/*/ops.py: VNE001,VNE003
tests_compatibility/test_ngraph/*: C101,C812,C815,C816,C819,CCE001,D212,E800,ECE001,N400,N802,N806,P101,P103,PT001,PT005,PT006,PT011,PT019,PT023,RST201,S001,VNE002
src/compatibility/ngraph/*: C101,C812,C819,CCE001,E800,N806,P101,RST201,RST202,RST203,RST206,VNE001,VNE003

[pydocstyle]
convention = google
Expand Down
3 changes: 3 additions & 0 deletions src/bindings/python/src/openvino/runtime/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

Expand All @@ -7,6 +8,7 @@
from openvino.utils import add_openvino_libs_to_path
from pkg_resources import get_distribution, DistributionNotFound


try:
__version__ = get_distribution("openvino-core").version
except DistributionNotFound:
Expand Down Expand Up @@ -65,6 +67,7 @@
from openvino.runtime.ie_api import tensor_from_file
from openvino.runtime.ie_api import compile_model


# Extend Node class to support binary operators
Node.__add__ = opset9.add
Node.__sub__ = opset9.subtract
Expand Down
1 change: 1 addition & 0 deletions src/bindings/python/src/openvino/runtime/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

Expand Down
74 changes: 26 additions & 48 deletions src/bindings/python/src/openvino/runtime/ie_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

Expand All @@ -22,19 +23,15 @@ def tensor_from_file(path: str) -> Tensor:
return Tensor(np.fromfile(path, dtype=np.uint8))


def set_scalar_tensor(
request: InferRequestBase, tensor: Tensor, key: Union[str, int, ConstOutput] = None
) -> None:
def set_scalar_tensor(request: InferRequestBase, tensor: Tensor, key: Union[str, int, ConstOutput] = None) -> None:
if key is None:
request.set_input_tensor(tensor)
elif isinstance(key, int):
request.set_input_tensor(key, tensor)
elif isinstance(key, (str, ConstOutput)):
request.set_tensor(key, tensor)
else:
raise TypeError(
"Unsupported key type: {} for Tensor under key: {}".format(type(key), key)
)
raise TypeError(f"Unsupported key type: {type(key)} for Tensor under key: {key}")


@singledispatch
Expand All @@ -43,9 +40,7 @@ def update_tensor(
request: InferRequestBase,
key: Union[str, int, ConstOutput] = None,
) -> None:
raise TypeError(
"Incompatible input data of type {} under {} key!".format(type(inputs), key)
)
raise TypeError(f"Incompatible input data of type {type(inputs)} under {key} key!")


@update_tensor.register(np.ndarray)
Expand All @@ -65,11 +60,7 @@ def _(
elif isinstance(key, (str, ConstOutput)):
tensor = request.get_tensor(key)
else:
raise TypeError(
"Unsupported key type: {} for Tensor under key: {}".format(
type(key), key
)
)
raise TypeError(f"Unsupported key type: {type(key)} for Tensor under key: {key}")
# Update shape if there is a mismatch
if tensor.shape != inputs.shape:
tensor.shape = inputs.shape
Expand All @@ -86,7 +77,7 @@ def _(
key: Union[str, int, ConstOutput] = None,
) -> None:
set_scalar_tensor(
request, Tensor(np.ndarray([], type(inputs), np.array(inputs))), key
request, Tensor(np.ndarray([], type(inputs), np.array(inputs))), key,
)


Expand All @@ -100,29 +91,25 @@ def normalize_inputs(request: InferRequestBase, inputs: dict) -> dict:
# new_inputs will be used to transfer data to inference calls,
# ensuring that original inputs are not overwritten with Tensors.
new_inputs: Dict[Union[str, int, ConstOutput], Tensor] = {}
for k, val in inputs.items():
if not isinstance(k, (str, int, ConstOutput)):
raise TypeError("Incompatible key type for input: {}".format(k))
for key, value in inputs.items():
if not isinstance(key, (str, int, ConstOutput)):
raise TypeError(f"Incompatible key type for input: {key}")
# Copy numpy arrays to already allocated Tensors.
if isinstance(val, (np.ndarray, np.number, int, float)):
update_tensor(val, request, k)
if isinstance(value, (np.ndarray, np.number, int, float)):
update_tensor(value, request, key)
# If value is of Tensor type, put it into temporary dictionary.
elif isinstance(val, Tensor):
new_inputs[k] = val
elif isinstance(value, Tensor):
new_inputs[key] = value
# Throw error otherwise.
else:
raise TypeError(
"Incompatible input data of type {} under {} key!".format(type(val), k)
)
raise TypeError(f"Incompatible input data of type {type(value)} under {key} key!")
return new_inputs


class InferRequest(InferRequestBase):
"""InferRequest class represents infer request which can be run in asynchronous or synchronous manners."""

def infer(
self, inputs: Union[dict, list, tuple, Tensor, np.ndarray] = None
) -> dict:
def infer(self, inputs: Union[dict, list, tuple, Tensor, np.ndarray] = None) -> dict:
"""Infers specified input(s) in synchronous mode.
Blocks all methods of InferRequest while request is running.
Expand Down Expand Up @@ -158,10 +145,7 @@ def infer(
# It is an extension of above branch with dict inputs.
elif isinstance(inputs, (list, tuple)):
return super().infer(
normalize_inputs(
self, {index: input for index, input in enumerate(inputs)}
)
)
normalize_inputs(self, {index: input for index, input in enumerate(inputs)}))
# If inputs are Tensor, call infer method directly.
elif isinstance(inputs, Tensor):
return super().infer(inputs)
Expand Down Expand Up @@ -211,11 +195,7 @@ def start_async(
super().start_async(normalize_inputs(self, inputs), userdata)
elif isinstance(inputs, (list, tuple)):
super().start_async(
normalize_inputs(
self, {index: input for index, input in enumerate(inputs)}
),
userdata,
)
normalize_inputs(self, {index: input for index, input in enumerate(inputs)}), userdata)
elif isinstance(inputs, Tensor):
super().start_async(inputs, userdata)
elif isinstance(inputs, (np.ndarray, np.number, int, float)):
Expand All @@ -242,9 +222,7 @@ def create_infer_request(self) -> InferRequest:
"""
return InferRequest(super().create_infer_request())

def infer_new_request(
self, inputs: Union[dict, list, tuple, Tensor, np.ndarray] = None
) -> dict:
def infer_new_request(self, inputs: Union[dict, list, tuple, Tensor, np.ndarray] = None) -> dict:
"""Infers specified input(s) in synchronous mode.
Blocks all methods of CompiledModel while request is running.
Expand Down Expand Up @@ -292,7 +270,6 @@ class AsyncInferQueue(AsyncInferQueueBase):
InferRequests and provides synchronization functions to control flow of
a simple pipeline.
"""

def __getitem__(self, i: int) -> InferRequest:
"""Gets InferRequest from the pool with given i id.
Expand Down Expand Up @@ -334,7 +311,7 @@ def start_async(
super().start_async({}, userdata)
elif isinstance(inputs, dict):
super().start_async(
normalize_inputs(self[self.get_idle_request_id()], inputs), userdata
normalize_inputs(self[self.get_idle_request_id()], inputs), userdata,
)
elif isinstance(inputs, (list, tuple)):
super().start_async
Expand Down Expand Up @@ -364,7 +341,7 @@ class Core(CoreBase):
"""

def compile_model(
self, model: Union[Model, str], device_name: str = None, config: dict = None
self, model: Union[Model, str], device_name: str = None, config: dict = None,
) -> CompiledModel:
"""Creates a compiled model.
Expand All @@ -389,15 +366,15 @@ def compile_model(
"""
if device_name is None:
return CompiledModel(
super().compile_model(model, {} if config is None else config)
super().compile_model(model, {} if config is None else config),
)

return CompiledModel(
super().compile_model(model, device_name, {} if config is None else config)
super().compile_model(model, device_name, {} if config is None else config),
)

def import_model(
self, model_stream: bytes, device_name: str, config: dict = None
self, model_stream: bytes, device_name: str, config: dict = None,
) -> CompiledModel:
"""Imports a compiled model from a previously exported one.
Expand All @@ -414,6 +391,7 @@ def import_model(
:rtype: openvino.runtime.CompiledModel
:Example:
.. code-block:: python
user_stream = compiled.export_model()
Expand All @@ -439,8 +417,8 @@ def import_model(
"""
return CompiledModel(
super().import_model(
model_stream, device_name, {} if config is None else config
)
model_stream, device_name, {} if config is None else config,
),
)


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

Expand Down
Loading

0 comments on commit 93839f8

Please sign in to comment.