Fixed async generator crash

This commit is contained in:
jableader 2025-07-29 17:15:38 +10:00
parent 1706809458
commit 4fa88335d1
3 changed files with 9 additions and 10 deletions

View file

@ -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):
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]:

View file

@ -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

View file

@ -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('''