mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2025-08-14 09:27:21 +08:00
Report duplicate keys in check_json
Raise ValueError and return 1 if json contains duplicate keys
This commit is contained in:

committed by
Anthony Sottile

parent
11a2fdbab8
commit
fe37451719
@ -1,7 +1,23 @@
|
||||
import argparse
|
||||
import json
|
||||
from typing import Any
|
||||
from typing import Dict
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
from typing import Sequence
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
def raise_duplicate_keys(
|
||||
ordered_pairs: List[Tuple[str, Any]],
|
||||
) -> Dict[str, Any]:
|
||||
d = {}
|
||||
for key, val in ordered_pairs:
|
||||
if key in d:
|
||||
raise ValueError(f'Duplicate key: {key}')
|
||||
else:
|
||||
d[key] = val
|
||||
return d
|
||||
|
||||
|
||||
def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
@ -13,7 +29,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
for filename in args.filenames:
|
||||
with open(filename, 'rb') as f:
|
||||
try:
|
||||
json.load(f)
|
||||
json.load(f, object_pairs_hook=raise_duplicate_keys)
|
||||
except ValueError as exc:
|
||||
print(f'{filename}: Failed to json decode ({exc})')
|
||||
retval = 1
|
||||
|
Reference in New Issue
Block a user