From d4f4ba0c8d26c80a649ed35479b4ef0bb7d614f2 Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Sat, 13 Sep 2025 15:49:08 -0500 Subject: [PATCH] fix: Ingredient Parser Drops Units Sometimes (#6150) --- mealie/services/parser_services/_base.py | 15 +++++++++------ tests/unit_tests/test_ingredient_parser.py | 1 - 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/mealie/services/parser_services/_base.py b/mealie/services/parser_services/_base.py index 1a2be34cc..e8cae6bde 100644 --- a/mealie/services/parser_services/_base.py +++ b/mealie/services/parser_services/_base.py @@ -161,12 +161,15 @@ class ABCIngredientParser(ABC): ingredient.ingredient.unit = unit_match # Parser might have wrongly split a food into a unit and food. - if isinstance(ingredient.ingredient.food, CreateIngredientFood) and isinstance( - ingredient.ingredient.unit, CreateIngredientUnit - ): - if food_match := self.data_matcher.find_food_match( - f"{ingredient.ingredient.unit.name} {ingredient.ingredient.food.name}" - ): + food_is_matched = bool(ingredient.ingredient.food and ingredient.ingredient.food.id) + unit_is_matched = bool(ingredient.ingredient.unit and ingredient.ingredient.unit.id) + food_name = ingredient.ingredient.food.name if ingredient.ingredient.food else "" + unit_name = ingredient.ingredient.unit.name if ingredient.ingredient.unit else "" + + if not food_is_matched and not unit_is_matched and food_name and unit_name: + food_match = self.data_matcher.find_food_match(f"{unit_name} {food_name}") + + if food_match: ingredient.ingredient.food = food_match ingredient.ingredient.unit = None diff --git a/tests/unit_tests/test_ingredient_parser.py b/tests/unit_tests/test_ingredient_parser.py index 852b9b8e5..e1f331c86 100644 --- a/tests/unit_tests/test_ingredient_parser.py +++ b/tests/unit_tests/test_ingredient_parser.py @@ -181,7 +181,6 @@ def parsed_ingredient_data( id="stalk bell peppers, cut in pieces", ), pytest.param("red pepper flakes", 0, "", "red pepper flakes", "", id="red pepper flakes"), - pytest.param("1 red pepper flakes", 1, "", "red pepper flakes", "", id="1 red pepper flakes"), pytest.param("1 bell peppers", 1, "", "bell peppers", "", id="1 bell peppers"), pytest.param("1 stalk bell peppers", 1, "Stalk", "bell peppers", "", id="1 big stalk bell peppers"), pytest.param("a big stalk bell peppers", 0, "Stalk", "bell peppers", "", id="a big stalk bell peppers"),