Black: Pre-commit black hook does not work with python 2.7

Created on 22 Mar 2019  路  3Comments  路  Source: psf/black

Howdy! Sorry you're having trouble. To expedite your experience,
provide some basics for me:

Operating system:
Python version: 3.7 is installed but I'm in a virtualenv running 2.7
Black version:
Does also happen on master:

To answer the last question, you have two options:

  1. Use the online formatter at https://black.now.sh/?version=master, which will use the latest master branch.
  2. Or run black on your machine:

    • create a new virtualenv (make sure it's the same Python version);

    • clone this repository;

    • run pip install -e .;

    • make sure it's sane by running python setup.py test; and

    • run black like you did last time.

Hi, I work at a robotics company and I am attempting to set up a pre-commit hook for our repo.
As most of our tools use python2.7 I am attempting to set up pre-commit for python2.7
When attempting to install pre-commit for my repository I fill my .pre-commit-config.yaml as follows:

repos:
-   repo: https://github.com/ambv/black
    rev: stable
    hooks:
    - id: black
      language_version: python2.7

When I then proceed to commit .pre-commit-config.yaml I run into the following error:

[INFO] Installing environment for https://github.com/ambv/black.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: Command: ('/Users/joel/.cache/pre-commit/repotfwhre6k/py_env-python2.7/bin/python2.7', '/Users/joel/.cache/pre-commit/repotfwhre6k/py_env-python2.7/bin/pip', 'install', '.')
Return code: 1
Expected return code: 0
Output:
    Processing /Users/joel/.cache/pre-commit/repotfwhre6k
      Installing build dependencies: started
      Installing build dependencies: finished with status 'done'
      Getting requirements to build wheel: started
      Getting requirements to build wheel: finished with status 'error'
      Complete output from command /Users/joel/.cache/pre-commit/repotfwhre6k/py_env-python2.7/bin/python2.7 /Users/joel/.cache/pre-commit/repotfwhre6k/py_env-python2.7/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py get_requires_for_build_wheel /var/folders/w8/ll36bzd537bgwgjwqtvy86hc0000gn/T/tmpwlo16H:
      Traceback (most recent call last):
        File "/Users/joel/.cache/pre-commit/repotfwhre6k/py_env-python2.7/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py", line 207, in <module>
          main()
        File "/Users/joel/.cache/pre-commit/repotfwhre6k/py_env-python2.7/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py", line 197, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "/Users/joel/.cache/pre-commit/repotfwhre6k/py_env-python2.7/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py", line 54, in get_requires_for_build_wheel
          return hook(config_settings)
        File "/private/var/folders/w8/ll36bzd537bgwgjwqtvy86hc0000gn/T/pip-build-env-VhK8Xn/overlay/lib/python2.7/site-packages/setuptools/build_meta.py", line 130, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'])
        File "/private/var/folders/w8/ll36bzd537bgwgjwqtvy86hc0000gn/T/pip-build-env-VhK8Xn/overlay/lib/python2.7/site-packages/setuptools/build_meta.py", line 112, in _get_build_requires
          self.run_setup()
        File "/private/var/folders/w8/ll36bzd537bgwgjwqtvy86hc0000gn/T/pip-build-env-VhK8Xn/overlay/lib/python2.7/site-packages/setuptools/build_meta.py", line 211, in run_setup
          self).run_setup(setup_script=setup_script)
        File "/private/var/folders/w8/ll36bzd537bgwgjwqtvy86hc0000gn/T/pip-build-env-VhK8Xn/overlay/lib/python2.7/site-packages/setuptools/build_meta.py", line 126, in run_setup
          exec(compile(code, __file__, 'exec'), locals())
repos:
        File "setup.py", line 13
          def get_long_description() -> str:
                                     ^
      SyntaxError: invalid syntax

I was wondering if anyone has faced a similar issue and knows of a workaround.

Thanks!

Most helpful comment

Yes :)

All 3 comments

Black will never run with python 2.7 so I guess the only reasonable thing you can do is to install it into its own virtualenv

Okay, sorry. I wasn't clear on this. My end goal is to add a pre-commit hook to lint python2.7 code

repos:
-   repo: https://github.com/ambv/black
    rev: stable
    hooks:
    - id: black
      language_version: python3.6

If I am in a virtualenv running python3.6 with the following config, will black successfully lint python2.7 code?

Yes :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dusty-phillips picture dusty-phillips  路  3Comments

JelleZijlstra picture JelleZijlstra  路  3Comments

layoaster picture layoaster  路  3Comments

Curly-Mo picture Curly-Mo  路  3Comments

bhearsum picture bhearsum  路  3Comments