From dcbc45f1c952b675395d1a436dcbadee3c48c1db Mon Sep 17 00:00:00 2001 From: gaoflow <148639492+gaoflow@users.noreply.github.com> Date: Sun, 28 Jun 2026 13:27:54 +0200 Subject: [PATCH] Fix parameterize regex collapsing consecutive multi-character separators When parameterize is called with a multi-character separator (e.g. '__'), the pattern r'%s{2,}' % re.escape(separator) incorrectly applies the {2,} quantifier only to the last character of the separator, causing non-separator characters to be consumed during the collapse step. For example, with separator '__', the string '___' (one separator plus one literal underscore) was incorrectly collapsed to '__'. Fix by wrapping the separator in a (?:...) non-capturing group so the quantifier applies to the full separator string. --- inflection/__init__.py | 2 +- test_inflection.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/inflection/__init__.py b/inflection/__init__.py index ffac5ce..a5337c0 100644 --- a/inflection/__init__.py +++ b/inflection/__init__.py @@ -277,7 +277,7 @@ def parameterize(string: str, separator: str = '-') -> str: if separator: re_sep = re.escape(separator) # No more than one of the separator in a row. - string = re.sub(r'%s{2,}' % re_sep, separator, string) + string = re.sub(r'(?:%s){2,}' % re_sep, separator, string) # Remove leading/trailing separator. string = re.sub(r"(?i)^{sep}|{sep}$".format(sep=re_sep), '', string) diff --git a/test_inflection.py b/test_inflection.py index f1e64a3..53c04d8 100644 --- a/test_inflection.py +++ b/test_inflection.py @@ -402,6 +402,13 @@ def test_parameterize_with_multi_character_separator( ) +def test_parameterize_with_multi_character_separator_partial_collapse() -> None: + # Regression test: multi-character separator should not consume + # non-separator characters that happen to overlap with separator chars. + # '__' is a 2-char separator, so '___' = one separator + one literal '_'. + assert 'test___case' == inflection.parameterize('test___case', '__') + + @pytest.mark.parametrize( ("some_string", "parameterized_string"), STRING_TO_PARAMETERIZE_WITH_NO_SEPARATOR