Skip to content

Commit 326f274

Browse files
committed
chore: replace black/isort/flake8 by ruff
1 parent 93be8e5 commit 326f274

File tree

12 files changed

+1299
-236
lines changed

12 files changed

+1299
-236
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ jobs:
3636

3737
- name: Launch quality checks
3838
run: |
39-
poetry run flake8 .
40-
poetry run black --check .
39+
poetry run ruff check .
4140
poetry run mypy .
4241
poetry run isort --check .
4342
- name: Test with pytest

openfoodfacts/api.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ def parse_ingredients(
397397

398398
if not api_version.startswith("v3"):
399399
logger.warning(
400-
"ingredient parsing is only available in v3 of the API (here: %s), using v3",
400+
"ingredient parsing is only available in v3 of the API (here: %s), "
401+
"using v3",
401402
self.api_config.version,
402403
)
403404
api_version = "v3"
@@ -431,11 +432,12 @@ def parse_ingredients(
431432
) as e:
432433
raise RuntimeError(
433434
f"Unable to parse ingredients: error during HTTP request: {e}"
434-
)
435+
) from e
435436

436437
if not r.ok:
437438
raise RuntimeError(
438-
f"Unable to parse ingredients (non-200 status code): {r.status_code}, {r.text}"
439+
"Unable to parse ingredients (non-200 status code): "
440+
f"{r.status_code}, {r.text}"
439441
)
440442

441443
response_data = r.json()
@@ -486,8 +488,9 @@ def upload_image(
486488
api_version = self.api_config.version
487489
if not self.api_config.version.startswith("v3"):
488490
warnings.warn(
489-
"image upload is only available in v3 of the API (here: %s), forcing use of v3"
490-
% self.api_config.version,
491+
"image upload is only available in v3 of the API (here: %s), "
492+
"forcing use of v3" % self.api_config.version,
493+
stacklevel=2,
491494
)
492495
api_version = "v3"
493496

@@ -534,7 +537,8 @@ def upload_image(
534537
)
535538
if not isinstance(value, dict):
536539
raise ValueError(
537-
f"selected[{key}][{lang_code}] must be a dict, got {type(value)}"
540+
f"selected[{key}][{lang_code}] must be a dict, got "
541+
f"{type(value)}"
538542
)
539543

540544
# copy the config but force v3 of the API
@@ -665,11 +669,12 @@ def parse_ingredients(text: str, lang: str, api_config: APIConfig) -> list[JSONT
665669
) as e:
666670
raise RuntimeError(
667671
f"Unable to parse ingredients: error during HTTP request: {e}"
668-
)
672+
) from e
669673

670674
if not r.ok:
671675
raise RuntimeError(
672-
f"Unable to parse ingredients (non-200 status code): {r.status_code}, {r.text}"
676+
"Unable to parse ingredients (non-200 status code): "
677+
f"{r.status_code}, {r.text}"
673678
)
674679

675680
response_data = r.json()

openfoodfacts/images.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ def download_image(
216216
:return: the downloaded image, or an `ImageDownloadItem` object if
217217
`return_struct` is True.
218218
219-
>>> download_image("https://images.openfoodfacts.org/images/products/324/227/210/2359/4.jpg") # noqa
219+
>>> download_image(
220+
"https://images.openfoodfacts.org/images/products/324/227/210/2359/4.jpg"
221+
)
220222
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1244x1500>
221223
222224
>>> download_image(("3242272102359", "4"))

openfoodfacts/ml/image_classification.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ def postprocess(
157157

158158
if len(response.raw_output_contents) != 1:
159159
raise Exception(
160-
f"expected 1 raw output content, got {len(response.raw_output_contents)}"
160+
"expected 1 raw output content, got "
161+
f"{len(response.raw_output_contents)}"
161162
)
162163

163164
output_index = {output.name: i for i, output in enumerate(response.outputs)}

openfoodfacts/ml/object_detection.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ def to_list(self) -> list[JSONType]:
140140
"""Convert the detection results to a JSON serializable format."""
141141
results = []
142142
for bounding_box, score, label in zip(
143-
self.detection_boxes, self.detection_scores, self.detection_classes
143+
self.detection_boxes,
144+
self.detection_scores,
145+
self.detection_classes,
146+
strict=True,
144147
):
145148
label_int = int(label)
146149
label_str = self.label_names[label_int]
@@ -329,7 +332,8 @@ def postprocess(
329332

330333
if len(response.raw_output_contents) != 1:
331334
raise ValueError(
332-
f"expected 1 raw output content, got {len(response.raw_output_contents)}"
335+
"expected 1 raw output content, got "
336+
f"{len(response.raw_output_contents)}"
333337
)
334338

335339
if nms_threshold is None:

openfoodfacts/ml/triton.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def serialize_byte_tensor(input_tensor):
7272
# don't convert it to str as Python will encode the
7373
# bytes which may distort the meaning
7474
if input_tensor.dtype == np.object_:
75-
if type(obj.item()) == bytes:
75+
if type(obj.item()) is bytes:
7676
s = obj.item()
7777
else:
7878
s = str(obj.item()).encode("utf-8")

openfoodfacts/utils/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,15 +400,15 @@ def get_image_from_url(
400400
image_bytes=content_bytes,
401401
)
402402
return image
403-
except PIL.UnidentifiedImageError:
403+
except PIL.UnidentifiedImageError as e:
404404
error_message = f"Cannot identify image {image_url}"
405405
if error_raise:
406-
raise AssetLoadingException(error_message)
406+
raise AssetLoadingException(error_message) from e
407407
logger.info(error_message)
408-
except PIL.Image.DecompressionBombError:
408+
except PIL.Image.DecompressionBombError as e:
409409
error_message = f"Decompression bomb error for image {image_url}"
410410
if error_raise:
411-
raise AssetLoadingException(error_message)
411+
raise AssetLoadingException(error_message) from e
412412
logger.info(error_message)
413413

414414
if return_struct:

0 commit comments

Comments
 (0)