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

feat: add schema and validate-pyproject support #4181

Merged
merged 8 commits into from Jan 29, 2024

Conversation

henryiii
Copy link
Contributor

@henryiii henryiii commented Jan 27, 2024

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.md if 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.

Copy link
Collaborator

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Some nits and questions below.

scripts/generate_schema.py Show resolved Hide resolved
scripts/generate_schema.py Outdated Show resolved Hide resolved
},
"ipynb": {
"type": "boolean",
"description": "Format all input files like Jupyter Notebooks regardless of file extension.This is useful when piping source on standard input.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing the missing space in #4182

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased and picked it up (will push soon).

src/black/schema.py Show resolved Hide resolved
src/black/schema.py Outdated Show resolved Hide resolved
tox.ini Show resolved Hide resolved
src/black/schema.py Outdated Show resolved Hide resolved
@henryiii henryiii force-pushed the henryiii/feat/schema branch 2 times, most recently from 4b20edf to 73c6cd3 Compare January 28, 2024 04:04
@henryiii henryiii marked this pull request as ready for review January 28, 2024 04:07
@JelleZijlstra
Copy link
Collaborator

@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)

henryiii and others added 7 commits January 28, 2024 08:43
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
@henryiii
Copy link
Contributor Author

diff-shades reporting "Opération was cancelled" is normal?

@JelleZijlstra
Copy link
Collaborator

It's a known issue, unfortunately. This change is highly unlikely to affect Black's formatting, so I'll merge. Thank you!

@JelleZijlstra JelleZijlstra merged commit 2bc5ce8 into psf:main Jan 29, 2024
45 of 46 checks passed
@henryiii henryiii deleted the henryiii/feat/schema branch January 29, 2024 16:37
@henryiii
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can we add a json schema to complete pyproject.toml's [tool.black]?
2 participants