Skip to content

Commit b137254

Browse files
authored
Fallback to stdlib json if integer exceeds 64-bit range (#18148)
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
1 parent 2ebc690 commit b137254

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

mypy/util.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,11 +928,17 @@ def quote_docstring(docstr: str) -> str:
928928
def json_dumps(obj: object, debug: bool = False) -> bytes:
929929
if orjson is not None:
930930
if debug:
931-
return orjson.dumps(obj, option=orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS) # type: ignore[no-any-return]
931+
dumps_option = orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS
932932
else:
933933
# TODO: If we don't sort keys here, testIncrementalInternalScramble fails
934934
# We should document exactly what is going on there
935-
return orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) # type: ignore[no-any-return]
935+
dumps_option = orjson.OPT_SORT_KEYS
936+
937+
try:
938+
return orjson.dumps(obj, option=dumps_option) # type: ignore[no-any-return]
939+
except TypeError as e:
940+
if str(e) != "Integer exceeds 64-bit range":
941+
raise
936942

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

0 commit comments

Comments
 (0)