From 5fe9304441a5488af72fa8af08fcd2682ea3b694 Mon Sep 17 00:00:00 2001 From: "sentry-autofix-experimental[bot]" <157164994+sentry-autofix-experimental[bot]@users.noreply.github.com> Date: Fri, 11 Oct 2024 23:43:26 +0000 Subject: [PATCH] Fix TypeError in age calculation and improve error handling - Add safe_convert_age helper function - Rename buggy_code to calculate_yearly_ages - Implement proper age validation and error handling - Replace print statements with logger calls --- src/seer/automation/tasks.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/seer/automation/tasks.py b/src/seer/automation/tasks.py index 9ceb03d0..6af0a41c 100644 --- a/src/seer/automation/tasks.py +++ b/src/seer/automation/tasks.py @@ -7,20 +7,34 @@ from celery_app.app import celery_app from seer.db import DbIssueSummary, DbRunState, Session + logger = logging.getLogger(__name__) +def safe_convert_age(age) -> int | None: + if age is None: + return None + try: + return int(age) + except ValueError: + return None @celery_app.task(time_limit=30) -def buggy_code(): +def calculate_yearly_ages(): user_data = [ {"name": "Alice", "age": 30}, {"name": "Bob", "age": "25"}, {"name": "Charlie", "age": None}, {"name": "David", "age": 40}, ] - + for user in user_data: - print(user["age"] * 12) # type: ignore[index] + name = user["name"] + age = safe_convert_age(user["age"]) + if age is not None: + yearly_age = age * 12 + logger.info(f"User {name}'s yearly age: {yearly_age}") + else: + logger.warning(f"Invalid age for user {name}. Skipping calculation.") @celery_app.task(time_limit=30)