black: 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

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 241
  • Comments: 40 (2 by maintainers)

Commits related to this issue

Most upvoted comments

For those that are running black via pre-commit, you can pin click to 8.0.4 via the following:

- repo: https://github.com/psf/black
  rev: 22.1.0
  hooks:
    - id: black
      additional_dependencies: ['click==8.0.4']

Update - new version is available

- repo: https://github.com/psf/black
  rev: 22.3.0
  hooks:
    - id: black

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

Why was black importing an internal module? I was not expecting this to be the change that caused issues. Sorry about that.

We just released Black 22.3.0 with the fix. Sorry for the disruption everyone!

(There are no compiled wheels with the release yet, we’ll upload those shortly.)

We should probably pin to <8.1.0 for now. I can help get a release out later today to do that.

Well good news at least, that whole patch isn’t needed anymore, since Click itself is now at the same point of not raising locale encoding errors. 😄

@stumpylog try with locking click to click==8.0.1

When running locally, after updating versions, try following commands:

pre-commit clean
pre-commit autoupdate
pre-commit run --all

can confirm this issue is fixed in black 22.3.0.

Oh wow, Click 8.1.0 drops support for Python 3.6. We will have to either drop 3.6 soon or pin Click up to 8.0.4.

This is wrong, if we still support Click 8.0.0 until 8.0.4, as long as the python_requires metadata is right pip will install the older acceptable click keeping everything working. Take this as a lesson to not jump to conclusions in stressful situations 😃

Oh wow, Click 8.1.0 drops support for Python 3.6. We will have to either drop 3.6 soon or pin Click up to 8.0.4. very wrong, see below

+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.)

@iboates you have an outdated version of Black in your pre-commit environment. See this comment for a possible solution: https://github.com/psf/black/issues/2964#issuecomment-1083591334

@ehontoria we don’t provide bugfixes for old releases. You need to either update to 22.3.0 or pin Click 8.1.0 (as @angel-langdon already said).

Note that thanks to our stability policy (https://black.readthedocs.io/en/stable/the_black_code_style/index.html#stability-policy), upgrading from 22.1.0 should not cause any formatting changes.

@Edwardx89 you are running pip freeze on your pipeapp venv, but black is installed and running under your isolated pre-commit venv

You are not running the version you think you are

Hi folks, just looking for an explicit answer here about whether or not this fix will be released for versions earlier than 22.3.0? Or is the expectation that all previous versions of black will need a click version pin forever?

@ehontoria This bug is fixed in black==22.3.0 you should update it

It is still not working:

pip list
black               22.3.0   
click               8.1.0    

...
black....................................................................Failed
- hook id: black
- exit code: 1

pip list doesn’t tell you anything about what version pre-commit uses, because pre-commit has their own virtualenvs for hooks and sets the version from your hook configuration.

You can see the virtualenv path pre-commit created for black in your traceback:

/home/ebsym/.cache/pre-commit/repomlzam_dy/py_env-python3/bin/black

so you’d have to run /home/ebsym/.cache/pre-commit/repomlzam_dy/py_env-python3/bin/python -m pip list to see what versions are installed there.

Bottom line: Edit your .pre-commit-config.yaml file instead.

It is still not working:

pip list
black               22.3.0   
click               8.1.0    

...
black....................................................................Failed
- hook id: black
- exit code: 1

Traceback (most recent call last): File “/home/ebsym/.cache/pre-commit/repomlzam_dy/py_env-python3/bin/black”, line 8, in <module> sys.exit(patched_main()) File “/home/ebsym/.cache/pre-commit/repomlzam_dy/py_env-python3/lib/python3.8/site-packages/black/init.py”, line 1282, in patched_main patch_click() File “/home/ebsym/.cache/pre-commit/repomlzam_dy/py_env-python3/lib/python3.8/site-packages/black/init.py”, line 1268, in patch_click from click import _unicodefun # type: ignore ImportError: cannot import name ‘_unicodefun’ from ‘click’ (/home/ebsym/.cache/pre-commit/repomlzam_dy/py_env-python3/lib/python3.8/site-packages/click/init.py)

@Edwardx89 it is working across the board for me, I don’t think you’re running the version you think you are.

Mypyc compiled wheels are now also available 🚀 image

The import doesn’t raise ModuleNotFoundError, only a plain ImportError, because from click import _unicodefun is used.

The code would need to be changed to import click._unicodefun (and import click.core) or catch the correct exception.