Search code examples
pythonpre-commit-hookpre-commit.com

Incompatibility click and black versions with Poetry ( ImportError: cannot import name '_unicodefun' from 'click' )


The incompatibility between click 8.1.1 and black <=22.1.0 has been reported and apparently fixed here

But apparently still a problem with poetry, as reported (and also apparently fixed) here

Config:

Running poetry run pre-commit run --all-files results systematically in:

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

Traceback (most recent call last):
  File "/Users/charlotteweil1/.cache/pre-commit/repoqg3mkzrn/py_env-python3.9/bin/black", line 8, in <module>
    sys.exit(patched_main())
  File "/Users/charlotteweil1/.cache/pre-commit/repoqg3mkzrn/py_env-python3.9/lib/python3.9/site-packages/black/__init__.py", line 1372, in patched_main
    patch_click()
  File "/Users/charlotteweil1/.cache/pre-commit/repoqg3mkzrn/py_env-python3.9/lib/python3.9/site-packages/black/__init__.py", line 1358, in patch_click
    from click import _unicodefun
ImportError: cannot import name '_unicodefun' from 'click' (/Users/charlotteweil1/.cache/pre-commit/repoqg3mkzrn/py_env-python3.9/lib/python3.9/site-packages/click/__init__.py)

I've tried removing the whole repoqg3mkzrn directory in .cache/pre-commit and re-installing pre-commit, but behavior is identical :(

Thanks in advance!


Solution

  • you'll want to check your .pre-commit-config.yaml -- you almost certainly have an outdated version of black configured there. the easiest fix is to run pre-commit autoupdate to get to the latest version

    alternatively, if you would like to remain on an outdated version of black you can force an older version of click:

    -   repo: https://github.com/psf/black
        rev: ...  # whatever old version
        hooks:
        -   id: black
            additional_dependencies: ['click<8.1']
    

    disclaimer: I created pre-commit