Skip to content

Commit e69e117

Browse files
committed
Implement closed support in serialization
1 parent 6de2fe4 commit e69e117

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

mypy/types.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,17 +3023,17 @@ def serialize(self) -> JsonDict:
30233023
"required_keys": sorted(self.required_keys),
30243024
"readonly_keys": sorted(self.readonly_keys),
30253025
"fallback": self.fallback.serialize(),
3026+
"is_closed": self.is_closed,
30263027
}
30273028

30283029
@classmethod
30293030
def deserialize(cls, data: JsonDict) -> TypedDictType:
30303031
assert data[".class"] == "TypedDictType"
3031-
# TODO: round-trip is_closed
30323032
return TypedDictType(
30333033
{n: deserialize_type(t) for (n, t) in data["items"]},
30343034
set(data["required_keys"]),
30353035
set(data["readonly_keys"]),
3036-
False,
3036+
bool(data["is_closed"]),
30373037
Instance.deserialize(data["fallback"]),
30383038
)
30393039

@@ -3043,18 +3043,18 @@ def write(self, data: WriteBuffer) -> None:
30433043
write_type_map(data, self.items)
30443044
write_str_list(data, sorted(self.required_keys))
30453045
write_str_list(data, sorted(self.readonly_keys))
3046+
write_bool(data, self.is_closed)
30463047
write_tag(data, END_TAG)
30473048

30483049
@classmethod
30493050
def read(cls, data: ReadBuffer) -> TypedDictType:
30503051
assert read_tag(data) == INSTANCE
30513052
fallback = Instance.read(data)
3052-
# TODO: round-trip is_closed
30533053
ret = TypedDictType(
30543054
read_type_map(data),
30553055
set(read_str_list(data)),
30563056
set(read_str_list(data)),
3057-
False,
3057+
read_bool(data),
30583058
fallback,
30593059
)
30603060
assert read_tag(data) == END_TAG

test-data/unit/check-serialize.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,20 @@ main:2: note: Revealed type is "TypedDict('m.D', {'x'?: builtins.int, 'y'?: buil
10881088
[out2]
10891089
main:2: note: Revealed type is "TypedDict('m.D', {'x'?: builtins.int, 'y'?: builtins.str})"
10901090

1091+
[case testSerializeClosedTotalTypedDict]
1092+
from m import d
1093+
reveal_type(d)
1094+
[file m.py]
1095+
from typing import TypedDict
1096+
D = TypedDict('D', {'x': int, 'y': str}, closed=True)
1097+
d: D
1098+
[builtins fixtures/dict.pyi]
1099+
[typing fixtures/typing-typeddict.pyi]
1100+
[out1]
1101+
main:2: note: Revealed type is "TypedDict('m.D', {'x': builtins.int, 'y': builtins.str}, closed=True)"
1102+
[out2]
1103+
main:2: note: Revealed type is "TypedDict('m.D', {'x': builtins.int, 'y': builtins.str}, closed=True)"
1104+
10911105
--
10921106
-- Modules
10931107
--

0 commit comments

Comments
 (0)