Skip to content

Commit 08bec72

Browse files
committed
Clean-up/optimize some case reads
1 parent e3d9d41 commit 08bec72

File tree

3 files changed

+58
-41
lines changed

3 files changed

+58
-41
lines changed

mypy/nodes.py

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -728,16 +728,14 @@ def write(self, data: BytesIO) -> None:
728728

729729
@classmethod
730730
def read(cls, data: BytesIO) -> OverloadedFuncDef:
731-
res = OverloadedFuncDef(
732-
[cast(OverloadPart, read_symbol(data)) for _ in range(read_int(data))]
733-
)
731+
res = OverloadedFuncDef([read_overload_part(data) for _ in range(read_int(data))])
734732
typ = mypy.types.read_type_opt(data)
735733
if typ is not None:
736734
assert isinstance(typ, mypy.types.ProperType)
737735
res.type = typ
738736
res._fullname = read_str(data)
739737
if read_bool(data):
740-
res.impl = cast(OverloadPart, read_symbol(data))
738+
res.impl = read_overload_part(data)
741739
# set line for empty overload items, as not set in __init__
742740
if len(res.items) > 0:
743741
res.set_line(res.impl.line)
@@ -1042,12 +1040,11 @@ def write(self, data: BytesIO) -> None:
10421040

10431041
@classmethod
10441042
def read(cls, data: BytesIO) -> FuncDef:
1045-
ret = FuncDef(
1046-
read_str(data),
1047-
[],
1048-
Block([]),
1049-
cast("mypy.types.FunctionLike | None", mypy.types.read_type_opt(data)),
1050-
)
1043+
name = read_str(data)
1044+
typ: mypy.types.FunctionLike | None = None
1045+
if read_bool(data):
1046+
typ = mypy.types.read_function_like(data)
1047+
ret = FuncDef(name, [], Block([]), typ)
10511048
ret._fullname = read_str(data)
10521049
read_flags(data, ret, FUNCDEF_FLAGS)
10531050
# NOTE: ret.info is set in the fixup phase.
@@ -1340,14 +1337,12 @@ def write(self, data: BytesIO) -> None:
13401337
@classmethod
13411338
def read(cls, data: BytesIO) -> Var:
13421339
name = read_str(data)
1343-
type = mypy.types.read_type_opt(data)
1344-
setter_type = mypy.types.read_type_opt(data)
1345-
v = Var(name, type)
1346-
assert (
1347-
setter_type is None
1348-
or isinstance(setter_type, mypy.types.ProperType)
1349-
and isinstance(setter_type, mypy.types.CallableType)
1350-
)
1340+
typ = mypy.types.read_type_opt(data)
1341+
v = Var(name, typ)
1342+
setter_type: mypy.types.CallableType | None = None
1343+
if read_bool(data):
1344+
assert read_int(data) == mypy.types.CALLABLE_TYPE
1345+
setter_type = mypy.types.CallableType.read(data)
13511346
v.setter_type = setter_type
13521347
v.is_ready = False # Override True default set in __init__
13531348
v._fullname = read_str(data)
@@ -1480,7 +1475,7 @@ def read(cls, data: BytesIO) -> ClassDef:
14801475
res = ClassDef(
14811476
read_str(data),
14821477
Block([]),
1483-
cast(list[mypy.types.TypeVarLikeType], mypy.types.read_type_list(data)),
1478+
[mypy.types.read_type_var_like(data) for _ in range(read_int(data))],
14841479
)
14851480
res.fullname = read_str(data)
14861481
return res
@@ -4275,32 +4270,26 @@ def write(self, data: BytesIO) -> None:
42754270
write_str(data, self._fullname)
42764271
self.target.write(data)
42774272
mypy.types.write_type_list(data, self.alias_tvars)
4278-
write_bool(data, self.no_args)
4279-
write_bool(data, self.normalized)
42804273
write_int(data, self.line)
42814274
write_int(data, self.column)
4275+
write_bool(data, self.no_args)
4276+
write_bool(data, self.normalized)
42824277
write_bool(data, self.python_3_12_type_alias)
42834278

42844279
@classmethod
42854280
def read(cls, data: BytesIO) -> TypeAlias:
42864281
fullname = read_str(data)
42874282
target = mypy.types.read_type(data)
4288-
alias_tvars = mypy.types.read_type_list(data)
4289-
assert all(isinstance(t, mypy.types.TypeVarLikeType) for t in alias_tvars)
4290-
no_args = read_bool(data)
4291-
normalized = read_bool(data)
4292-
line = read_int(data)
4293-
column = read_int(data)
4294-
python_3_12_type_alias = read_bool(data)
4295-
return cls(
4283+
alias_tvars = [mypy.types.read_type_var_like(data) for _ in range(read_int(data))]
4284+
return TypeAlias(
42964285
target,
42974286
fullname,
4298-
line,
4299-
column,
4300-
alias_tvars=cast(list[mypy.types.TypeVarLikeType], alias_tvars),
4301-
no_args=no_args,
4302-
normalized=normalized,
4303-
python_3_12_type_alias=python_3_12_type_alias,
4287+
read_int(data),
4288+
read_int(data),
4289+
alias_tvars=alias_tvars,
4290+
no_args=read_bool(data),
4291+
normalized=read_bool(data),
4292+
python_3_12_type_alias=read_bool(data),
43044293
)
43054294

43064295

@@ -4562,7 +4551,7 @@ def deserialize(cls, data: JsonDict) -> SymbolTableNode:
45624551
return stnode
45634552

45644553
def write(self, data: BytesIO, prefix: str, name: str) -> None:
4565-
write_str(data, node_kinds[self.kind])
4554+
write_int(data, self.kind)
45664555
write_bool(data, self.module_hidden)
45674556
write_bool(data, self.module_public)
45684557
write_bool(data, self.implicit)
@@ -4592,8 +4581,7 @@ def write(self, data: BytesIO, prefix: str, name: str) -> None:
45924581

45934582
@classmethod
45944583
def read(cls, data: BytesIO) -> SymbolTableNode:
4595-
kind = inverse_node_kinds[read_str(data)]
4596-
sym = SymbolTableNode(kind, None)
4584+
sym = SymbolTableNode(read_int(data), None)
45974585
sym.module_hidden = read_bool(data)
45984586
sym.module_public = read_bool(data)
45994587
sym.implicit = read_bool(data)
@@ -4912,3 +4900,12 @@ def read_symbol(data: BytesIO) -> mypy.nodes.SymbolNode:
49124900
if marker == TYPE_VAR_TUPLE_EXPR:
49134901
return mypy.nodes.TypeVarTupleExpr.read(data)
49144902
assert False, f"Unknown symbol marker {marker}"
4903+
4904+
4905+
def read_overload_part(data: BytesIO) -> OverloadPart:
4906+
marker = read_int(data)
4907+
if marker == DECORATOR:
4908+
return Decorator.read(data)
4909+
if marker == FUNC_DEF:
4910+
return FuncDef.read(data)
4911+
assert False, f"Invalid marker for an OverloadPart {marker}"

mypy/test/testcheck.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import re
77
import sys
88
import tempfile
9-
from mypy.cache import BytesIO
109
from pathlib import Path
1110

1211
from mypy import build
1312
from mypy.build import Graph
13+
from mypy.cache import BytesIO
1414
from mypy.errors import CompileError
1515
from mypy.fixup import fixup_module
1616
from mypy.modulefinder import BuildSource, FindModuleCache, SearchPaths

mypy/types.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,7 @@ def read(cls, data: BytesIO) -> Parameters:
20122012
# we would spend ~20% of types deserialization time in Enum.__call__().
20132013
[ARG_KINDS[ak] for ak in read_int_list(data)],
20142014
read_str_opt_list(data),
2015-
variables=cast(list[TypeVarLikeType], read_type_list(data)),
2015+
variables=[read_type_var_like(data) for _ in range(read_int(data))],
20162016
imprecise_arg_kinds=read_bool(data),
20172017
)
20182018

@@ -2544,7 +2544,7 @@ def read(cls, data: BytesIO) -> CallableType:
25442544
read_type(data),
25452545
fallback,
25462546
name=read_str_opt(data),
2547-
variables=cast(list[TypeVarLikeType], read_type_list(data)),
2547+
variables=[read_type_var_like(data) for _ in range(read_int(data))],
25482548
is_ellipsis_args=read_bool(data),
25492549
implicit=read_bool(data),
25502550
is_bound=read_bool(data),
@@ -4175,6 +4175,26 @@ def read_type(data: BytesIO) -> Type:
41754175
assert False, f"Unknown type marker {marker}"
41764176

41774177

4178+
def read_function_like(data: BytesIO) -> FunctionLike:
4179+
marker = read_int(data)
4180+
if marker == CALLABLE_TYPE:
4181+
return CallableType.read(data)
4182+
if marker == OVERLOADED:
4183+
return Overloaded.read(data)
4184+
assert False, f"Invalid type marker for FunctionLike {marker}"
4185+
4186+
4187+
def read_type_var_like(data: BytesIO) -> TypeVarLikeType:
4188+
marker = read_int(data)
4189+
if marker == TYPE_VAR_TYPE:
4190+
return TypeVarType.read(data)
4191+
if marker == PARAM_SPEC_TYPE:
4192+
return ParamSpecType.read(data)
4193+
if marker == TYPE_VAR_TUPLE_TYPE:
4194+
return TypeVarTupleType.read(data)
4195+
assert False, f"Invalid type marker for TypeVarLikeType {marker}"
4196+
4197+
41784198
def read_type_opt(data: BytesIO) -> Type | None:
41794199
if read_bool(data):
41804200
return read_type(data)

0 commit comments

Comments
 (0)