click icon indicating copy to clipboard operation
click copied to clipboard

[rejected AI] `Choice.shell_complete` Unicode case-folding mismatch under `case_sensitive=False`

Open codeCraft-Ritik opened this issue 1 month ago • 1 comments

Outline

When case_sensitive=False, Choice.normalize_choice normalizes choice values using .casefold(). However, Choice.shell_complete normalizes the user's incomplete input string using .lower(). In Unicode, .lower() and .casefold() diverge for non-ASCII characters (e.g. 'ß'.lower() == 'ß', while 'ß'.casefold() == 'ss'). Consequently, completing a word containing such characters fails completely because the prefix comparison is performed between a casefolded choice and a lowercased prefix.

Steps to Reproduce

import click

choice = click.Choice(["GROSS", "GROß"], case_sensitive=False)
cmd = click.Command("cmd")
ctx = click.Context(cmd)
param = click.Option(["--type"], type=choice)

completions = choice.shell_complete(ctx, param, "groß")
print("Completions returned:", [c.value for c in completions])
assert len(completions) > 0, "No completions returned for valid prefix 'groß'!"

Traceback / Output

Completions returned: []
Traceback (most recent call last):
  File "test_choice.py", line 10, in <module>
    assert len(completions) > 0, "No completions returned for valid prefix 'groß'!"
AssertionError: No completions returned for valid prefix 'groß'!

Expected Behavior

shell_complete should normalize incomplete using .casefold() so that Unicode case-folding matches choices consistently.

Root Cause Analysis

In src/click/types.py, lines 508–514:

str_choices = [self.normalize_choice(choice, ctx) for choice in self.choices]
if self.case_sensitive:
    matched = (c for c in str_choices if c.startswith(incomplete))
else:
    incomplete = incomplete.lower()
    matched = (c for c in str_choices if c.lower().startswith(incomplete))

normalize_choice has already applied .casefold() to every element of str_choices. Using incomplete.lower() creates an algorithmic mismatch against the casefolded choices, causing .startswith() to return False.

Proposed Fix

--- a/src/click/types.py
+++ b/src/click/types.py
@@ -509,8 +509,8 @@ class Choice(ParamType[_ValueT_co], t.Generic[_ValueT_co]):
         if self.case_sensitive:
             matched = (c for c in str_choices if c.startswith(incomplete))
         else:
-            incomplete = incomplete.lower()
-            matched = (c for c in str_choices if c.lower().startswith(incomplete))
+            incomplete = incomplete.casefold()
+            matched = (c for c in str_choices if c.startswith(incomplete))
 
         return [CompletionItem(c) for c in matched]

Regression Test

def test_choice_case_insensitive_casefold_completion():
    choice = click.Choice(["GROSS", "GROß"], case_sensitive=False)
    cmd = click.Command("cmd")
    ctx = click.Context(cmd)
    param = click.Option(["--type"], type=choice)
    completions = [item.value for item in choice.shell_complete(ctx, param, "groß")]
    assert len(completions) == 2
    assert completions == ["gross", "gross"]

Environment

  • Python version: 3.10 / 3.11 / 3.12 / 3.13 / 3.14
  • Click version: 8.0+ / 8.6.0a0 (main)

Complete Verification & Test Run

Running the full pytest suite after applying the 5 fixes and 6 regression tests:

$ pytest tests/
============================= test session starts =============================
platform win32 -- Python 3.14.7, pytest-9.1.1, pluggy-1.6.0
rootdir: C:\Users\ritik\.gemini\antigravity\scratch\click
configfile: pyproject.toml
collected 33293 items / 31000 deselected / 2293 selected

tests\test_abort_interrupt.py ........                                   [  0%]
tests\test_arguments.py ................................................ [  2%]
tests\test_basic.py .................................................... [ 11%]
tests\test_chain.py ...............x                                     [ 14%]
tests\test_command_decorators.py .........                               [ 15%]
tests\test_commands.py ................................................. [ 17%]
tests\test_compat.py ................................................... [ 21%]
tests\test_context.py ..............................                     [ 28%]
tests\test_custom_classes.py ......                                      [ 28%]
tests\test_defaults.py ..............................................    [ 30%]
tests\test_deprecations.py ............................................. [ 32%]
tests\test_formatting.py .......................................         [ 35%]
tests\test_info_dict.py .......................................          [ 37%]
tests\test_normalization.py ...                                          [ 37%]
tests\test_options.py .................................................. [ 39%]
tests\test_parser.py .......                                             [ 68%]
tests\test_shell_completion.py ......................................... [ 70%]
tests\test_stream_lifecycle.py ........................                  [ 71%]
tests\test_termui.py ................................................... [ 74%]
tests\test_testing.py .................................................. [ 86%]
tests\test_types\test_Choice.py .                                        [ 86%]
tests\test_types\test_File.py ..                                         [ 86%]
tests\test_types\test_FloatRange.py ........                             [ 86%]
tests\test_types\test_IntRange.py ..............                         [ 87%]
tests\test_types\test_Path.py .s.s.....                                  [ 87%]
tests\test_types\test_convert_type.py ..............................     [ 89%]
tests\test_utils\test_style.py ......................................... [ 97%]

======= 2184 passed, 108 skipped, 31000 deselected, 1 xfailed in 5.19s ========

codeCraft-Ritik avatar Oct 23 '26 04:10 codeCraft-Ritik

https://palletsprojects.com/contributing/llm-ai

davidism avatar Oct 23 '26 04:10 davidism