Repository: psf/black

Issue:
Incompatible with click 8.1.0 (ImportError: cannot import name '_unicodefun' from 'click')
Please use Black 22.3.0 (or newer) which was just released to fix this issue or pin Click to 8.0.0, 8.0.1, 8.0.2, 8.0.3, or 8.0.4.

~ @ichard26 at March 28 3:15 PM EST

---

So hey y'all, CI is broken eh?

Anyway, we are aware the most recent release of Click, 8.1.0, is breaking Black. This is because Black imports an internal module so Python 3.6 users with misconfigured LANG continues to work mostly properly. The code that patches click was supposed to be resilient to the module disappearing but the code was catching the wrong exception. 

**To unbreak your CI, please pin Click to 8.0.0, 8.0.1, 8.0.2, 8.0.3, or 8.0.4. We are working on a more longterm fix (probably fixing the patching code or getting rid of it entirely) and will try to get a release out soon.**

~ @ichard26 at March 28 2:29 PM EST

---

**Describe the bug**

The Github Action step:

```
- name: Run black
  uses: psf/black@stable
  with:
     options: "--check --diff"
      version: "22.1.0"
```

fails with the following trackeback:

```
Traceback (most recent call last):
  File "/home/runner/work/_actions/psf/black/stable/.black-env/bin/black", line 8, in <module>
    sys.exit(patched_main())
  File "src/black/__init__.py", line 1423, in patched_main
  File "src/black/__init__.py", line 1[40](https://github.com/stumpylog/paperless-ngx/runs/5724998040?check_suite_focus=true#step:7:40)9, in patch_click
ImportError: cannot import name '_unicodefun' from 'click' (/home/runner/work/_actions/psf/black/stable/.black-env/lib/python3.8/site-packages/click/__init__.py)
```

**To Reproduce**

Run the `black` action on a Github runner, configured as above.

**Expected behavior**

The action should only fail if files are not formatted.

**Environment**

OS: `runs-on: ubuntu-20.04`
Version: `22.1.0`
Action: `@stable`
Python version: `3.8` I believe

**Additional context**



Discussion:
@stumpylog 
try with locking `click` to `click==8.0.1`
I just came here to open a very similar issue. 

The issue is caused by the release of click 8.1.0. 

Install `click==8.0.4`
+1: We have a failure in `pip-audit` here: https://github.com/trailofbits/pip-audit/pull/249

(`click` is strictly a subdependency of ours, so pinning it isn't an ideal workaround.)

Resolving patch:
diff --git a/src/black/__init__.py b/src/black/__init__.py
--- a/src/black/__init__.py
+++ b/src/black/__init__.py
@@ -1427,13 +1427,21 @@ def patch_click() -> None:
     file paths is minimal since it's Python source code.  Moreover, this crash was
     spurious on Python 3.7 thanks to PEP 538 and PEP 540.
     """
+    modules: List[Any] = []
     try:
         from click import core
+    except ImportError:
+        pass
+    else:
+        modules.append(core)
+    try:
         from click import _unicodefun
-    except ModuleNotFoundError:
-        return
+    except ImportError:
+        pass
+    else:
+        modules.append(_unicodefun)
 
-    for module in (core, _unicodefun):
+    for module in modules:
         if hasattr(module, "_verify_python3_env"):
             module._verify_python3_env = lambda: None  # type: ignore
         if hasattr(module, "_verify_python_env"):


Test patch:
diff --git a/tests/test_black.py b/tests/test_black.py
--- a/tests/test_black.py
+++ b/tests/test_black.py
@@ -1257,7 +1257,7 @@ def test_assert_equivalent_different_asts(self) -> None:
     def test_shhh_click(self) -> None:
         try:
             from click import _unicodefun
-        except ModuleNotFoundError:
+        except ImportError:
             self.skipTest("Incompatible Click version")
         if not hasattr(_unicodefun, "_verify_python3_env"):
             self.skipTest("Incompatible Click version")