feat: add schema and validate-pyproject support
Description
This adds a script (Python 3.10+ required) to produce a JSONSchema. I've added it to the package, and provided a helper function / entrypoint to allow validate-pyproject to use it. You can try it out like this:
$ virtualenv .venv
$ py -m pip install -e . validate-pyproject[all]
$ validate-pyproject -v pyproject.toml
[INFO] black.schema.get_schema defines `tool.black` schema
[INFO] validate_pyproject.api.load_builtin_plugin defines `tool.distutils` schema
[INFO] validate_pyproject.api.load_builtin_plugin defines `tool.setuptools` schema
[WARNING] `blackd:patched_main [d]` - using extras for entry points is not recommended
Valid file: pyproject.toml
You can also adjust the tool.black section and see the validation errors. :)
I've also included a --schemastore option, which adjusts the output a little for SchemaStore. After this goes in, I'll make a PR to SchemaStore with the schema. The main difference is enable-unstable-feature is simplified to just be any string. If someone is getting the schema directly from black, then they get the exact enum of options, but SchemaStore is not versioned.
Closes #4160.
This goes along with #4178 and doesn't include all the interesting ways to set things that are technically currently supported, like using strings instead of bools and using _ instead of -.
Checklist - did you ...
- [ ] Add an entry in
CHANGES.mdif necessary? - [ ] Add / update tests if necessary?
- [ ] Add new / update outdated documentation?
I'll adjust those things after I see how much/little is needed here. I could add (re)generation of the schema file to CI, some tests using validate-pyproject, etc. Or I could remove parts too.
I also realize I forgot to see if this works on Python 3.8, as the importlib logic is different there.
@henryiii I fixed a few CI issues but the entry points check appears broken on 3.9 and below:
def test_schema_entrypoint() -> None:
if sys.version_info < (3, 10):
eps = importlib.metadata.entry_points()["validate_pyproject.tool_schema"]
> (black_ep,) = [ep for ep in eps if ep.name == "wheel"]
E ValueError: not enough values to unpack (expected 1, got 0)
diff-shades reporting "Opération was cancelled" is normal?
It's a known issue, unfortunately. This change is highly unlikely to affect Black's formatting, so I'll merge. Thank you!
Awesome, thanks!
This change is highly unlikely to affect Black's formatting
I would hope so. 😆
I'll make the SchemaStore contribution next & mention this PR.