From 4fa88335d180e588ce3ef1a825e9294c13e6b357 Mon Sep 17 00:00:00 2001 From: jableader Date: Tue, 29 Jul 2025 17:15:38 +1000 Subject: [PATCH] Fixed async generator crash --- ingredients/__init__.py | 9 +++++++-- products/__init__.py | 5 +---- shopping/db.py | 5 +---- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/ingredients/__init__.py b/ingredients/__init__.py index f23244a..f19bd10 100644 --- a/ingredients/__init__.py +++ b/ingredients/__init__.py @@ -65,8 +65,13 @@ def parse_ingredient_from_nlp(ingredient_string: str) -> Ingredient: ) async def _find_existing_product(conn, ingredient: str) -> Product: - async for item in find_product_by_tag(conn, ingredient): - return item + gen = find_product_by_tag(conn, ingredient) + try: + async for item in gen: + return item + finally: + await gen.aclose() + return None async def match_existing_products(conn, ingredients: List[Ingredient]) -> List[Ingredient]: diff --git a/products/__init__.py b/products/__init__.py index f3bf01d..5a29afd 100644 --- a/products/__init__.py +++ b/products/__init__.py @@ -16,10 +16,7 @@ def _get_shop_key(link: str) -> Union[str, str]: # (shop_code, product_id) return None, None async def add_missing_tags(conn, product: Product, tags: List[str]): - existing_tags = set() - async for tag in get_tags(conn, product): - existing_tags.add(tag) - + existing_tags = {tag async for tag in get_tags(conn, product)} remaining_tags = set(tags) - existing_tags if not remaining_tags: return False diff --git a/shopping/db.py b/shopping/db.py index aa6ed52..f4963d8 100644 --- a/shopping/db.py +++ b/shopping/db.py @@ -80,15 +80,12 @@ def validate_request(request: ShoppingListItem) -> None: raise ValueError('Request must have either an ingredient or a meal') async def purchase(conn, shopping_list: ShoppingList) -> None: - if shopping_list.purchased_by_id is None: + if shopping_list.purchased_by_id is None or shopping_list.purchased_by_id < 0: raise ValueError('Shopping list must have a person id') if shopping_list.items is None or len(shopping_list.items) == 0: raise ValueError('Shopping list must have items') - if shopping_list.purchased_by_id < 0: - raise ValueError('Shopping list must have a valid person id') - shopping_list.created_date = datetime.now().astimezone() async with conn.execute('''