From 3047685f71ae147e617c1881035278ae01d0bb64 Mon Sep 17 00:00:00 2001 From: santhreal <64453045+santhreal@users.noreply.github.com> Date: Thu, 16 Jul 2026 20:22:53 -0700 Subject: [PATCH] Fix KeyError in Catalan to_ordinal for exact tens MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit to_ordinal built the tens ordinals (30, 40, ..., 90) by appending a units part looked up in self.ords_3, which only has keys 1-9. For an exact multiple of ten the units index is 0, so self.ords_3[0] raised KeyError: 0. This also broke every higher ordinal that recurses through a whole ten (for example 130 -> "cent trentè"). Emit the tens ordinal as the tens stem plus the gender suffix when there is no units part, matching how 20 ("vintè") and 100 ("centè") are already handled. Add the previously missing exact-tens cases to the ordinal test data. --- num2words/lang_CA.py | 15 ++++++++++++--- tests/test_ca.py | 8 ++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/num2words/lang_CA.py b/num2words/lang_CA.py index 97d0f29e..377f7ee9 100644 --- a/num2words/lang_CA.py +++ b/num2words/lang_CA.py @@ -406,11 +406,20 @@ def to_ordinal(self, value): text = "%s%s" % (self.ords[value], self.gender_stem) elif value <= 30: frac = value % 10 - text = "%s%s%s" % (self.ords[20], "-i-", self.ords_3[frac]) + if frac == 0: + # An exact ten ("trentè") has no units part to append. + text = "%s%s" % (self.ords[value], self.gender_stem) + else: + text = "%s%s%s" % (self.ords[20], "-i-", self.ords_3[frac]) elif value < 100: dec = (value // 10) * 10 - text = "%s%s%s%s" % (self.ords[dec], "a", - "-", self.ords_3[value - dec]) + frac = value - dec + if frac == 0: + # An exact ten ("quarantè") has no units part to append. + text = "%s%s" % (self.ords[dec], self.gender_stem) + else: + text = "%s%s%s%s" % (self.ords[dec], "a", + "-", self.ords_3[frac]) elif value == 1e2: text = "%s%s" % (self.ords[value], self.gender_stem) elif value < 2e2: diff --git a/tests/test_ca.py b/tests/test_ca.py index ab5be158..b537fa61 100644 --- a/tests/test_ca.py +++ b/tests/test_ca.py @@ -81,10 +81,18 @@ (12, "dotzè"), (14, "catorzè"), (28, "vint-i-vuitè"), + (30, "trentè"), (33, "trenta-tresè"), + (40, "quarantè"), + (50, "cinquantè"), + (60, "seixantè"), + (70, "setantè"), + (80, "vuitantè"), (88, "vuitanta-vuitè"), + (90, "norantè"), (100, "centè"), (128, "cent vint-i-vuitè"), + (130, "cent trentè"), (199, "cent noranta-novè"), (1000, "milè"), (1827, "mil vuit-cents vint-i-setè"),