Black: setting line-length in pyproject.toml prevents Black from running in vim

Created on 14 Jun 2020  Â·  6Comments  Â·  Source: psf/black

Describe the bug

If line-length is set in pyproject.toml, running black in Vim fails and throws the following error:

'<=' not supported between instances of 'int' and 'str'

To Reproduce

The minimum example that reproduces the error for me:

├── pyproject.toml
└── test.py

The contents of pyproject.toml follow the same syntax as is provided by pyproject.toml in the black repo

[tool.black]
line-length = 79

The contents of test.py:

foo = 1

Running black in Vim on test.py throws the error:

'<=' not supported between instances of 'int' and 'str'

I can successfully run black on test.py outside of vim:

~/.vim/black/bin/black test.py

or

black test.py

If I comment out line-length in pyproject.toml, the error we see when calling black in Vim goes away:

[tool.black]
#line-length = 79

The problem seems to be how line-length is parsed and cast by black.

Expected behavior
black runs without errors, and utilizes the line-length value specified in pyproject.toml.

Environment (please complete the following information):

  • Version: 19.10b0
  • OS and Python version: macOS 10.15.5 and Python 3.8.3; also reproduced on Ubuntu 20.04 and Python 3.8.3.

Does this bug also happen on master?

To answer this, you have two options:

  1. Use the online formatter at https://black.now.sh/?version=master, which will use the latest master branch.

n/a

  1. 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 -m unittest; and

    • run black like you did last time.

I get errors running python -m unittest but unsure related to this bug:

$ python -m unittest                                                                                                                                   
E....[2020-06-14 14:22:15,341] DEBUG: Using selector: EpollSelector (selector_events.py:59)
.[2020-06-14 14:22:15,342] DEBUG: Using selector: EpollSelector (selector_events.py:59)
.[2020-06-14 14:22:15,680] DEBUG: Using selector: EpollSelector (selector_events.py:59)
.[2020-06-14 14:22:15,682] DEBUG: Using selector: EpollSelector (selector_events.py:59)
[2020-06-14 14:22:15,683] INFO: 16 projects to run Black over (lib.py:311)
[2020-06-14 14:22:15,683] DEBUG: Using 2 parallel workers to run Black (lib.py:316)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on aioexabgp (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on attrs (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on bandersnatch (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on channels (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on django (lib.py:247)
[2020-06-14 14:22:15,684] INFO: Skipping django as it's disabled via config (lib.py:254)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on flake8-bugbear (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on hypothesis (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on pandas (lib.py:247)
[2020-06-14 14:22:15,684] INFO: Skipping pandas as it's disabled via config (lib.py:254)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on poetry (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on ptr (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on pyramid (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on pytest (lib.py:247)
[2020-06-14 14:22:15,684] INFO: Skipping pytest as it's disabled via config (lib.py:254)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on sqlalchemy (lib.py:247)
[2020-06-14 14:22:15,685] DEBUG: worker 0 working on tox (lib.py:247)
[2020-06-14 14:22:15,685] DEBUG: worker 0 working on virtualenv (lib.py:247)
[2020-06-14 14:22:15,685] DEBUG: worker 0 working on warehouse (lib.py:247)
[2020-06-14 14:22:15,685] DEBUG: project_runner 0 exiting (lib.py:245)
[2020-06-14 14:22:15,685] DEBUG: project_runner 1 exiting (lib.py:245)
[2020-06-14 14:22:15,685] INFO: Analyzing results (lib.py:327)
.
======================================================================
ERROR: tests.test_black (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.test_black
Traceback (most recent call last):
  File "/home/username/miniconda3/envs/ml4sts/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/home/username/miniconda3/envs/ml4sts/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/home/erik/black/tests/test_black.py", line 1766, in <module>
    class BlackDTestCase(AioHTTPTestCase):
NameError: name 'AioHTTPTestCase' is not defined


----------------------------------------------------------------------
Ran 9 tests in 0.348s

FAILED (errors=1)
bug

Most helpful comment

I can fix this, I introduced this regression trying to fix #1458.

All 6 comments

I can fix this, I introduced this regression trying to fix #1458.

@ichard26 Is this at all related to https://github.com/psf/black/pull/1486 ?

@cooperlees Sounds suspiciously similar, and kudos to @keith's excellent choice of line-length, although 1) my issue was in context of Vim 8.1, not neovim, and 2) unsure if the exception he refers to is the same as what we're seeing here.

Hello, yes the PR above is similar and probably could fix the problems, but
I think it will be better if I fix the source of the issue than putting a
workaround atop of a broken function like the PR above.

On Mon, Jun 15, 2020, 10:25 AM Erik Reinertsen notifications@github.com
wrote:

@cooperlees https://github.com/cooperlees Sounds suspiciously similar,
and kudos to @keith https://github.com/keith's excellent choice of
line-length, although 1) my issue was in context of Vim 8.1, not neovim,
and 2) unsure if the exception he refers to is the same as what we're
seeing here.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/psf/black/issues/1496#issuecomment-644167223, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/APHZN7NIWMGIU6HHRSHHYI3RWYVMDANCNFSM4N5R7FGQ
.

Agree, but should we merge the workaround for now and have your upcoming PR remove it if no longer needed?

If I don't get a PR ready in 40 minutes, go ahead on merge the workaround. You know what I was trying to do yesterday with pipenv 😂

Was this page helpful?
0 / 5 - 0 ratings

Related issues

spapanik picture spapanik  Â·  23Comments

jonadaly picture jonadaly  Â·  23Comments

underyx picture underyx  Â·  22Comments

stefanoborini picture stefanoborini  Â·  54Comments

rouge8 picture rouge8  Â·  20Comments