mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-04 19:26:52 +00:00
Report duplicate keys in check_json
Raise ValueError and return 1 if json contains duplicate keys
This commit is contained in:
parent
11a2fdbab8
commit
fe37451719
3 changed files with 22 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue