From b42910d262128285800c7cc8aafba0fe7beda3a0 Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Tue, 21 Oct 2025 07:35:25 +0530 Subject: [PATCH 1/4] Add age_controller.py with validate_age function (issue #12809) This module validates and processes age input, ensuring it is a positive integer within the range of 0 to 150. It includes error handling for invalid inputs and provides examples in the docstring. --- age_controller.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 age_controller.py diff --git a/age_controller.py b/age_controller.py new file mode 100644 index 000000000000..01bc60fc1b57 --- /dev/null +++ b/age_controller.py @@ -0,0 +1,69 @@ +"""Age Controller Module + +This module provides functionality to validate and process age input. +Related to issue #12809. +""" + + +def validate_age(age): + """Validate and process age input. + + This function validates that the provided age is a valid positive integer + within a reasonable range (0-150 years). + + Args: + age: The age input to validate (can be int, str, or float) + + Returns: + int: The validated age as an integer + + Raises: + ValueError: If age is invalid, negative, or out of range + TypeError: If age cannot be converted to a number + + Examples: + >>> validate_age(25) + 25 + >>> validate_age('30') + 30 + >>> validate_age(45.0) + 45 + >>> validate_age(-5) + Traceback (most recent call last): + ... + ValueError: Age must be a positive number + >>> validate_age(200) + Traceback (most recent call last): + ... + ValueError: Age must be between 0 and 150 + >>> validate_age('invalid') + Traceback (most recent call last): + ... + ValueError: Age must be a valid number + """ + try: + # Convert to float first to handle string numbers + age_float = float(age) + + # Check if it's a whole number + if age_float != int(age_float): + age_int = int(age_float) + else: + age_int = int(age_float) + + except (ValueError, TypeError): + raise ValueError("Age must be a valid number") + + # Validate range + if age_int < 0: + raise ValueError("Age must be a positive number") + + if age_int > 150: + raise ValueError("Age must be between 0 and 150") + + return age_int + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 98046a3dd0a059e1f009e1b5565d96f93fec90e0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 21 Oct 2025 02:07:14 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- age_controller.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/age_controller.py b/age_controller.py index 01bc60fc1b57..ed6be634da00 100644 --- a/age_controller.py +++ b/age_controller.py @@ -7,20 +7,20 @@ def validate_age(age): """Validate and process age input. - + This function validates that the provided age is a valid positive integer within a reasonable range (0-150 years). - + Args: age: The age input to validate (can be int, str, or float) - + Returns: int: The validated age as an integer - + Raises: ValueError: If age is invalid, negative, or out of range TypeError: If age cannot be converted to a number - + Examples: >>> validate_age(25) 25 @@ -44,26 +44,27 @@ def validate_age(age): try: # Convert to float first to handle string numbers age_float = float(age) - + # Check if it's a whole number if age_float != int(age_float): age_int = int(age_float) else: age_int = int(age_float) - + except (ValueError, TypeError): raise ValueError("Age must be a valid number") - + # Validate range if age_int < 0: raise ValueError("Age must be a positive number") - + if age_int > 150: raise ValueError("Age must be between 0 and 150") - + return age_int if __name__ == "__main__": import doctest + doctest.testmod() From 0bf1d69dd76a7aa3d2391c05e247a1145a85b9fb Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Tue, 21 Oct 2025 07:42:52 +0530 Subject: [PATCH 3/4] Fix Ruff linter errors: use ternary operator and move to other/ directory --- age_controller.py => other/age_controller.py | 45 +++++++++----------- 1 file changed, 20 insertions(+), 25 deletions(-) rename age_controller.py => other/age_controller.py (55%) diff --git a/age_controller.py b/other/age_controller.py similarity index 55% rename from age_controller.py rename to other/age_controller.py index ed6be634da00..2f16c6b8b81f 100644 --- a/age_controller.py +++ b/other/age_controller.py @@ -1,11 +1,10 @@ """Age Controller Module - This module provides functionality to validate and process age input. Related to issue #12809. """ -def validate_age(age): +def validate_age(age: int | str | float) -> int: """Validate and process age input. This function validates that the provided age is a valid positive integer @@ -35,36 +34,32 @@ def validate_age(age): >>> validate_age(200) Traceback (most recent call last): ... - ValueError: Age must be between 0 and 150 + ValueError: Age must be between 0 and 150 years >>> validate_age('invalid') Traceback (most recent call last): ... - ValueError: Age must be a valid number + ValueError: Invalid age format + >>> validate_age(25.5) + Traceback (most recent call last): + ... + ValueError: Age must be a whole number """ - try: - # Convert to float first to handle string numbers - age_float = float(age) - - # Check if it's a whole number - if age_float != int(age_float): - age_int = int(age_float) - else: - age_int = int(age_float) + # Convert age to appropriate numeric type + age_int = int(age) if isinstance(age, int | float) else int(str(age)) - except (ValueError, TypeError): - raise ValueError("Age must be a valid number") + # Validate that the age is a whole number if it was a float + if isinstance(age, float) and age != age_int: + msg = "Age must be a whole number" + raise ValueError(msg) - # Validate range + # Validate age is positive if age_int < 0: - raise ValueError("Age must be a positive number") + msg = "Age must be a positive number" + raise ValueError(msg) - if age_int > 150: - raise ValueError("Age must be between 0 and 150") + # Validate age is within reasonable range + if age_int > 150: # noqa: PLR2004 + msg = "Age must be between 0 and 150 years" + raise ValueError(msg) return age_int - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From def1c09deb8e34b3dfa5329b1f307e4b20b0b807 Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Tue, 21 Oct 2025 07:44:50 +0530 Subject: [PATCH 4/4] Fix remaining Ruff linter errors (PYI041, RUF100) --- other/age_controller.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/other/age_controller.py b/other/age_controller.py index 2f16c6b8b81f..b43f9ae7fae3 100644 --- a/other/age_controller.py +++ b/other/age_controller.py @@ -4,7 +4,7 @@ """ -def validate_age(age: int | str | float) -> int: +def validate_age(age: float | str) -> int: """Validate and process age input. This function validates that the provided age is a valid positive integer @@ -58,7 +58,7 @@ def validate_age(age: int | str | float) -> int: raise ValueError(msg) # Validate age is within reasonable range - if age_int > 150: # noqa: PLR2004 + if age_int > 150: msg = "Age must be between 0 and 150 years" raise ValueError(msg)