From 629df55544f138442c47e9d64b3d9bbc57b5b225 Mon Sep 17 00:00:00 2001 From: SilverTux Date: Thu, 6 Oct 2022 17:51:41 +0200 Subject: [PATCH 1/2] Fix too long floating point number parse problem - Also include @dougthor42's diff to test the fix see: [his diff](https://github.com/pre-commit/pre-commit-hooks/compare/main...dougthor42:high-precision-numbers) --- pre_commit_hooks/pretty_format_json.py | 4 ++-- testing/resources/high_precision_numbers.json | 3 +++ tests/pretty_format_json_test.py | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 testing/resources/high_precision_numbers.json diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index 627a11cc..420bd73b 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -1,7 +1,7 @@ from __future__ import annotations import argparse -import json +import simplejson as json import sys from difflib import unified_diff from typing import Mapping @@ -23,7 +23,7 @@ def pairs_first(pairs: Sequence[tuple[str, str]]) -> Mapping[str, str]: after.sort() return dict(before + after) json_pretty = json.dumps( - json.loads(contents, object_pairs_hook=pairs_first), + json.loads(contents, object_pairs_hook=pairs_first, use_decimal=True), indent=indent, ensure_ascii=ensure_ascii, ) diff --git a/testing/resources/high_precision_numbers.json b/testing/resources/high_precision_numbers.json new file mode 100644 index 00000000..740fef71 --- /dev/null +++ b/testing/resources/high_precision_numbers.json @@ -0,0 +1,3 @@ +{ + "foo": 4.4257052820783003 +} diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index 5ded724a..16a3328a 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -23,6 +23,8 @@ def test_parse_num_to_int(): ('unsorted_pretty_formatted_json.json', 1), ('non_ascii_pretty_formatted_json.json', 1), ('pretty_formatted_json.json', 0), + # numbers with high precision should not be modified. + ('high_precision_numbers.json', 0), ), ) def test_main(filename, expected_retval): From 2e029b708cd099163673b595a20791627277b363 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 6 Oct 2022 16:20:51 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pre_commit_hooks/pretty_format_json.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index 420bd73b..5c866e3b 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -1,12 +1,13 @@ from __future__ import annotations import argparse -import simplejson as json import sys from difflib import unified_diff from typing import Mapping from typing import Sequence +import simplejson as json + def _get_pretty_format( contents: str,