Skip to content

Reject rule radix below 2 - #18

Merged
synesthesiam merged 1 commit into
mainfrom
fix-radix-guard
Nov 19, 2026
Merged

synesthesiam merged 1 commit into
mainfrom
fix-radix-guard

Conversation

@synesthesiam

Copy link
Copy Markdown
Member

What

RbnfRule now raises ValueError when it's created with a radix below 2.

Why

Since #17, the divisor is found by multiplying up to the largest power of the radix that is at or below the rule's value. With a radix of 0 or 1 that loop never ends, so a rule added via add_rule(..., radix=1) would hang the first time it formatted a number (before #17 it raised from math.log). No bundled CLDR file uses such a radix; this only guards hand-added rules.

Notes for review

  • The check is in RbnfRule.__post_init__, so it covers both add_rule and XML loading.
  • In RbnfRule.parse, the rule is now constructed outside the try that catches ValueError from int(value_str). Otherwise the new error would be swallowed and logged as "Unrecognized special rule".
  • CHANGELOG.md: added this change and the Fix divisor computation: use highest power of radix <= rule value #17 divisor fix (which wasn't listed) under "Unreleased".

Testing

  • New test_invalid_radix_is_rejected covers radix 0, 1 and -10.
  • pytest: 135 passed. script/lint (black, isort, flake8, pylint, mypy): clean.

🤖 Generated with Claude Code

The divisor loop looks for the largest power of the radix at or below
the rule's value, which never terminates for a radix of 0 or 1. Raise
ValueError when such a rule is created instead of hanging later while
formatting.

Also note PR #17's divisor fix in the changelog.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@synesthesiam
synesthesiam merged commit 2ed04b9 into main Nov 19, 2026
1 check passed
@synesthesiam
synesthesiam deleted the fix-radix-guard branch November 19, 2026 17:00
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.

1 participant