Handle negative numbers in ordinal() (#192) - #259
Open
apoorvdarshan wants to merge 1 commit into
Open
Conversation
ordinal() raised a TypeError for negative integers (they fell through to _sub_ord, which runs re.sub on the value) and returned a bare 'th' suffix for negative strings (e.g. '-1th'), because DIGIT.match() does not match a leading '-'. Accept an optional leading '-' and base the suffix on abs(n) (since '-1 % 10' is 9 in Python), so ordinal(-1) is '-1st', ordinal(-113) is '-113th', etc. Positive and word inputs are unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses the negative-number cases of #192.
Problem
ordinal()mishandles negative numbers:ordinal()usesDIGIT.match(str(num))to decide whether the input is numeric, butDIGIT(\d) does not match a leading-. So negative numbers fall through to_sub_ord, which is meant for word input (e.g."one"→"first"). For a negative string it just appends"th"; for a negative int it runsre.subon the integer and raisesTypeError.Fix
-in the numeric branch.abs(n)—-1 % 10is9in Python, which would give the wrong suffix.Positive numbers, floats, and word inputs are unchanged (e.g.
ordinal(1)→'1st',ordinal(1.5)→'1.5th',ordinal("one")→'first').This addresses the negative-number cases raised in #192. I left the
ordinal(1.0)→'1.0st'question alone, since whether that should be'1st','1.0th', or unchanged is a design call for you rather than a clear defect.Testing
test_negative_ordinalstotests/test_inflections.py(ints and strings, including the teens which correctly keep"th"). It raisesTypeErroronmainand passes with this change.215 passed, 16 xfailed— no regressions. Doctests pass.ruff formatclean and the change introduces no newruff checkfindings.newsfragments/192.bugfix.rst.Disclosure: this change was prepared with the assistance of an AI tool (Claude Code). I reproduced the issue, implemented and verified the fix and tests, ran the suite and linters, and take responsibility for the contribution and will respond to review feedback personally.