Black: Error message for invalid target-version config in pyproject.toml is not obvious

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

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

Operating system: Whatever
Python version: Whatever
Black version: 19.x
Does also happen on master: Yes

I was migrating from 18.x and looking into target-version for the first time. Since --target-version takes a string, I intuitively went for this

[tool.black]
target-version = "py37"

and was greeted by a cryptic error:

Error: Invalid value for "-t" / "--target-version": invalid choice: p. (choose from py27, py33, py34, py35, py36, py37, py38)

I realised that I should provide a list instead after staring at it for a while (in retrospect I should read the docs first), but the message could be a lot friendlier IMO.

I can think of two options:

  1. Explicitly check for types for each config, so the error message can tell me directly I鈥檓 providing the wrong type.
  2. Automatically coerce the string to a single-item list.
command line

Most helpful comment

Note that

  1. The fixed version should look something like
[tool.black]
target-version = ["py37"]
  1. If pyproject.toml is in your version control and you use pre-commit, pre-commit will stash the changes to pyproject.toml, rolling back to the bad version, and then black will continue to fail with an error. Use git commit --no-verify to bypass pre-commit without disabling it.

All 3 comments

It's also a bit confusing that in the pyproject file it's a singular target-version vs. target-versions.

I'm guessing that on the command line it's happy to take something like black --target-version py36 --target-version py37 ..., and Click or whatever is configured to jam them all into a set, which might only have a single item.

Tack an "s" on the key that's looked up in the settings file, or does that asymmetry hurt even more? :stuck_out_tongue:

Note that

  1. The fixed version should look something like
[tool.black]
target-version = ["py37"]
  1. If pyproject.toml is in your version control and you use pre-commit, pre-commit will stash the changes to pyproject.toml, rolling back to the bad version, and then black will continue to fail with an error. Use git commit --no-verify to bypass pre-commit without disabling it.

The problem here is that on the command line, Click handles turning your one argument into a list automatically, but Black handles the pyproject.toml parsing itself, meaning that it expects the value to be a list already. I guess a reasonable solution would be to convert a string with a valid target-version to a list before handing it off back to the main function.

EDIT: This should just be closed since Black will complain if target-version isn't a list because of PR https://github.com/psf/black/pull/1284

Was this page helpful?
0 / 5 - 0 ratings

Related issues

J0 picture J0  路  3Comments

JelleZijlstra picture JelleZijlstra  路  3Comments

decibyte picture decibyte  路  3Comments

dimaqq picture dimaqq  路  3Comments

dusty-phillips picture dusty-phillips  路  3Comments