Skip to content

Commit 2d87b2d

Browse files
Enable more ruff rules (#224)
* Enable more rules Signed-off-by: Ashwin Vaidya <[email protected]> * Enable some more flake8 rules Signed-off-by: Ashwin Vaidya <[email protected]> --------- Signed-off-by: Ashwin Vaidya <[email protected]>
1 parent 5830b62 commit 2d87b2d

23 files changed

+130
-95
lines changed

model_api/python/model_api/adapters/openvino_adapter.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737

3838
def create_core():
3939
if openvino_absent:
40-
raise ImportError("The OpenVINO package is not installed")
40+
msg = "The OpenVINO package is not installed"
41+
raise ImportError(msg)
4142

4243
log.info("OpenVINO Runtime")
4344
log.info(f"\tbuild: {get_version()}")
@@ -73,7 +74,8 @@ def parse_value_per_device(devices: set[str], values_string: str) -> dict[str, i
7374
for device in devices:
7475
result[device] = int(device_value_list[0])
7576
elif device_value_list[0] != "":
76-
raise RuntimeError(f"Unknown string format: {values_string}")
77+
msg = f"Unknown string format: {values_string}"
78+
raise RuntimeError(msg)
7779
return result
7880

7981

@@ -174,7 +176,8 @@ def __init__(
174176
log.info(f"Reading model {self.model_path}")
175177
self.model = core.read_model(self.model_path)
176178
return
177-
raise RuntimeError("Model must be bytes, a file or existing OMZ model name")
179+
msg = "Model must be bytes, a file or existing OMZ model name"
180+
raise RuntimeError(msg)
178181

179182
def load_model(self):
180183
self.compiled_model = self.core.compile_model(
@@ -377,9 +380,8 @@ def embed_preprocessing(
377380
)
378381

379382
else:
380-
raise ValueError(
381-
f"Upsupported resize type in model preprocessing: {resize_mode}",
382-
)
383+
msg = f"Upsupported resize type in model preprocessing: {resize_mode}"
384+
raise ValueError(msg)
383385

384386
# Handle layout
385387
ppp.input(input_idx).model().set_layout(ov.Layout(layout))

model_api/python/model_api/adapters/ovms_adapter.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def reshape_model(self, new_shape):
116116
raise NotImplementedError
117117

118118
def get_rt_info(self, path):
119-
raise NotImplementedError("OVMSAdapter does not support RT info getting")
119+
msg = "OVMSAdapter does not support RT info getting"
120+
raise NotImplementedError(msg)
120121

121122

122123
_tf2ov_precision = {
@@ -147,21 +148,24 @@ def get_rt_info(self, path):
147148

148149
def _parse_model_arg(target_model: str):
149150
if not isinstance(target_model, str):
150-
raise TypeError("target_model must be str")
151+
msg = "target_model must be str"
152+
raise TypeError(msg)
151153
# Expected format: <address>:<port>/models/<model_name>[:<model_version>]
152154
if not re.fullmatch(
153155
r"(\w+\.*\-*)*\w+:\d+\/models\/[a-zA-Z0-9._-]+(\:\d+)*",
154156
target_model,
155157
):
156-
raise ValueError("invalid --model option format")
158+
msg = "invalid --model option format"
159+
raise ValueError(msg)
157160
service_url, _, model = target_model.split("/")
158161
model_spec = model.split(":")
159162
if len(model_spec) == 1:
160163
# model version not specified - use latest
161164
return service_url, model_spec[0], 0
162165
if len(model_spec) == 2:
163166
return service_url, model_spec[0], int(model_spec[1])
164-
raise ValueError("invalid target_model format")
167+
msg = "invalid target_model format"
168+
raise ValueError(msg)
165169

166170

167171
def _verify_model_available(client, model_name, model_version):
@@ -171,22 +175,21 @@ def _verify_model_available(client, model_name, model_version):
171175
try:
172176
model_status = client.get_model_status(model_name, model_version)
173177
except ovmsclient.ModelNotFoundError as e:
174-
raise RuntimeError(
175-
f"Requested model: {model_name}, version: {version} has not been found",
176-
) from e
178+
msg = f"Requested model: {model_name}, version: {version} has not been found"
179+
raise RuntimeError(msg) from e
177180
target_version = max(model_status.keys())
178181
version_status = model_status[target_version]
179182
if version_status["state"] != "AVAILABLE" or version_status["error_code"] != 0:
180-
raise RuntimeError(
181-
f"Requested model: {model_name}, version: {version} is not in available state",
182-
)
183+
msg = f"Requested model: {model_name}, version: {version} is not in available state"
184+
raise RuntimeError(msg)
183185

184186

185187
def _prepare_inputs(dict_data, inputs_meta):
186188
inputs = {}
187189
for input_name, input_data in dict_data.items():
188190
if input_name not in inputs_meta:
189-
raise ValueError("Input data does not match model inputs")
191+
msg = "Input data does not match model inputs"
192+
raise ValueError(msg)
190193
input_info = inputs_meta[input_name]
191194
model_precision = _tf2np_precision[input_info["dtype"]]
192195
if isinstance(input_data, np.ndarray) and input_data.dtype != model_precision:

model_api/python/model_api/adapters/utils.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ def from_shape(shape):
3333
if len(shape) == 6:
3434
return "NSTHWC" if shape[5] in range(1, 5) else "NSCTHW"
3535

36-
raise RuntimeError(
37-
f"Get layout from shape method doesn't support {len(shape)}D shape",
38-
)
36+
msg = f"Get layout from shape method doesn't support {len(shape)}D shape"
37+
raise RuntimeError(msg)
3938

4039
@staticmethod
4140
def from_openvino(input):
@@ -75,9 +74,11 @@ def parse_layouts(layout_string: str) -> dict | None:
7574

7675
def resize_image_letterbox_graph(input: Output, size, interpolation, pad_value):
7776
if not isinstance(pad_value, int):
78-
raise RuntimeError("pad_value must be int")
77+
msg = "pad_value must be int"
78+
raise RuntimeError(msg)
7979
if not 0 <= pad_value <= 255:
80-
raise RuntimeError("pad_value must be in range [0, 255]")
80+
msg = "pad_value must be in range [0, 255]"
81+
raise RuntimeError(msg)
8182
w, h = size
8283
h_axis = 1
8384
w_axis = 2
@@ -291,9 +292,11 @@ def resize_image_graph(
291292
pad_value,
292293
):
293294
if not isinstance(pad_value, int):
294-
raise RuntimeError("pad_value must be int")
295+
msg = "pad_value must be int"
296+
raise RuntimeError(msg)
295297
if not 0 <= pad_value <= 255:
296-
raise RuntimeError("pad_value must be in range [0, 255]")
298+
msg = "pad_value must be in range [0, 255]"
299+
raise RuntimeError(msg)
297300
h_axis = 1
298301
w_axis = 2
299302
w, h = size
@@ -426,9 +429,8 @@ def get_rt_info_from_dict(rt_info_dict, path):
426429
value = value[item]
427430
return OVAny(value)
428431
except KeyError:
429-
raise RuntimeError(
430-
"Cannot get runtime attribute. Path to runtime attribute is incorrect.",
431-
)
432+
msg = "Cannot get runtime attribute. Path to runtime attribute is incorrect."
433+
raise RuntimeError(msg)
432434

433435

434436
def resize_image_ocv(

model_api/python/model_api/models/action_classification.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ActionClassificationModel(Model):
4646
def __init__(
4747
self,
4848
inference_adapter: InferenceAdapter,
49-
configuration: dict[str, Any] = dict(),
49+
configuration: dict[str, Any] = {},
5050
preload: bool = False,
5151
) -> None:
5252
"""Action classaification model constructor

model_api/python/model_api/models/anomaly.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ class AnomalyDetection(ImageModel):
2525
__model__ = "AnomalyDetection"
2626

2727
def __init__(
28-
self, inference_adapter: InferenceAdapter, configuration: dict = dict(), preload: bool = False
28+
self,
29+
inference_adapter: InferenceAdapter,
30+
configuration: dict = {},
31+
preload: bool = False,
2932
) -> None:
3033
super().__init__(inference_adapter, configuration, preload)
3134
self._check_io_number(1, 1)

model_api/python/model_api/models/classification.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class ClassificationModel(ImageModel):
2525
__model__ = "Classification"
2626

27-
def __init__(self, inference_adapter: InferenceAdapter, configuration: dict = dict(), preload: bool = False):
27+
def __init__(self, inference_adapter: InferenceAdapter, configuration: dict = {}, preload: bool = False):
2828
super().__init__(inference_adapter, configuration, preload=False)
2929
self.topk: int
3030
self.labels: list[str]
@@ -605,9 +605,8 @@ def topological_sort(self):
605605
nodes_deque.append(node)
606606

607607
if len(ordered) != len(self._v):
608-
raise RuntimeError(
609-
"Topological sort failed: input graph has been" "changed during the sorting or contains a cycle",
610-
)
608+
msg = "Topological sort failed: input graph has been changed during the sorting or contains a cycle"
609+
raise RuntimeError(msg)
611610

612611
return ordered
613612

model_api/python/model_api/models/detection_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DetectionModel(ImageModel):
2020

2121
__model__ = "DetectionModel"
2222

23-
def __init__(self, inference_adapter, configuration=dict(), preload=False):
23+
def __init__(self, inference_adapter, configuration: dict = {}, preload=False):
2424
"""Detection Model constructor
2525
2626
It extends the `ImageModel` construtor.
@@ -36,7 +36,7 @@ def __init__(self, inference_adapter, configuration=dict(), preload=False):
3636
WrapperError: if the model has more than 1 image inputs
3737
"""
3838
super().__init__(inference_adapter, configuration, preload)
39-
39+
self.path_to_labels: str
4040
if not self.image_blob_name:
4141
self.raise_error(
4242
f"The Wrapper supports only one image input, but {len(self.image_blob_names)} found",

model_api/python/model_api/models/image_model.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ImageModel(Model):
3232

3333
__model__ = "ImageModel"
3434

35-
def __init__(self, inference_adapter, configuration=dict(), preload=False):
35+
def __init__(self, inference_adapter, configuration: dict = {}, preload=False):
3636
"""Image model constructor
3737
3838
It extends the `Model` constructor.
@@ -50,6 +50,14 @@ def __init__(self, inference_adapter, configuration=dict(), preload=False):
5050
super().__init__(inference_adapter, configuration, preload)
5151
self.image_blob_names, self.image_info_blob_names = self._get_inputs()
5252
self.image_blob_name = self.image_blob_names[0]
53+
self.orig_height: int
54+
self.orig_width: int
55+
self.pad_value: int
56+
self.resize_type: str
57+
self.mean_values: list
58+
self.scale_values: list
59+
self.reverse_input_channels: bool
60+
self.embedded_processing: bool
5361

5462
self.nchw_layout = self.inputs[self.image_blob_name].layout == "NCHW"
5563
if self.nchw_layout:

model_api/python/model_api/models/instance_segmentation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
class MaskRCNNModel(ImageModel):
1616
__model__ = "MaskRCNN"
1717

18-
def __init__(self, inference_adapter, configuration=dict(), preload=False):
18+
def __init__(self, inference_adapter, configuration: dict = {}, preload=False):
1919
super().__init__(inference_adapter, configuration, preload)
2020
self._check_io_number((1, 2), (3, 4, 5, 6, 8))
21+
self.path_to_labels: str
2122
if self.path_to_labels:
2223
self.labels = load_labels(self.path_to_labels)
2324
self.is_segmentoly = len(self.inputs) == 2

model_api/python/model_api/models/keypoint_detection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ class KeypointDetectionModel(ImageModel):
1919

2020
__model__ = "keypoint_detection"
2121

22-
def __init__(self, inference_adapter, configuration=dict(), preload=False):
22+
def __init__(self, inference_adapter, configuration: dict = {}, preload=False):
2323
"""Initializes the keypoint detection model.
2424
2525
Args:
2626
inference_adapter (InferenceAdapter): inference adapter containing the underlying model.
2727
configuration (dict, optional): configuration overrides the model parameters (see parameters() method).
28-
Defaults to dict().
28+
Defaults to {}.
2929
preload (bool, optional): forces inference adapter to load the model. Defaults to False.
3030
"""
3131
super().__init__(inference_adapter, configuration, preload)

0 commit comments

Comments
 (0)