Skip to content

pre-commit integration v2#857

Closed
AleksaC wants to merge 5 commits intopython-jsonschema:mainfrom
AleksaC:pre-commit-integration-v2
Closed

pre-commit integration v2#857
AleksaC wants to merge 5 commits intopython-jsonschema:mainfrom
AleksaC:pre-commit-integration-v2

Conversation

@AleksaC
Copy link
Copy Markdown
Contributor

@AleksaC AleksaC commented Oct 12, 2021

Here's an improved version of pre-commit integration. I think it's in accordance with what @janosh and @asottile proposed in #765 at least in terms of interface. I made a test repo with pre-commit github action where you can check how the hook works on a simple test example. Any feedback is welcome.

@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 12, 2021

Codecov Report

Merging #857 (851b4fd) into main (0878727) will increase coverage by 0.03%.
The diff coverage is 98.77%.

❗ Current head 851b4fd differs from pull request most recent head a03e77f. Consider uploading reports for the commit a03e77f to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main     #857      +/-   ##
==========================================
+ Coverage   98.25%   98.29%   +0.03%     
==========================================
  Files          20       22       +2     
  Lines        3152     3519     +367     
  Branches      423      471      +48     
==========================================
+ Hits         3097     3459     +362     
- Misses         44       47       +3     
- Partials       11       13       +2     
Impacted Files Coverage Δ
jsonschema/_pre_commit_cli.py 93.47% <93.47%> (ø)
jsonschema/tests/test_pre_commit_cli.py 100.00% <100.00%> (ø)
jsonschema/validators.py 97.60% <0.00%> (+0.36%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0878727...a03e77f. Read the comment docs.

@janosh
Copy link
Copy Markdown

janosh commented Oct 13, 2021

Great work!

Is it necessary to release this hook with a new id (jsonschema-v2)? Perhaps its release can coincide with a major version bump of jsonschema so users know to expect breaking changes? Of course if none is planned in the foreseeable future, this is impractical.

Comment thread jsonschema/_cli_pre-commit.py Outdated

def main(argv=None):
wrapper_parser = argparse.ArgumentParser()
wrapper_parser.add_argument("--schema", required=True, type=str)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

type=str is the default.

Comment thread jsonschema/_cli_pre-commit.py Outdated
for file in parsed_args.files:
args.extend(["-i", file])

argv = argv or sys.argv[1:]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is this line necessary?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah in case argv is None which is the default value, I guess I could use sys.argv[1:] as the default instead

Comment thread jsonschema/_cli_pre-commit.py Outdated
argv = argv or sys.argv[1:]

for arg in argv:
if arg != "--schema" and arg != parsed_args.schema and arg not in parsed_args.files:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you leave a comment why we need to check both arg != "--schema" and arg != parsed_args.schema?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The goal here is to filter out all arguments other than jsonschema cli options. So when we have --schema schema.json argv will contain both --schema and schema.json (parsed_args.schema)

Copy link
Copy Markdown
Contributor

@asottile asottile left a comment

Choose a reason for hiding this comment

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

the implementation seems very fragile -- I would write a fresh argument parser that does precisely what you want instead of trying to pick apart the other one.

the arg filtering for --schema is also very fragile as it doesn't handle prefix matching or --schema=...

Comment thread .pre-commit-hooks.yaml Outdated
@@ -1,6 +1,6 @@
- id: jsonschema
- id: jsonschema-v2
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the old one should be left for compatibility at least for some time

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's call the new one "validate", in case we some day have some hook that does something different (and also since I hate names with versions in them).

@AleksaC
Copy link
Copy Markdown
Contributor Author

AleksaC commented Oct 13, 2021

Thanks for the feedback @asottile.

I considered writing a fresh parser and the reason I didn't is because I didn't want to cause additional maintenance burden or make big changes to the existing one.

Writing a fresh parser would mean that I'd need to duplicate a decent portion of the original parser. In addition to that run and parse_args functions expect arguments as they are parsed by the original parser so I would need to either transform the args to match the format which would be fragile in a similar way the current implementation is or to once again copy the functions and make small modifications to them. Having two parsers that are basically the same would mean that changes to the cli would need to be made in two places.

The other option would be to rewrite a portion of the original parser so that it could be reused for the new one but reusing arguments in argparse isn't clean and I wanted to avoid changing the existing code.

I didn't like the current implementation either, but I couldn't think of a way it breaks other than major change happening to the original parser like changing positional arguments or renaming instances option. There are two other assumptions about argparse itself, which from my experience with argparse and brief glance at its source code seem to be true. First assumption is that the first action is help. Second assumption is that actions are stateless meaning that the previous invocations won't mess with the later invocations.

When it comes to filtering other than the case I missed with schema there is one case where it can break - if an argument to some other option matches schema and or one of the files in which case it would be filtered out. The only such option currently is --instances so I probably needed to forbid its usage by throwing an exception if it is provided. I can't think of an option that may be added in the future that would also suffer from this problem.

Copy link
Copy Markdown
Member

@Julian Julian left a comment

Choose a reason for hiding this comment

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

I'll have to read more closely to form an opinion on shoehorning into the command line interface or not -- my intuition though is that given how simple this hook is likely to be, that it's easier not to shoehorn it, and just to directly implement the interface needed for nice pre-commit usage on top of the normal jsonschema APIs (that the other CLI uses).

This CLI (for pre-commit) wouldn't be directly public anyhow, it'd just be used via pre-commit.

But yeah that's just my first intuition, so I could be wrong after looking more carefully.

And thanks again for doing this!

Comment thread .pre-commit-hooks.yaml Outdated
@@ -1,6 +1,6 @@
- id: jsonschema
- id: jsonschema-v2
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's call the new one "validate", in case we some day have some hook that does something different (and also since I hate names with versions in them).

Comment thread .pre-commit-hooks.yaml Outdated
- id: jsonschema-v2
name: jsonschema
description: json schema validation
entry: python -m jsonschema._cli_pre-commit
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we name the module a valid identifier? _cli_pre_commit I guess is fine.

Comment thread jsonschema/_cli_pre-commit.py Outdated
@@ -0,0 +1,36 @@
import argparse
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Once the interface is agreed upon I definitely wouldn't mind if this file ran in CI, even if not all the code paths were covered, just to ensure the hook doesn't stop working due to a change.

@AleksaC AleksaC force-pushed the pre-commit-integration-v2 branch from 0f98dc6 to 4856e4d Compare October 18, 2021 01:01
@AleksaC
Copy link
Copy Markdown
Contributor Author

AleksaC commented Oct 18, 2021

Here's a fresh parser implementation of the pre-commit cli. I brought back the old hook and renamed the new one not to contain version in it. I also adapted the tests for original cli for the pre-commit cli. I added pycache files to .gitignore since I commited them accidentally (can revert this if you prefer it in a separate pr).

Comment thread jsonschema/tests/test_pre_commit_cli.py Outdated
@janosh
Copy link
Copy Markdown

janosh commented Dec 16, 2021

Feels like a lot of work went into this already. Anything I can do help get this in?

@Julian
Copy link
Copy Markdown
Member

Julian commented Dec 16, 2021

So sorry, somehow I didn't realize this was waiting on me! Will have another look, though will likely take a few days to clear some other things off.

AleksaC and others added 3 commits December 16, 2021 21:22
@AleksaC AleksaC force-pushed the pre-commit-integration-v2 branch from c3dba7f to 0f3c228 Compare December 16, 2021 20:23
@ssbarnea
Copy link
Copy Markdown
Contributor

ssbarnea commented Apr 8, 2022

Any chance to revive this PR?

@sirosen
Copy link
Copy Markdown
Member

sirosen commented May 24, 2022

Per #910, we're going to start treating check-jsonschema as the official CLI for jsonschema. One of the responsibilities of that project is providing a set of pre-commit hooks. The generic check-jsonschema hook is comparable to the behavior of this PR.

I would recommend closing this. I know that people have worked hard on it -- and I don't feel great about recommending that the work be discarded -- but I believe that there's a better future for pre-commit hooks in a separate project from jsonschema itself. If there are valuable behaviors here which are missing from check-jsonschema, I'm happy to take PRs, feature requests, bug reports, or constructive criticism!

@Julian
Copy link
Copy Markdown
Member

Julian commented May 24, 2022

I would recommend closing this. I know that people have worked hard on it -- and I don't feel great about recommending that the work be discarded -- but I believe that there's a better future for pre-commit hooks in a separate project from jsonschema itself.

It's my apology to make, I'm sorry all for not getting to this :( some other (non-jsonschema things) have been sapping even more time away.

But yes agreed, we're going to move this functionality there, so please give feedback there.

@Julian Julian closed this May 24, 2022
Julian added a commit that referenced this pull request May 3, 2026
c7257e92 refOfUnknownKeyword.json rm unused $id (#890)
bf54c9bd support negative multiples (#888)
e819f329 Fix broken test in v1/ref.json from PR #873
29965c01 Merge pull request #873 from jdesrosiers/fix-v1-meta-schema-ref
3b08f79b test: add some missing edge cases to json-pointer format tests (#877)
a1de34ae test(format): allow consecutive hyphens in hostname (RFC1123) (#874)
aac8a1bd chore: remove duplicate idn-email test and fix ambiguous test descriptions (#881)
78048ead format: add RFC 3339 unknown local offset (-00:00) test for time (#878)
ee3fc3d8 test(draft2020-12): add boundary coverage for maxContains = 0 (#857)
54ed4d1f Add additional URI edge cases and include them in v1 (#850)
87f09d65 Add missing email format edge cases across drafts (#849)
f2720d32 more tests for the duration format: missing units (#875)
e391238a Draft-06/7 don't allow empty enums (#876)
c7fc5095 Add tests for empty enum validation (#870)
20f4897c docs: clarify intent of format-assertion vocabulary test cases (#854)
eb018552 Add annotation tests for $dynamicRef and $dynamicAnchor (#862)
0a1dc5e9 fix: remove duplicate allOf and maxItems entries in draft2020-12 KNOWN set (#865)
06e3102d docs: fix typos and wording in suite documentation (#867)
031e9e9a test(duration): expand RFC 3339 grammar coverage for duration format (#868)
3181cefa Fix issues with v1 tests
06481b14 test: add unicode pattern and patternProperties tests for draft2020-12 (#837)
601aa708 Add exhaustive ABNF-driven tests with RFC 2673 compliance for ipv4 format (#840)
583d7c62 Merge pull request #851 from Anshikakalpana/fix/maximum-test-description-mismatch
61bfe27a fix: apply description fix to v1 maximum test
516d71f7 fix: align test description with actual valid field in maximum.json across drafts
4253477c Merge pull request #836 from VIDIT45AGARWAL/date-time
9e787e38 Add tests for out-of-range time components in date-time format
bce6a47c Merge pull request #793 from 414owen/os/fix-unevaluatedproperties-test-case
a17eb1f6 Merge pull request #833 from JulesGosnell/add-m3-validator
648811b2 Add M3 JSON Schema validator to Who Uses section
32fa6e28 Merge pull request #827 from Shristibot/required-null
086bf9a1 Revert unintended .gitignore change
909b58d7 Remove unrelated files from PR
c957e914 Add boolean and null cases to draft-04 , draft-06 and v1
0650027c Add boolean case to required non-object validation
d8d15620 Reset README to upstream to avoid unrelated changes
f6523449 Add required + null test case for issue #821
75995a1c Merge pull request #818 from fperrad/lua-schema
7e6b3a15 Add lua-schema in README.md
8b826d6b Merge pull request #816 from kpavlov/kotlinx-schema-link
b3ae21c2 Update README.md
646f11da Merge pull request #812 from Shristibot/clarification-on-test-suite
ba3f9505 Update README.md
3fc54179 Merge pull request #808 from json-schema-org/ether/more-fix-remotes
3550ca73 clarify optional tests and document integer example
fac4f6f6 Clarify that the test-suite is not  astyle guide
ece5bd10 Merge pull request #804 from Shristibot/add-if-then-else-tests
525adc90 move aside remotes that are not valid in earlier drafts
bbe22f88 remove redundant remotes that also exist in draft-specific directories
ea0baf8d these files are no longer used, and contain an unresolveable $ref on earlier drafts
bef1e2ed Remove redundant then/else false test cases
910af732 Merge pull request #803 from json-schema-org/gregsdennis/proposals
23ff2750 Fix incorrect test condition, simplify test, and copy tests to all drafts as requested
ccfb91af Fix description style and add false-handling tests for issue #767
bc1763ed Fix description formatting in false-handling tests
bd13fcf7 Add if/then/else test cases for issue #767
dc659514 added documentation about folder in root readme and folder readme
c4a99e00 move propertyDependencies tests to proposals folder
d69537ac Merge pull request #802 from json-schema-org/ether/draft4-boolean-schema
79ee846d Merge pull request #801 from json-schema-org/ether/missing-not-tests
2da8f9a6 remove unused remotes files
15cc161f add missing not tests from #714
47ff61c2 Merge pull request #799 from frawa/frawa-patch-1
4a640206 Merge pull request #800 from frawa/frawa-patch-2
fdabd3f9 Merge pull request #795 from anmolbhadoriya5849/fix/issue-768-additionalItems-case
cd02dece Add Unison section to README
4da52d58 Remove 'typed-json' link from README
232f0795 Revert removal of additionalProperties applicator tests in v1
b4032b96 Revert removal of additionalProperties applicator tests in draft2020-12
df833be4 Remove test case for additionalProperties behavior
0d54c828 Remove test for additionalProperties behavior
756eebd6 Remove valid case for additionalItems validation
a3feff79 Remove valid case for additionalItems validation
8ba96359 Remove test for additionalItems applicators
f3488882 Remove valid test case for additionalItems
da4a5684 Remove valid case for additionalItems validation
a247442b Merge pull request #794 from json-schema-org/ether/dynamic-scopes-subschemas
19810ec1 Add a test for respecting dynamic scopes while avoiding the root of each schema
e0e431f8 Fix unevaluatedProperties & additionalProperties test case
8da0a7bc Merge pull request #792 from jdesrosiers/more-v1-transistion
94c0b0e1 Fix more "draft/next" occurances that we missed
8fd4fcec Merge pull request #786 from jdesrosiers/a-label-tests
be58fa98 Merge pull request #791 from json-schema-org/ether/fix-draft-next-removal
acaece38 stop using the term "draft" to mean "version", and fix the remaining mentions of draft-next (now v1)
980e1052 Merge pull request #790 from dylankerr-bis/add-test-sibling-nested-ref-id
b5037ed7 Add test for sibling $ref and $id in nested schema to v1 suite
b8bb8585 Add more comprehensive tessts for A-labels in the "hostname" format
9b8ef536 Cleanup hostname tests (remove dups, fix descriptions, reorder)
4cf55996 Merge pull request #776 from davishmcclurg/idn-hostname-separators
a930db43 Shorten test descriptions to appease ci
aab08752 Updates form PR feedback
081a16a7 Test IDN label separators separate labels
658c8cf9 Test IDN label separator in `hostname` format
38c04b83 Test label separator position in hostname formats
cabbd65c Merge pull request #735 from zaplapl/main
ed74ee74 Add unicode tests to all the dialects
fd0df6cf Update unicode tests per PR feedback
60098220 base test implementation for const equality for strings based on evalutaion by individual codepoint
ef78a0eb Merge pull request #683 from mwadams/main
db365735 Added tests to encoded refs to unknown keywords.
59944b7e Add test for sibling $ref and $id in nested schema to 2019 suite
2196a531 Add test for sibling $ref and $id in nested schema to 2020 suite
e99b24c9 Merge pull request #788 from jdesrosiers/next-to-v1
7eac79be Bring tests up-to-date with format changes
ae27be13 Bring dynamic reference tests up to date with spec changes
79dc2fd3 Unknown optional vocabs can't be ignored anymore
51f84640 Change draft-next to v1
f4e41b0c Merge pull request #785 from jdesrosiers/uneval-nested-conflicts
60a2633f Merge pull request #778 from koplas/extend-uri-validation-tests
bf65ef20 Copy uri tests to other drafts
02a0d89f Merge pull request #743 from Vinit-Pandit/MeastroZI-patch-1
c1545e6c Update unevaluatedProperties.json
7e970016 Merge pull request #764 from Dragonsangel/date_time_validation
8970b1ca Add tests for evaluated property/item nesting conflicts
677a9399 Merge pull request #784 from sangamon/781-date-time-format-extended-year
9672a5b6 Merge pull request #783 from sangamon/780-uuid-format-shifted-dashes
28e49b14 add test for invalid "shifted" dashes in uuid format (#780)
aecf038c add test for invalid extended year in date-time format (#781)
15e4505b Merge pull request #765 from JDepooter/unevaluated_items_with_mincontains_0
b8611c40 Extend URI and URI-reference tests
48461fc3 Merge pull request #775 from jviotti/annotations-content-id
9f7dce32 Merge pull request #766 from json-schema-org/ether/bare-email
a1e077d4 ensure we do not parse email strings permissively
72c670b0 Delete identifiers in Content annotation tests
675186ba Take into account `$id` for annotation schema location assertions
100e9823 Add tests for unevaluatedItems with minContains = 0
0be5549b Added tests to ensure date and time formats don't accept date-time formats

git-subtree-dir: json
git-subtree-split: c7257e92580678a086f0b9243a1903ed88bd27f7
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.

7 participants