From 3206642d1cf5674128add064c8afffaa4905d60f Mon Sep 17 00:00:00 2001 From: "sentry-autofix-experimental[bot]" <157164994+sentry-autofix-experimental[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 22:24:57 +0000 Subject: [PATCH 1/2] Fix TypeError in user age processing and improve error handling - Rename buggy_code to process_user_ages - Add type hints for better code clarity - Implement safe age calculation with proper error handling - Add informative logging for successful and failed age calculations --- src/seer/automation/tasks.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/seer/automation/tasks.py b/src/seer/automation/tasks.py index 9ceb03d0..94748a8a 100644 --- a/src/seer/automation/tasks.py +++ b/src/seer/automation/tasks.py @@ -1,5 +1,6 @@ import datetime import logging +from typing import List, Dict, Union import sentry_sdk import sqlalchemy.sql as sql @@ -7,6 +8,7 @@ from celery_app.app import celery_app from seer.db import DbIssueSummary, DbRunState, Session + logger = logging.getLogger(__name__) From 8cef0467c50d94aead191235b13580facbb00d04 Mon Sep 17 00:00:00 2001 From: "sentry-autofix-experimental[bot]" <157164994+sentry-autofix-experimental[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 22:24:59 +0000 Subject: [PATCH 2/2] Fix TypeError in user age processing and improve error handling - Rename buggy_code to process_user_ages - Add type hints for better code clarity - Implement safe age calculation with proper error handling - Add informative logging for successful and failed age calculations --- src/seer/automation/tasks.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/seer/automation/tasks.py b/src/seer/automation/tasks.py index 94748a8a..5f99a692 100644 --- a/src/seer/automation/tasks.py +++ b/src/seer/automation/tasks.py @@ -12,17 +12,33 @@ logger = logging.getLogger(__name__) + + @celery_app.task(time_limit=30) -def buggy_code(): - user_data = [ +def process_user_ages(): + user_data: List[Dict[str, Union[str, int, None]]] = [ {"name": "Alice", "age": 30}, {"name": "Bob", "age": "25"}, {"name": "Charlie", "age": None}, {"name": "David", "age": 40}, ] + def calculate_age_in_months(age: Union[int, str, None]) -> Union[int, None]: + if age is None: + return None + try: + return int(age) * 12 + except ValueError: + logger.warning(f"Invalid age value: {age}") + return None + for user in user_data: - print(user["age"] * 12) # type: ignore[index] + name = user["name"] + age_in_months = calculate_age_in_months(user["age"]) + if age_in_months is not None: + logger.info(f"User {name} is {age_in_months} months old") + else: + logger.warning(f"Unable to calculate age in months for user {name}") @celery_app.task(time_limit=30)