Skip to content

Raise Invalid for Infinity/NaN input to Number validator - #542

Merged
alecthomas merged 1 commit into
alecthomas:masterfrom
SAY-5:fix-number-infinity-nan-invalid
Jul 11, 2026
Merged

Raise Invalid for Infinity/NaN input to Number validator#542
alecthomas merged 1 commit into
alecthomas:masterfrom
SAY-5:fix-number-infinity-nan-invalid

Conversation

@SAY-5

@SAY-5 SAY-5 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #541.

Number(precision, scale) documents :raises Invalid:, but Number(precision=6, scale=2)("Infinity") (also "NaN"/"-Infinity") leaks a raw TypeError("infinity and NaN have no precision"). Decimal(number) succeeds for these, then as_tuple().exponent is a string rather than an int, so _get_precision_scale fell into the else branch that raised TypeError. The commented-out line there already showed the intended behaviour.

The else now raises Invalid with the same message the InvalidOperation sibling uses, so a non-finite value is rejected like any other unusable number. Added test_number_validation_with_infinity_and_nan mirroring test_number_validation_with_string; it fails before the change and passes after. Full suite: 168 passed, flake8 clean.

This is separate from #539, which handles None/dict/bytes before the Decimal() call and explicitly leaves this non-finite path alone.

@SAY-5
SAY-5 force-pushed the fix-number-infinity-nan-invalid branch 2 times, most recently from 67636bc to 1c39734 Compare July 2, 2026 08:42
@alecthomas

Copy link
Copy Markdown
Owner

This has conflicts now that #539 is merged.

@SAY-5
SAY-5 force-pushed the fix-number-infinity-nan-invalid branch from 1c39734 to e3470ef Compare July 10, 2026 19:49
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
@SAY-5
SAY-5 force-pushed the fix-number-infinity-nan-invalid branch from e3470ef to 8bc6fa2 Compare July 10, 2026 19:55
@SAY-5

SAY-5 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto master. #539 changed the Decimal(...) except clause, but the Infinity/NaN branch this PR targets is the else after the exponent check, which still raised a raw TypeError, so the two changes are complementary. Conflicts are resolved and the branch is a single commit on current master.

@alecthomas
alecthomas merged commit 44593ce into alecthomas:master Jul 11, 2026
8 checks passed
@alecthomas

Copy link
Copy Markdown
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Number validator leaks a raw TypeError on Infinity/NaN instead of raising Invalid

2 participants