Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatible with click 8.1.0 (ImportError: cannot import name '_unicodefun' from 'click') #2964

Labels
C: packaging Installation and packaging of Black T: bug Something isn't working

Comments

@stumpylog
Copy link

stumpylog commented Mar 28, 2022

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

@stumpylog stumpylog added the T: bug Something isn't working label Mar 28, 2022
@liranbg
Copy link

liranbg commented Mar 28, 2022

@stumpylog
try with locking click to click==8.0.1

@bradleyhurley
Copy link

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

@woodruffw
Copy link
Member

+1: We have a failure in pip-audit here: pypa/pip-audit#249

(click is strictly a subdependency of ours, so pinning it isn't an ideal workaround.)

@ichard26
Copy link
Collaborator

ichard26 commented Mar 28, 2022

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

@ichard26 ichard26 added the C: packaging Installation and packaging of Black label Mar 28, 2022
@ichard26 ichard26 pinned this issue Mar 28, 2022
woodruffw added a commit to pypa/pip-audit that referenced this issue Mar 28, 2022
@scasagrande
Copy link

scasagrande commented Mar 28, 2022

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

@JelleZijlstra JelleZijlstra changed the title Github Action Fails with ImportError from 'click' Incompatible with click 8.1.0 Mar 28, 2022
@JelleZijlstra
Copy link
Collaborator

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

@davidism
Copy link

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

@woodruffw
Copy link
Member

woodruffw commented Mar 28, 2022

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

Looks like this is why:

def patch_click() -> None:
    """Make Click not crash on Python 3.6 with LANG=C.
    On certain misconfigured environments, Python 3 selects the ASCII encoding as the
    default which restricts paths that it can access during the lifetime of the
    application.  Click refuses to work in this scenario by raising a RuntimeError.
    In case of Black the likelihood that non-ASCII characters are going to be used in
    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.
    """
    try:
        from click import core
        from click import _unicodefun
    except ModuleNotFoundError:
        return

    for module in (core, _unicodefun):
        if hasattr(module, "_verify_python3_env"):
            module._verify_python3_env = lambda: None  # type: ignore
        if hasattr(module, "_verify_python_env"):
            module._verify_python_env = lambda: None  # type: ignore

So funnily enough, this should still work, it's just catching the wrong exception.

@mjpieters
Copy link

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.

@davidism
Copy link

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. 😄

@ProGamerGov
Copy link

This causing black to fail when run on Google Colab right now as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment