Skip to content

Commit

Permalink
Add schema support to pyink.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 671669739
  • Loading branch information
AleksMat authored and copybara-github committed Sep 6, 2024
1 parent 9ec49c6 commit b5bd10b
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 4 deletions.
30 changes: 26 additions & 4 deletions patches/pyink.patch
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@
classifiers = [
"Development Status :: 5 - Production/Stable",
"Environment :: Console",
@@ -70,53 +42,35 @@ dependencies = [
@@ -70,53 +42,38 @@ dependencies = [
"platformdirs>=2",
"tomli>=1.1.0; python_version < '3.11'",
"typing_extensions>=4.0.1; python_version < '3.11'",
Expand All @@ -1113,10 +1113,11 @@
[project.scripts]
-black = "black:patched_main"
-blackd = "blackd:patched_main [d]"
-
-[project.entry-points."validate_pyproject.tool_schema"]
-black = "black.schema:get_schema"
+pyink = "pyink:patched_main"

[project.entry-points."validate_pyproject.tool_schema"]
-black = "black.schema:get_schema"
+pyink = "pyink.schema:get_schema"

[project.urls]
-Documentation = "https://black.readthedocs.io/"
Expand Down Expand Up @@ -1195,6 +1196,27 @@
-[[tool.mypy.overrides]]
-module = ["tests.data.*"]
-ignore_errors = true
--- a/resources/pyink.schema.json
+++ b/resources/pyink.schema.json
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
- "$id": "https://github.com/psf/black/blob/main/black/resources/black.schema.json",
- "$comment": "tool.black table in pyproject.toml",
+ "$id": "https://github.com/google/pyink/blob/pyink/src/pyink/resources/pyink.schema.json",
+ "$comment": "tool.pyink table in pyproject.toml",
"type": "object",
"additionalProperties": false,
"properties": {
@@ -12,7 +12,7 @@
"line-length": {
"type": "integer",
"description": "How many characters per line to allow.",
- "default": 88
+ "default": 80
},
"target-version": {
"type": "array",
--- a/strings.py
+++ b/strings.py
@@ -8,6 +8,7 @@ from functools import lru_cache
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ jupyter = [
[project.scripts]
pyink = "pyink:patched_main"

[project.entry-points."validate_pyproject.tool_schema"]
pyink = "pyink.schema:get_schema"

[project.urls]
Changelog = "https://github.com/google/pyink/blob/pyink/CHANGES.md"
Repository = "https://github.com/google/pyink"
Expand Down
Empty file added src/pyink/resources/__init__.py
Empty file.
154 changes: 154 additions & 0 deletions src/pyink/resources/pyink.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://github.com/google/pyink/blob/pyink/src/pyink/resources/pyink.schema.json",
"$comment": "tool.pyink table in pyproject.toml",
"type": "object",
"additionalProperties": false,
"properties": {
"code": {
"type": "string",
"description": "Format the code passed in as a string."
},
"line-length": {
"type": "integer",
"description": "How many characters per line to allow.",
"default": 80
},
"target-version": {
"type": "array",
"items": {
"enum": [
"py33",
"py34",
"py35",
"py36",
"py37",
"py38",
"py39",
"py310",
"py311",
"py312",
"py313"
]
},
"description": "Python versions that should be supported by Pyink's output. You should include all versions that your code supports. By default, Pyink will infer target versions from the project metadata in pyproject.toml. If this does not yield conclusive results, Pyink will use per-file auto-detection."
},
"pyi": {
"type": "boolean",
"description": "Format all input files like typing stubs regardless of file extension. This is useful when piping source on standard input.",
"default": false
},
"ipynb": {
"type": "boolean",
"description": "Format all input files like Jupyter Notebooks regardless of file extension. This is useful when piping source on standard input.",
"default": false
},
"python-cell-magics": {
"type": "array",
"items": {
"type": "string"
},
"description": "When processing Jupyter Notebooks, add the given magic to the list of known python-magics (capture, prun, pypy, python, python3, time, timeit). Useful for formatting cells with custom python magics."
},
"skip-source-first-line": {
"type": "boolean",
"description": "Skip the first line of the source code.",
"default": false
},
"skip-string-normalization": {
"type": "boolean",
"description": "Don't normalize string quotes or prefixes.",
"default": false
},
"skip-magic-trailing-comma": {
"type": "boolean",
"description": "Don't use trailing commas as a reason to split lines.",
"default": false
},
"preview": {
"type": "boolean",
"description": "Enable potentially disruptive style changes that may be added to Pyink's main functionality in the next major release.",
"default": false
},
"unstable": {
"type": "boolean",
"description": "Enable potentially disruptive style changes that have known bugs or are not currently expected to make it into the stable style Pyink's next major release. Implies --preview.",
"default": false
},
"enable-unstable-feature": {
"type": "array",
"items": {
"enum": [
"hex_codes_in_unicode_sequences",
"string_processing",
"hug_parens_with_braces_and_square_brackets",
"unify_docstring_detection",
"no_normalize_fmt_skip_whitespace",
"wrap_long_dict_values_in_parens",
"multiline_string_handling",
"typed_params_trailing_comma",
"is_simple_lookup_for_doublestar_expression",
"docstring_check_for_newline",
"remove_redundant_guard_parens",
"parens_for_long_if_clauses_in_case_block"
]
},
"description": "Enable specific features included in the `--unstable` style. Requires `--preview`. No compatibility guarantees are provided on the behavior or existence of any unstable features."
},
"check": {
"type": "boolean",
"description": "Don't write the files back, just return the status. Return code 0 means nothing would change. Return code 1 means some files would be reformatted. Return code 123 means there was an internal error.",
"default": false
},
"diff": {
"type": "boolean",
"description": "Don't write the files back, just output a diff to indicate what changes Pyink would've made. They are printed to stdout so capturing them is simple.",
"default": false
},
"color": {
"type": "boolean",
"description": "Show (or do not show) colored diff. Only applies when --diff is given.",
"default": false
},
"fast": {
"type": "boolean",
"description": "By default, Pyink performs an AST safety check after formatting your code. The --fast flag turns off this check and the --safe flag explicitly enables it. [default: --safe]",
"default": false
},
"required-version": {
"type": "string",
"description": "Require a specific version of Pyink to be running. This is useful for ensuring that all contributors to your project are using the same version, because different versions of Pyink may format code a little differently. This option can be set in a configuration file for consistent results across environments."
},
"exclude": {
"type": "string",
"description": "A regular expression that matches files and directories that should be excluded on recursive searches. An empty value means no paths are excluded. Use forward slashes for directories on all platforms (Windows, too). By default, Pyink also ignores all paths listed in .gitignore. Changing this value will override all default exclusions. [default: /(\\.direnv|\\.eggs|\\.git|\\.hg|\\.ipynb_checkpoints|\\.mypy_cache|\\.nox|\\.pytest_cache|\\.ruff_cache|\\.tox|\\.svn|\\.venv|\\.vscode|__pypackages__|_build|buck-out|build|dist|venv)/]"
},
"extend-exclude": {
"type": "string",
"description": "Like --exclude, but adds additional files and directories on top of the default values instead of overriding them."
},
"force-exclude": {
"type": "string",
"description": "Like --exclude, but files and directories matching this regex will be excluded even when they are passed explicitly as arguments. This is useful when invoking Pyink programmatically on changed files, such as in a pre-commit hook or editor plugin."
},
"include": {
"type": "string",
"description": "A regular expression that matches files and directories that should be included on recursive searches. An empty value means all files are included regardless of the name. Use forward slashes for directories on all platforms (Windows, too). Overrides all exclusions, including from .gitignore and command line options.",
"default": "(\\.pyi?|\\.ipynb)$"
},
"workers": {
"type": "integer",
"description": "When Pyink formats multiple files, it may use a process pool to speed up formatting. This option controls the number of parallel workers. This can also be specified via the PYINK_NUM_WORKERS environment variable. Defaults to the number of CPUs in the system."
},
"quiet": {
"type": "boolean",
"description": "Stop emitting all non-critical output. Error messages will still be emitted (which can silenced by 2>/dev/null).",
"default": false
},
"verbose": {
"type": "boolean",
"description": "Emit messages about files that were not changed or were ignored due to exclusion patterns. If Pyink is using a configuration file, a message detailing which one it is using will be emitted.",
"default": false
}
}
}
17 changes: 17 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import importlib.metadata
import sys


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 == "pyink"]
else:
(black_ep,) = importlib.metadata.entry_points(
group="validate_pyproject.tool_schema", name="pyink"
)

black_fn = black_ep.load()
schema = black_fn()
assert schema == black_fn("pyink")
assert schema["properties"]["line-length"]["type"] == "integer"

0 comments on commit b5bd10b

Please sign in to comment.