Skip to content

Commit

Permalink
fix: fix previously introduced issues
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Oct 27, 2023
1 parent a141d5a commit 0624efe
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion robotoff/prediction/ocr/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class City:
coordinates: Optional[tuple[float, float]]


@cache()
@cache
def load_cities_fr(source: Union[Path, BinaryIO, None] = None) -> set[City]:
"""Load French cities dataset.
Expand Down
4 changes: 2 additions & 2 deletions robotoff/prediction/ocr/packager_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def process_USDA_match_to_flashtext(match) -> Optional[str]:
return USDA_code


@cache()
@cache
def generate_USDA_code_keyword_processor() -> KeywordProcessor:
"""Builds the KeyWordProcessor for USDA codes."""

Expand Down Expand Up @@ -184,7 +184,7 @@ def find_packager_codes_regex(content: Union[OCRResult, str]) -> list[Prediction
return results


@cache()
@cache
def generate_fishing_code_keyword_processor() -> KeywordProcessor:
codes = text_file_iter(settings.OCR_FISHING_FLASHTEXT_DATA_PATH)
return generate_keyword_processor(("{}||{}".format(c.upper(), c) for c in codes))
Expand Down
7 changes: 5 additions & 2 deletions robotoff/prediction/ocr/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from robotoff import settings
from robotoff.types import Prediction, PredictionType
from robotoff.utils import text_file_iter
from robotoff.utils.text.flashtext import KeywordProcessor

from .utils import generate_keyword_processor

Expand All @@ -21,8 +22,10 @@
PREDICTOR_VERSION = "1"


@cache()
def generate_trace_keyword_processor(labels: Optional[list[str]] = None):
@cache
def generate_trace_keyword_processor(
labels: Optional[list[str]] = None,
) -> KeywordProcessor:
if labels is None:
labels = list(text_file_iter(settings.OCR_TRACE_ALLERGEN_DATA_PATH))

Expand Down
3 changes: 3 additions & 0 deletions robotoff/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ def get_image_from_url(
error_raise=error_raise,
session=session,
)

if content_bytes is None:
return None
else:
r = _get_image_from_url(image_url, error_raise, session)
if r is None:
Expand Down
9 changes: 5 additions & 4 deletions tests/unit/prediction/ocr/test_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def test_load_cities_fr(mocker):
],
)

res = load_cities_fr()
# Bypass the cache
res = load_cities_fr.__wrapped__()

m_gzip_open.assert_called_once_with(settings.OCR_CITIES_FR_PATH, "rb")
m_json_load.assert_called_once_with(m_gzip_open.return_value.__enter__.return_value)
Expand All @@ -46,7 +47,7 @@ def test_load_cities_fr(mocker):
with pytest.raises(
ValueError, match="'123', invalid FR postal code for city 'yolo'"
):
load_cities_fr()
load_cities_fr.__wrapped__()

m_gzip_open.assert_called_once_with(settings.OCR_CITIES_FR_PATH, "rb")
m_json_load.assert_called_once_with(m_gzip_open.return_value.__enter__.return_value)
Expand All @@ -60,14 +61,14 @@ def test_load_cities_fr(mocker):
with pytest.raises(
ValueError, match="'12A42', invalid FR postal code for city 'yolo'"
):
load_cities_fr()
load_cities_fr.__wrapped__()

m_gzip_open.assert_called_once_with(settings.OCR_CITIES_FR_PATH, "rb")
m_json_load.assert_called_once_with(m_gzip_open.return_value.__enter__.return_value)


def test_cities_fr_dataset():
cities_fr = load_cities_fr()
cities_fr = load_cities_fr.__wrapped__()

assert all(isinstance(item, City) for item in cities_fr)
assert len(set(cities_fr)) == len(cities_fr)
Expand Down

0 comments on commit 0624efe

Please sign in to comment.