Skip to content

Commit 0bf1d69

Browse files
authored
Fix Ruff linter errors: use ternary operator and move to other/ directory
1 parent 98046a3 commit 0bf1d69

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed
Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
"""Age Controller Module
2-
32
This module provides functionality to validate and process age input.
43
Related to issue #12809.
54
"""
65

76

8-
def validate_age(age):
7+
def validate_age(age: int | str | float) -> int:
98
"""Validate and process age input.
109
1110
This function validates that the provided age is a valid positive integer
@@ -35,36 +34,32 @@ def validate_age(age):
3534
>>> validate_age(200)
3635
Traceback (most recent call last):
3736
...
38-
ValueError: Age must be between 0 and 150
37+
ValueError: Age must be between 0 and 150 years
3938
>>> validate_age('invalid')
4039
Traceback (most recent call last):
4140
...
42-
ValueError: Age must be a valid number
41+
ValueError: Invalid age format
42+
>>> validate_age(25.5)
43+
Traceback (most recent call last):
44+
...
45+
ValueError: Age must be a whole number
4346
"""
44-
try:
45-
# Convert to float first to handle string numbers
46-
age_float = float(age)
47-
48-
# Check if it's a whole number
49-
if age_float != int(age_float):
50-
age_int = int(age_float)
51-
else:
52-
age_int = int(age_float)
47+
# Convert age to appropriate numeric type
48+
age_int = int(age) if isinstance(age, int | float) else int(str(age))
5349

54-
except (ValueError, TypeError):
55-
raise ValueError("Age must be a valid number")
50+
# Validate that the age is a whole number if it was a float
51+
if isinstance(age, float) and age != age_int:
52+
msg = "Age must be a whole number"
53+
raise ValueError(msg)
5654

57-
# Validate range
55+
# Validate age is positive
5856
if age_int < 0:
59-
raise ValueError("Age must be a positive number")
57+
msg = "Age must be a positive number"
58+
raise ValueError(msg)
6059

61-
if age_int > 150:
62-
raise ValueError("Age must be between 0 and 150")
60+
# Validate age is within reasonable range
61+
if age_int > 150: # noqa: PLR2004
62+
msg = "Age must be between 0 and 150 years"
63+
raise ValueError(msg)
6364

6465
return age_int
65-
66-
67-
if __name__ == "__main__":
68-
import doctest
69-
70-
doctest.testmod()

0 commit comments

Comments
 (0)