Repository: pallets/click (python)

Issue:
shell completion is case sensitive, even with click.Choice(case_sensitive=False)
### Expected Behavior

Since I set case_sensitive to False, I would expect shell completions to search without case sensitivity.

```python
import click
from click._bashcomplete import get_choices

options = ['Paul', 'Simon', 'Art', 'Garfunkel']

@click.command()
@click.option('--opt', type=click.Choice(options))
def demo():
    pass

completions = list(get_choices(demo, 'dummy', ['--opt'], 'gar'))
assert len(completions) == 1, "expected to find 'Garfunkel' as a completion item"
```

### Actual Behavior

No completions are found. However, if I change the `incomplete` parameter to `"Gar"`, one completion is found as expected.

### Environment

* Python version: 3.7.7
* Click version: 7.0 (but I've reviewed the code on master and it doesn't seem to have a fix)


Pull request:
Case insensitive shell completions for Choice(..., case_sensitive=False)
This PR adds the logic and a test for checking case insensitive completions for `Choice` instances which have the argument `case_sensitive=False`. 

What changed was having the `Choice` class' `shell_complete`-method take `self.case_sensitive` into account when matching on the provided instance choices by using temporary string transforms on the `incomplete` argument and `self.choices` into all lower case when `self.case_sensitive=False`.

- Fixes #1692 

Checklist:

- [x] Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
- [x] Add or update relevant docs, in the docs folder and in code.
- [x] Add an entry in `CHANGES.rst` summarizing the change and linking to the issue.
- [x] Add `.. versionchanged::` entries in any relevant code docs.
- [x] Run `pre-commit` hooks and fix any issues.
- [x] Run `pytest` and `tox`, no tests failed. (**NOTE**: No test results changed with this contribution, there was 1 failing test though)


Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 4845673..cb6cd13 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -151,6 +151,8 @@ Unreleased
     ending of a phrase or double linebreak. :issue:`1082`
 -   Skip progress bar render steps for efficiency with very fast
     iterators by setting ``update_min_steps``. :issue:`676`
+-   Respect ``case_sensitive=False`` when doing shell completion for
+    ``Choice`` :issue:`1692`
 
 
 Version 7.1.2
diff --git a/src/click/types.py b/src/click/types.py
index 8c886d6..626287d 100644
--- a/src/click/types.py
+++ b/src/click/types.py
@@ -274,7 +274,14 @@ class Choice(ParamType):
         from click.shell_completion import CompletionItem
 
         str_choices = map(str, self.choices)
-        return [CompletionItem(c) for c in str_choices if c.startswith(incomplete)]
+
+        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))
+
+        return [CompletionItem(c) for c in matched]
 
 
 class DateTime(ParamType):


Test patch:
diff --git a/tests/test_shell_completion.py b/tests/test_shell_completion.py
index e85dc32..40cb88f 100644
--- a/tests/test_shell_completion.py
+++ b/tests/test_shell_completion.py
@@ -304,3 +304,13 @@ def test_context_settings(runner):
         env={"COMP_WORDS": "", "COMP_CWORD": "0", "_CLI_COMPLETE": "bash_complete"},
     )
     assert result.output == "plain,a\nplain,b\n"
+
+
+@pytest.mark.parametrize(("value", "expect"), [(False, ["Au", "al"]), (True, ["al"])])
+def test_choice_case_sensitive(value, expect):
+    cli = Command(
+        "cli",
+        params=[Option(["-a"], type=Choice(["Au", "al", "Bc"], case_sensitive=value))],
+    )
+    completions = _get_words(cli, ["-a"], "a")
+    assert completions == expect