Repository: sqlfluff/sqlfluff
Pull request: Remove requirement for Click>=8
<!--Thanks for adding this feature!-->

<!--Please give the Pull Request a meaningful title for the release notes-->

### Brief summary of the change made
<!--Please include `fixes #XXXX` to automatically close any corresponding issue when the pull request is merged. Alternatively if not fully closed you can say `makes progress on #XXXX`.-->
Fixes #2543 

Not sure if this it the best way of doing this in Python, and don't think we can add test cases, so feel free to suggest improvements

### Are there any other side effects of this change that we should be aware of?
Shouldn't be

### Pull Request checklist
- [ ] Please confirm you have completed any of the necessary steps below.

- Included test cases to demonstrate any code changes, which may be one or more of the following:
  - `.yml` rule test cases in `test/fixtures/rules/std_rule_cases`.
  - `.sql`/`.yml` parser test cases in `test/fixtures/dialects` (note YML files can be auto generated with `tox -e generate-fixture-yml`).
  - Full autofix test cases in `test/fixtures/linter/autofix`.
  - Other.
- Added appropriate documentation for the change.
- Created GitHub issues for any relevant followup/future enhancements if appropriate.


Problem statement:
Less strict click version
From what I get the pinned click requirement >= 8.0.0 was added to support autocompletion of the cli. Could that be made optional  and the version requirement be lowered. It makes it difficult for us to combine in env with other old apps. It might be an edge case, I don't know but we get hit by it through and old airflow -> flask version.  

Get that it's not the intention by we and I would assume at a lot of other users prefer to have sqlfluff as a lib and used in other cli's or apps.

Hints from issue discussion:
I think that's a fair ask, since it's an optional extra. Does seem to be [causing other issues too](https://github.com/megalinter/megalinter/issues/1224).

Commit message: Add pragma no covers

Patch to review:
diff --git a/requirements.txt b/requirements.txt
index 45ea07600cd..11dd55ba824 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -5,7 +5,7 @@ appdirs
 backports.cached-property; python_version < '3.8'
 # To get the encoding of files.
 chardet
-click>=8.0
+click
 colorama>=0.3
 # Used for diffcover plugin
 diff-cover>=2.5.0
diff --git a/setup.cfg b/setup.cfg
index ffd82c913e0..aa7fb91fb7f 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -66,7 +66,7 @@ install_requires =
     backports.cached-property; python_version < '3.8'
     # To get the encoding of files.
     chardet
-    click>=8.0
+    click
     colorama>=0.3
     # Used for diffcover plugin
     diff-cover>=2.5.0
diff --git a/src/sqlfluff/cli/autocomplete.py b/src/sqlfluff/cli/autocomplete.py
index d855cdac574..96a99abed8e 100644
--- a/src/sqlfluff/cli/autocomplete.py
+++ b/src/sqlfluff/cli/autocomplete.py
@@ -1,9 +1,15 @@
 """autocompletion commands."""
 
-from click.shell_completion import CompletionItem
-
 from sqlfluff import list_dialects
 
+# Older versions of click don't have shell completion
+# so handle for now, as version 8 still fairly recent
+shell_completion_enabled = True
+try:
+    completion = __import__("click.shell_completion", fromlist=["click"])
+except ImportError:  # pragma: no cover
+    shell_completion_enabled = False
+
 
 def dialect_shell_complete(ctx, param, incomplete):
     """Shell completion for possible dialect names.
@@ -13,5 +19,7 @@ def dialect_shell_complete(ctx, param, incomplete):
     """
     dialect_names = [e.name for e in list_dialects()]
     return [
-        CompletionItem(name) for name in dialect_names if name.startswith(incomplete)
+        completion.CompletionItem(name)
+        for name in dialect_names
+        if name.startswith(incomplete)
     ]
diff --git a/src/sqlfluff/cli/commands.py b/src/sqlfluff/cli/commands.py
index 1805024e0b4..b3f16409cb2 100644
--- a/src/sqlfluff/cli/commands.py
+++ b/src/sqlfluff/cli/commands.py
@@ -25,7 +25,7 @@
 # To enable colour cross platform
 import colorama
 from tqdm import tqdm
-from sqlfluff.cli.autocomplete import dialect_shell_complete
+from sqlfluff.cli.autocomplete import shell_completion_enabled, dialect_shell_complete
 
 from sqlfluff.cli.formatters import (
     format_rules,
@@ -175,12 +175,21 @@ def core_options(f: Callable) -> Callable:
     These are applied to the main (but not all) cli commands like
     `parse`, `lint` and `fix`.
     """
-    f = click.option(
-        "--dialect",
-        default=None,
-        help="The dialect of SQL to lint (default=ansi)",
-        shell_complete=dialect_shell_complete,
-    )(f)
+    # Only enable dialect completion if on version of click
+    # that supports it
+    if shell_completion_enabled:
+        f = click.option(
+            "--dialect",
+            default=None,
+            help="The dialect of SQL to lint (default=ansi)",
+            shell_complete=dialect_shell_complete,
+        )(f)
+    else:  # pragma: no cover
+        f = click.option(
+            "--dialect",
+            default=None,
+            help="The dialect of SQL to lint (default=ansi)",
+        )(f)
     f = click.option(
         "--templater",
         default=None,


Code review comments:
diff_hunk: @@ -1,9 +1,15 @@
 """autocompletion commands."""
 
-from click.shell_completion import CompletionItem
-
 from sqlfluff import list_dialects
 
+# Older versions of click don't have shell completion
+# so handle for now, as version 8 still fairly recent
+shell_completion_enabled = True
+try:
+    completion = __import__("click.shell_completion", fromlist=["click"])
path: src/sqlfluff/cli/autocomplete.py
text: @user1:
You can use the normal.`import` command here -- no need to use the function.

@author:
Oh yeah. Went round the houses as this was originally importing just one class where that doesn't work when the module doesn't exist. But works fine now just importing the module.

Fixed!