Skip to content

Commit 9e78415

Browse files
committed
Support floats in JSON in fixed-format cache
1 parent e15a6d5 commit 9e78415

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

mypy/cache.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
from mypy_extensions import u8
7070

7171
# High-level cache layout format
72-
CACHE_VERSION: Final = 9
72+
CACHE_VERSION: Final = 10
7373

7474
# Type used internally to represent errors:
7575
# (path, line, column, end_line, end_column, severity, message, code)
@@ -478,7 +478,7 @@ def write_str_opt_list(data: WriteBuffer, value: list[str | None]) -> None:
478478
write_str_opt(data, item)
479479

480480

481-
Value: _TypeAlias = None | int | str | bool
481+
Value: _TypeAlias = None | int | float | str | bool
482482

483483
# Our JSON format is somewhat non-standard as we distinguish lists and tuples.
484484
# This is convenient for some internal things, like mypyc plugin and error serialization.
@@ -508,6 +508,8 @@ def read_json_value(data: ReadBuffer) -> JsonValue:
508508
if tag == DICT_STR_GEN:
509509
size = read_int_bare(data)
510510
return {read_str_bare(data): read_json_value(data) for _ in range(size)}
511+
if tag == LITERAL_FLOAT:
512+
return read_float_bare(data)
511513
assert False, f"Invalid JSON tag: {tag}"
512514

513515

@@ -538,6 +540,9 @@ def write_json_value(data: WriteBuffer, value: JsonValue) -> None:
538540
for key in sorted(value):
539541
write_str_bare(data, key)
540542
write_json_value(data, value[key])
543+
elif isinstance(value, float):
544+
write_tag(data, LITERAL_FLOAT)
545+
write_float_bare(data, value)
541546
else:
542547
assert False, f"Invalid JSON value: {value}"
543548

test-data/unit/check-dataclasses.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,26 @@ class Person:
12521252

12531253
[builtins fixtures/dataclasses.pyi]
12541254

1255+
[case testDataclassesFloatSerializationIncremental]
1256+
import m
1257+
[file m.py]
1258+
from lib import MyDataClass
1259+
[file m.py.2]
1260+
from lib import MyDataClass
1261+
1262+
reveal_type(MyDataClass.MY_CONSTANT)
1263+
[file lib.py]
1264+
from dataclasses import dataclass
1265+
from typing import Final
1266+
1267+
@dataclass(kw_only=True, repr=False)
1268+
class MyDataClass:
1269+
MY_CONSTANT: Final = 1.234
1270+
[builtins fixtures/dataclasses.pyi]
1271+
[out]
1272+
[out2]
1273+
tmp/m.py:3: note: Revealed type is "Literal[1.234]?"
1274+
12551275
[case testDataclassesDefaultsMroOtherFile]
12561276
import a
12571277

0 commit comments

Comments
 (0)