From 498619bf7a658d127c8be1e7e7e465b69dcf6800 Mon Sep 17 00:00:00 2001 From: Mitchel Humpherys Date: Fri, 16 Aug 2024 14:26:25 -0700 Subject: [PATCH 1/3] Handle a single apostrophe more gracefully Currently we raise an exception if you pass a single-character string consisting solely of an apostrophe to engine.plural(), e.g.: >>> import inflect >>> p = inflect.engine() >>> p.plural("'") Traceback (most recent call last): File "", line 1, in File "/home/mgalgs/src/inflect/inflect/__init__.py", line 2386, in plural self._pl_special_adjective(word, count) File "/home/mgalgs/src/inflect/inflect/__init__.py", line 3157, in _pl_special_adjective pl = self.plural_noun(mo.group(1)) File "/home/mgalgs/src/inflect/inflect/__init__.py", line 2393, in plural_noun def plural_noun( File "/home/mgalgs/src/inflect/env/lib/python3.9/site-packages/typeguard/_functions.py", line 136, in check_argument_types check_type_internal(value, annotation, memo) File "/home/mgalgs/src/inflect/env/lib/python3.9/site-packages/typeguard/_checkers.py", line 866, in check_type_internal raise TypeCheckError(f"is not an instance of {qualified_name(origin_type)}") typeguard.TypeCheckError: argument "text" (str) is not an instance of inflect.Word Rather than raising an exception, just return the "'" string back to the caller (since nothing needs to be done to pluralize it). This is accomplished by modifying the "ends with apostrophe s" regex to require that there is at least one character present preceding the apostrophe (rather than "zero or more"), to ensure that the match group isn't empty for the "'" string. --- inflect/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inflect/__init__.py b/inflect/__init__.py index 3eec27f..001e81f 100644 --- a/inflect/__init__.py +++ b/inflect/__init__.py @@ -1960,7 +1960,7 @@ def get_si_pron(thecase, word, gender) -> str: PLVERB_SPECIAL_S_RE = re.compile(rf"^({plverb_special_s})$") WHITESPACE = re.compile(r"\s") ENDS_WITH_S = re.compile(r"^(.*[^s])s$", re.IGNORECASE) -ENDS_WITH_APOSTROPHE_S = re.compile(r"^(.*)'s?$") +ENDS_WITH_APOSTROPHE_S = re.compile(r"^(.+)'s?$") INDEFINITE_ARTICLE_TEST = re.compile(r"\A(\s*)(?:an?\s+)?(.+?)(\s*)\Z", re.IGNORECASE) SPECIAL_AN = re.compile(r"^[aefhilmnorsx]$", re.IGNORECASE) SPECIAL_A = re.compile(r"^[bcdgjkpqtuvwyz]$", re.IGNORECASE) From f60cdca30e359579b4b70b04bdd6714a51239435 Mon Sep 17 00:00:00 2001 From: Mitchel Humpherys Date: Fri, 16 Aug 2024 14:30:12 -0700 Subject: [PATCH 2/3] Add a test for a single-character apostrophe string We recently fixed a bug where a single-character string consisting solely of the apostrophe character raised an exception. Add this test case to the test suite. --- tests/test_pwd.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_pwd.py b/tests/test_pwd.py index e3390dd..ff8064c 100644 --- a/tests/test_pwd.py +++ b/tests/test_pwd.py @@ -743,6 +743,7 @@ def test__pl_general_verb(self): ("tuna's", "tuna's"), ("TUNA's", "TUNA's"), ("bad", False), + ("'", False), pytest.param( "JOHN's", "JOHNS'", From 243e42542067626a152ad11a9e0d00897ee31e00 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 7 Sep 2024 09:02:30 -0400 Subject: [PATCH 3/3] Add news fragment. --- newsfragments/218.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/218.feature.rst diff --git a/newsfragments/218.feature.rst b/newsfragments/218.feature.rst new file mode 100644 index 0000000..129bfe4 --- /dev/null +++ b/newsfragments/218.feature.rst @@ -0,0 +1 @@ +Handle a single apostrophe more gracefully. \ No newline at end of file