Skip to content

Commit

Permalink
Fallback to stdlib json if integer exceeds 64-bit range (#18148)
Browse files Browse the repository at this point in the history
Fixes the error below:
```python
 File "mypy/main.py", line 102, in main
  File "mypy/main.py", line 186, in run_build
  File "mypy/build.py", line 194, in build
  File "mypy/build.py", line 269, in _build
  File "mypy/build.py", line 2935, in dispatch
  File "mypy/build.py", line 3333, in process_graph
  File "mypy/build.py", line 3460, in process_stale_scc
  File "mypy/build.py", line 2497, in write_cache
  File "mypy/build.py", line 1560, in write_cache
  File "mypy/util.py", line 924, in json_dumps
TypeError: Integer exceeds 64-bit range
```

Related: ijl/orjson#116
  • Loading branch information
q0w authored Nov 14, 2024
1 parent 2ebc690 commit b137254
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions mypy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,11 +928,17 @@ def quote_docstring(docstr: str) -> str:
def json_dumps(obj: object, debug: bool = False) -> bytes:
if orjson is not None:
if debug:
return orjson.dumps(obj, option=orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS) # type: ignore[no-any-return]
dumps_option = orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS
else:
# TODO: If we don't sort keys here, testIncrementalInternalScramble fails
# We should document exactly what is going on there
return orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) # type: ignore[no-any-return]
dumps_option = orjson.OPT_SORT_KEYS

try:
return orjson.dumps(obj, option=dumps_option) # type: ignore[no-any-return]
except TypeError as e:
if str(e) != "Integer exceeds 64-bit range":
raise

if debug:
return json.dumps(obj, indent=2, sort_keys=True).encode("utf-8")
Expand Down

0 comments on commit b137254

Please sign in to comment.