Generation of JSON schemas for parsers - #961
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #961 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 27 28 +1
Lines 8784 9162 +378
==========================================
+ Hits 8784 9162 +378 ☔ View full report in Codecov by Harness. |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



What does this PR do?
Adds a new
jsonschemacompletion type, so that a parser can produce a JSON Schema (draft 2020-12) describing the config files it accepts, either programmatically withparser.get_completion_script("jsonschema")or from the command line with--print_completion=jsonschema.Fixes #618
The schema is derived from the same information as the
--helpoutput, and includes:PositiveIntandEmail. For plain argparse actions, which have no type hint, what the action gives, e.g. boolean forstore_true, integer forcount, the possible values forstore_constand an array forappend.argparse.SUPPRESSdefaults and unset values.helpgiven toadd_argument.class_pathand aninit_argsobject for that specific class.subcommandkey.Types used in more than one place go once into
$defsand are referenced with$ref, which also makes recursive types work.For subclass types the schema is stricter than the parser: a string is accepted, since it can be a class path or a path to a sub-config file, but an object is only accepted for the known subclasses. An object that accepts any
class_pathwould keep tools from suggesting the known subclasses and from pointing out a class path that has a typo or is not the accepted import path, in which case theinit_argswould go undescribed. Only when a type has no known subclass is anyclass_pathaccepted, without describing itsinit_args.To get editor validation and autocompletion, configs can point to a schema with a
$schemakey. The key is accepted in any config that a parser loads, sub-config files included, and is removed before parsing, so it never reaches the parsed namespace. It is not removed when the parser has an argument with that name, nor from the values of dict types, since there it is data instead of a pointer to a schema.Other changes:
--print_completionis now added whenever the setting is enabled, no longer requiringshtabto be installed, and its accepted values arejsonschemaplus oneshtab-*value per supported shell whenshtabis available.get_completion_scriptfor theshtab-*types. Generating a JSON schema does not modify the parser.argcompleteruntime completion documented separately.This feature is experimental, so the details of the generated schema might change in non-major releases.
Before submitting