-
-
Notifications
You must be signed in to change notification settings - Fork 590
[DRAFT] #708 - Make pretty formatter output prettier #712
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
[DRAFT] #708 - Make pretty formatter output prettier #712
Conversation
Unicode box characters, true JSON output, (slightly) more space between each error, etc.
…8-prettier-pretty
This comment has been minimized.
This comment has been minimized.
Awesome thanks! I will take a closer look at this (possibly after you get to the tests) but just to save you from one wrong turn -- the "use JSON" desire I had should only apply to the CLI, not to general formatting of the exception -- in other words, we can't change it within e.g. We have some options there. The least coupled is to just construct a custom way to format the errors within the CLI (using all the attributes present on a But yeah want to save you from that otherwise you'll find you have to change lots of tests (and then find out later that I didn't mean that :D). But yeah thanks for all you've done already! Seems like we're on the way here... |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Only use JSON formatting for exception messages when explicitly requested.
Cool. Yes, think you're getting to the same place I was describing -- I'd probably want it to just be a private helper function, so consider something like:
and then in the CLI you'd use |
This comment has been minimized.
This comment has been minimized.
Sounds reasonable!
Maybe this helps?:
(In theory you can replace |
As clarified in PR, not all exception output needs to use valid JSON. Add a message formatter method to the exception class and pass in a formatter lambda/function when needed. Updated tests with the new expected output format.
Codecov Report
@@ Coverage Diff @@
## master #712 +/- ##
==========================================
+ Coverage 96.02% 96.04% +0.02%
==========================================
Files 17 17
Lines 2664 2681 +17
Branches 310 310
==========================================
+ Hits 2558 2575 +17
Misses 87 87
Partials 19 19
Continue to review full report at Codecov.
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Hey! Sorry, I haven't really gotten a chance to look at the error you were hitting -- let me know though if you're still having difficulty -- replacing ljust with just the manual calculation sounds fine to me, but if it continues to be an issue I can have a look (maybe tomorrow) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Will have a look at this in a few hours I hope -- but are you sure this passes locally for you? It certainly fails here. From glancing at the diff, it seems like you probably want native strings (i.e. no Will have some more specific guidance after having a look. And thanks for the work so far it's much appreciated! |
This comment has been minimized.
This comment has been minimized.
These are not needed, as I expected. They may even contribute to the "buffer interface" errors.
From one version of Python to another, the "type" that appears in brackets may be of differing lengths. (E.g., `JSONDecodeError` in Python 3, but `ValueError` in Python 2.) That would make the length of the Unicode bar different under each version of Python. When these tests were corrected to work under Python 2, they wouldn't work under Python 3 and vice versa. Therefore, it's simpler to break the single assertions into two shorter ones that use very little of the bar characters.
This comment has been minimized.
This comment has been minimized.
The CI failures are python-hyper/hyperlink#133 which is being resolved one way or another (either upstream or if not quickly then I'll shove a fix in here somehow) |
sorry, to be clearer about what I mean there, since maybe that was too short -- that function doesn't use anything relevant to an instance, or to the class itself, so it should just be a global function, completely unattached to the class (and then you won't need neither |
This comment has been minimized.
This comment has been minimized.
Generally I'd just leave it right alongside |
As per code review discussion.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Resolved conflicts: * jsonschema/cli.py * jsonschema/exceptions.py * jsonschema/tests/test_cli.py
All CI checks except coverage passed. Automated coverage check isn't something I know much about, but I'd like to learn. According to the raw logs of the check, it looks like a configuration file for running the check is missing. Is that right? I saw a coverage report from closer to the time I opened this PR. There have been several changes to my code since then, so it's probably out of date. Is there any value to using that report? I'm unsure how to interpret the report anyway. 🙈 Edit: I see that although the comment from the coverage report is 10 days old, the report itself is current. I'll look up the coverage documentation and try to understand what the report is telling us. |
The failure there is codecov.io (which is the free service for hosting those reports) being flaky, likely nothing you did/changed. I just re-ran the job, it's unfortunately common for it to barf. Let's see if it passes this time. |
Yes, they ran this time. The only failure was to hit the 100% coverage target. Looks like there's an overall slight increase in coverage. Does this satisfy your requirements for merging, or would you like me to look into increasing coverage for code I added? |
Yeah don't mind the coverage being confused not sure why codecov.io has begun to do that as well recently but you're fine there -- will give this another review I hope later today! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Left a few minor comments but think we're almost there! Appreciated.
pinstance = pprint.pformat(self.instance, width=72) | ||
pschema = formatter(self.schema) | ||
|
||
pinstance = pformat(self.instance, width=72) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're only using the formatter for the schema, not the instance here, so there's going to be inconsistency in the output.
Can we add a test that covers the instance too and then fix this to make sure the formatter is used for both?
"""\ | ||
===[{type}]===({path})=== | ||
def _message_line(self, path, type, header=False): | ||
begin_char, end_char = self._MESSAGE_CORNER_CHARS if header \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor style: jsonschema follows PEP8 / generally avoids using backslashes for continuation. Can you switch these to use parentheses instead?
Though, I think this will be a lot clearer if we split it into _header_line
and _non_header_line
-- can you give that a shot? It should remove a bunch of the conditional behavior here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I usually don't like backslashes, either. I got in the habit of using it because my coworkers do like them. I'll change this code.
In case it helps, I added a pre-commit config for you/others so you should be able to fix some of the style comments here by just running pre-commit (which will e.g. restore the import order). |
* origin/master: Remove docformatter, it's too unwieldy. Minor doc formatting. Sigh, this appears to be a regex, not exact match, so it was ignoring everything. Update pre-commit hooks. And two last ones... Remove the rest of the notebooks.ai related resources. Remove demo links from README file Move the latest validator CLI test to where it must go now. Simplify the schemas in the tests for CLI validator detection. add test case Delete the failed test case Fix the bug of processing arguments[validator] in parse_args use correct format check globally renames message and function better handling for Python 3.6 fix formatting error skip tests on python 3.6 unskip tests Temporarily skip the tests that will be unskipped by python-jsonschema#722. Squashed 'json/' changes from 86f52b87..fce9e9b3 Validate IP addresses using the ipaddress module. Skip more tests for python-jsonschema#686. Squashed 'json/' changes from ea415537..86f52b87 improve date parsing Bump the version in the notebooks.io requirements. pre-commit setup. Trailing whitespace Kill Py2 in the sphinx role. isorted. Unbreak non-format-setuptools-extra-installed jsonschema. Run CI against all setuptools extras separately. Pick a format checker that has no external dep now that fqdn exists. regenerated the requirements.txt Modify the code based on review comments fix bug about hostname by import fqdn
Co-authored-by: Julian Berman <[email protected]>
What is the definition of the order of imports preferred for this project? I often use IntelliJ IDEA's reformatting feature. In addition to cleaning up lines of code, it reorganizes imports. Off the top of my head, I think the order is something like sections of core Python modules, third-party modules, and project modules, each section alphabetized. |
It's exactly that in fact :), just actually alphabetized (some tools, e.g. |
544f7c3d Merge pull request #712 from otto-ifak/main 9dad3ebe Add tests for enum with array of bool 589a0858 Merge pull request #706 from marksparkza/unevaluated-before-ref 64d5cab9 Merge pull request #710 from spacether/patch-1 418cdbd6 Removes idea folder e0a9e066 Updates all other tests to mention grapheme/graphemes 69136952 Update minLength.json 4a2c61e8 Test unevaluatedItems|Properties before $ref git-subtree-dir: json git-subtree-split: 544f7c3df93b69f84f587b345f2835c380e43226
REVERT: 83e866b4 Merge pull request python-jsonschema#763 from michaelmior/propertynames-const REVERT: c5a9703f Merge pull request python-jsonschema#760 from OptimumCode/rfc3490-label-separator REVERT: b4c09b65 Add tests for propertyNames with const/enum REVERT: 4fa572d8 Move tests for rfc3490#3.1 into a separate test case REVERT: ce9f68ca Add link to rfc and quote REVERT: ad94cacc Add test cases for other valid label separators in IDN hostnames REVERT: 39002ae7 Merge pull request python-jsonschema#762 from OptimumCode/rfc-html-link REVERT: c8780535 Correct section anchor for rfc URL template REVERT: 5f2ca7d6 Modify rfc url template to use html version REVERT: 9c5d99b6 Merge pull request python-jsonschema#761 from OptimumCode/annotation-script-rfc-support REVERT: 9563ce7b Correct rfc URL template - incorrect path pattern was used REVERT: 961bfad0 Correct spec kind extraction from defined key. Continue on unkown URL kind REVERT: e524505b Merge pull request python-jsonschema#759 from sirosen/hostname-format-reject-single-dot REVERT: 4a3efd18 Add negative tests for "." for hostname formats REVERT: 4ba013d5 Merge pull request python-jsonschema#747 from santhosh-tekuri/duration REVERT: aa500e80 Merge pull request python-jsonschema#749 from json-schema-org/gregsdennis/json-everything-update REVERT: eb8ce976 Merge pull request python-jsonschema#757 from ajevans99/main REVERT: dcdae5c0 Merge pull request python-jsonschema#758 from sirosen/hostname-format-check-empty-string REVERT: db21d21b Merge branch 'main' into hostname-format-check-empty-string REVERT: 3fd78f04 Merge pull request python-jsonschema#1 from ajevans99/swift-json-schema REVERT: 3cada3a9 Update README.md REVERT: 82a07749 Merge pull request python-jsonschema#753 from json-schema-org/ether/fix-draft-locations REVERT: a66d23d4 move draft-specific files to the dedicated dir for its draft REVERT: 8ef15501 Merge pull request python-jsonschema#751 from big-andy-coates/format_tests_under_format REVERT: fe1b1392 All format test cases should be under the `format` directory. REVERT: b1ee90f6 json-everything moved to an org REVERT: c00a3f94 test: duration format must start with P REVERT: 9fc880bf Merge pull request python-jsonschema#740 from notEthan/format-pattern-control-char REVERT: cbd48ea5 Simplify test of \a regex character to test directly against `pattern` schema REVERT: d6f1010a Merge pull request python-jsonschema#746 from json-schema-org/annotations REVERT: 4aec22c1 Revert the changes to additionalProperties.json. REVERT: 2dc10671 Move the workflow step title. REVERT: d9ce71ac May as well also show quotes in the annotation. REVERT: 1b719a84 Pick the line after the description when attaching spec annotations. REVERT: 08105151 Markdown is apparently not (yet?) supported in annotations. REVERT: 81645773 Tidy up the specification annotator a bit. REVERT: 38628b79 Make the spec URLs structure a bit easier for internal use. REVERT: 4ebbeaf4 Merge branch 'Era-cell/main' REVERT: e4bd7554 dumbness2 corrected REVERT: d8ade402 inside run REVERT: 57c7c869 changed install location REVERT: 11f8e511 Added installing command in workflow REVERT: f2766616 template library, url loads changes REVERT: c2badb12 Merge pull request python-jsonschema#734 from OptimumCode/idn-hostname-arabic-indic-mixed REVERT: dd9599a5 Merge branch 'main' of github.com:json-schema-org/JSON-Schema-Test-Suite REVERT: 5b393436 add pr dependencies action REVERT: 3a509007 Clear existin annotations on same PR REVERT: 23674123 Cases for rfc and iso written separately REVERT: 0b780b2c Corected yaml format REVERT: 2b1ffb74 Best practices followed with optimized code REVERT: e88a2da6 Works for all OS REVERT: 7b40efe4 Base path for neighbouring file? REVERT: 564e6957 Walking through all leaf files REVERT: 7b84fb44 Merge branch 'main' of https://github.com/Era-cell/JSON-Schema-Test-Suite REVERT: 891d0265 First workflow2 REVERT: 1c175195 regex correction REVERT: 96f7683a Final correction2 - file names beautufied REVERT: 5f050a07 Final correction1 REVERT: 77527b63 Stupidity corrected REVERT: eb8fd760 Branch name specified REVERT: 540a269b Log2 REVERT: f29d090a Wrong location sepcification REVERT: 582e12be logging logs check REVERT: df3bdecc path corrected REVERT: c6b937ca Reading all jsons and spec urls added REVERT: cbdd1755 change day2 REVERT: 54f3784a Merge pull request python-jsonschema#731 from MeastroZI/main REVERT: 79dc92f1 TOKEN REVERT: ce52852d Python file location changed REVERT: 3558c2c6 Fake add to tests REVERT: eecc7b7a Merge branch 'main' of https://github.com/Era-cell/JSON-Schema-Test-Suite REVERT: 810d148a First workflow2 REVERT: 4eac02c7 First workflow REVERT: ff29264c Merge pull request python-jsonschema#741 from harrel56/chore/tabs-to-spaces REVERT: 9f39cf73 use spaces instead of tabs REVERT: 2f3b5f7a Corrected replaced unevaluated with additoinalProperties REVERT: 40bcb8b3 Corrected replaced unevaluated with additoinalProperties REVERT: fa9224d7 Merge pull request python-jsonschema#732 from MeastroZI/main2 REVERT: 83bedd5c Changing descriptions REVERT: 49f73429 fixing tests REVERT: e6d6a081 adding more test cases REVERT: 7e6c9be6 changing descriptions REVERT: 959aca92 shifting test REVERT: 605d7d78 Update propertyDependencies.json : test must be tests REVERT: deb82824 test for dependentSchema and propertyDependencies with unevaluatedProperties and additionalProperties REVERT: ea485124 Merge branch 'json-schema-org:main' into main REVERT: 64a3e7b3 Merge pull request python-jsonschema#721 from json-schema-org/gregsdennis/dynamicref-skips-resources REVERT: b9f14e64 Fix $schema in new new test REVERT: 3d5048e8 Merge pull request python-jsonschema#733 from Era-cell/main REVERT: 4ae14268 Add valid first character to avoid Bidi rule violation REVERT: 2480edba Update additionalProperties.json formatting it REVERT: 6aa79c0b Update additionalProperties.json formatting it REVERT: 3e0139a5 Update tests/draft-next/additionalProperties.json REVERT: 616240b0 Update tests/draft-next/additionalProperties.json REVERT: c5f3e4ea Update tests/draft2020-12/propertyNames.json REVERT: 964efb8e propertyNames doesn't affect additionalProperties, tests exist already for unevaluatedProps REVERT: f08b884c Cases go under additional and unevaluated Properties REVERT: 99864ff6 added tests for propertyNames with additionalProperties/unevaluatedProperties, also with specification property REVERT: 3b5782b6 Update ref.json : changing $Ids REVERT: 546b3561 test for $ref with $recursiveAnchor REVERT: 57617f25 Merge pull request python-jsonschema#726 from Era-cell/main REVERT: 51fc69cd meta data and property names constraints added, additional Items: string REVERT: 9b169bed specification takes array of objects having section and quote REVERT: 1362a8cc Pattern for para corrected REVERT: 340116ec Schema of specification in much structured REVERT: 003ac021 Test-schema including sub-schema for scpecification REVERT: 50a20280 adding specification enhancement for additionalProperties REVERT: 604f5f99 Drop tests of `$id` and `$anchor` that just test values against meta-schema `pattern` for those properties REVERT: 9cd64ec9 come on man, save all the files REVERT: f494440e use unique $id in optional tests, too REVERT: 468453b0 use unique $id REVERT: 9ec6d17e fix copy/paste error REVERT: b284f423 add tests for $dynamicRef skipping over resources REVERT: bf0360f4 add $recursiveAnchor to 2019-09 meta-schemas REVERT: 0519d1f0 add $dynamicAnchor to meta-schemas REVERT: b41167c7 Merge pull request python-jsonschema#714 from json-schema-org/more-not REVERT: 4221a55a Add tests for not: {} schemas for all values. REVERT: c499d1d2 Merge pull request python-jsonschema#713 from spacether/patch-1 REVERT: 24a471bd Update README.md REVERT: 544f7c3d Merge pull request python-jsonschema#712 from otto-ifak/main REVERT: 9dad3ebe Add tests for enum with array of bool REVERT: 589a0858 Merge pull request python-jsonschema#706 from marksparkza/unevaluated-before-ref REVERT: 64d5cab9 Merge pull request python-jsonschema#710 from spacether/patch-1 REVERT: 418cdbd6 Removes idea folder REVERT: e0a9e066 Updates all other tests to mention grapheme/graphemes REVERT: 217bf81b Merge pull request python-jsonschema#701 from json-schema-org/ether/dynamicRef-boolean REVERT: 7a3d06d7 I remove a test that doesn't make sense. REVERT: e8bf453d Move tests with ids in non-schemas to optional REVERT: 69136952 Update minLength.json REVERT: d545be21 Fix duplidate identifiers in recently added tests REVERT: 4e9640c8 test when $dynamicRef references a boolean schema REVERT: 3dab98ca Merge pull request python-jsonschema#705 from json-schema-org/gregsdennis/remove-contains-objects-tests REVERT: 1d3aa495 remove more maxContains REVERT: 4a2c61e8 Test unevaluatedItems|Properties before $ref REVERT: ec553d76 contains no longer applies to objects REVERT: 0433a2bf Merge pull request python-jsonschema#704 from big-andy-coates/clarify-format-requirements REVERT: c685195f Merge pull request python-jsonschema#703 from big-andy-coates/link-to-creek-validator-comprison-site REVERT: a46174b0 Add more detail around test runner requirements for `format` tests REVERT: bb1de8a9 The site linked to is a data-driven functional and performance benchmark of JVM based validator implementations. REVERT: d38ddd54 Merge pull request python-jsonschema#696 from jdesrosiers/unevaluated-dynamicref REVERT: 5d0c05fa Fix copy/paste error REVERT: 95fe6ca2 Merge pull request python-jsonschema#694 from json-schema-org/heterogeneous-additionalItems REVERT: 9c88a0be Merge pull request python-jsonschema#697 from json-schema-org/gregsdennis/add-ref-into-known-nonapplicator REVERT: 49222046 Add unevaluted with dynamic ref tests to draft-next REVERT: 8ba1c90d Update unevaluted with dynamic ref to be more likely to catch errors REVERT: fea2cf19 add tests for 2019 and 2020 REVERT: 6695ca38 add optional tests for `$ref`ing into known non-applicator keywords REVERT: 2834c630 Add tests for unevaluated with dynamic reference REVERT: cda4281c Merge pull request python-jsonschema#695 from json-schema-org/ether/clean-up-subSchemas REVERT: 7b9f45c2 move subSchemas-defs.json to subSchemas.json REVERT: e41ec0ec remove unused definition files REVERT: 349c5a82 Merge pull request #692 from json-schema-org/ether/fix-subSchemas-refs REVERT: 451baca4 Merge pull request python-jsonschema#670 from marksparkza/invalid-output-test REVERT: b8da838a Add tests for heterogeneous arrays with additionalItems REVERT: 6d7a44b7 fix subschema locations and their $refs REVERT: a9a1e2e3 Merge pull request python-jsonschema#690 from skryukov/add-ipv4-mask-test REVERT: ba52c48a Merge pull request python-jsonschema#689 from skryukov/add-schema-keyword-to-required-tests REVERT: 69b53add Add a test case for ipv4 with netmask REVERT: d0c602a7 Add $schema keyword to required tests REVERT: 20f1f52c Merge pull request python-jsonschema#688 from spacether/feat_updates_python_exp_impl REVERT: b087b3ca Updates implmentation REVERT: 4ecd01f3 Merge pull request python-jsonschema#687 from swaeberle/check-single-label-idn-hostnames REVERT: 732e7275 test single label IDN hostnames REVERT: 202d5625 test: hostname format check fails on empty string REVERT: ea0b63c9 Remove invalid output tests git-subtree-dir: json git-subtree-split: ab3924a663d2f9e6de844f464d185a70d881b9ed
Fixes #708.
This makes the pretty output look better by making error header and footer lines equal lengths and to use Unicode characters in them. It also shows schema JSON in error messages as valid JSON, not Python data structure representations. (Mostly a difference in quote marks used for strings.)
For the error messages, I wanted to continue using the strings with
str.format()
. However, I couldn't find a clean way of using the left justification and padding that's available in the formatting micro-language. If the technique I used isn't satisfactory, I can try for another approach.To get the schema instance to display as true JSON, I thought encoding the schema back to JSON was the best approach. Using newline with the separator and sorting keys seems to give close to the same output format as
pprint.pformat()
. The difference is that it ends up being a little prettier, because there are always line breaks, but in my testing,pprint
only broke the lines when the schema was longer than 72 characters. I didn't test extensively, so I don't know how well thejson.dumps()
technique compares withpprint
for lines that longer than 72 characters and aren't easily broken.Please feel free to be critical with the code review. I'm very much open to updating my changes to fit with the style of the project.