Skip to content

Commit 551c2b8

Browse files
Merge branch 'master' into patch-6
2 parents 2bdf733 + e852829 commit 551c2b8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+4189
-419
lines changed

docs/requirements-docs.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-r ../mypy-requirements.txt
12
sphinx>=8.1.0
23
furo>=2022.3.4
34
myst-parser>=4.0.0

mypy/build.py

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from typing_extensions import TypeAlias as _TypeAlias
4141

4242
import mypy.semanal_main
43+
from mypy.cache import Buffer
4344
from mypy.checker import TypeChecker
4445
from mypy.error_formatter import OUTPUT_CHOICES, ErrorFormatter
4546
from mypy.errors import CompileError, ErrorInfo, Errors, report_internal_error
@@ -116,6 +117,8 @@
116117
"abc",
117118
}
118119

120+
# We are careful now, we can increase this in future if safe/useful.
121+
MAX_GC_FREEZE_CYCLES = 1
119122

120123
Graph: _TypeAlias = dict[str, "State"]
121124

@@ -707,6 +710,8 @@ def __init__(
707710
# new file can be processed O(n**2) times. This cache
708711
# avoids most of this redundant work.
709712
self.ast_cache: dict[str, tuple[MypyFile, list[ErrorInfo]]] = {}
713+
# Number of times we used GC optimization hack for fresh SCCs.
714+
self.gc_freeze_cycles = 0
710715

711716
def dump_stats(self) -> None:
712717
if self.options.dump_build_stats:
@@ -1139,6 +1144,17 @@ def read_deps_cache(manager: BuildManager, graph: Graph) -> dict[str, FgDepMeta]
11391144
return module_deps_metas
11401145

11411146

1147+
def _load_ff_file(file: str, manager: BuildManager, log_error: str) -> bytes | None:
1148+
t0 = time.time()
1149+
try:
1150+
data = manager.metastore.read(file)
1151+
except OSError:
1152+
manager.log(log_error + file)
1153+
return None
1154+
manager.add_stats(metastore_read_time=time.time() - t0)
1155+
return data
1156+
1157+
11421158
def _load_json_file(
11431159
file: str, manager: BuildManager, log_success: str, log_error: str
11441160
) -> dict[str, Any] | None:
@@ -1259,7 +1275,11 @@ def get_cache_names(id: str, path: str, options: Options) -> tuple[str, str, str
12591275
deps_json = None
12601276
if options.cache_fine_grained:
12611277
deps_json = prefix + ".deps.json"
1262-
return (prefix + ".meta.json", prefix + ".data.json", deps_json)
1278+
if options.fixed_format_cache:
1279+
data_suffix = ".data.ff"
1280+
else:
1281+
data_suffix = ".data.json"
1282+
return (prefix + ".meta.json", prefix + data_suffix, deps_json)
12631283

12641284

12651285
def find_cache_meta(id: str, path: str, manager: BuildManager) -> CacheMeta | None:
@@ -1559,8 +1579,13 @@ def write_cache(
15591579
tree.path = path
15601580

15611581
# Serialize data and analyze interface
1562-
data = tree.serialize()
1563-
data_bytes = json_dumps(data, manager.options.debug_cache)
1582+
if manager.options.fixed_format_cache:
1583+
data_io = Buffer()
1584+
tree.write(data_io)
1585+
data_bytes = data_io.getvalue()
1586+
else:
1587+
data = tree.serialize()
1588+
data_bytes = json_dumps(data, manager.options.debug_cache)
15641589
interface_hash = hash_digest(data_bytes)
15651590

15661591
plugin_data = manager.plugin.report_config_data(ReportConfigContext(id, path, is_check=False))
@@ -2085,15 +2110,23 @@ def load_tree(self, temporary: bool = False) -> None:
20852110
self.meta is not None
20862111
), "Internal error: this method must be called only for cached modules"
20872112

2088-
data = _load_json_file(
2089-
self.meta.data_json, self.manager, "Load tree ", "Could not load tree: "
2090-
)
2113+
data: bytes | dict[str, Any] | None
2114+
if self.options.fixed_format_cache:
2115+
data = _load_ff_file(self.meta.data_json, self.manager, "Could not load tree: ")
2116+
else:
2117+
data = _load_json_file(
2118+
self.meta.data_json, self.manager, "Load tree ", "Could not load tree: "
2119+
)
20912120
if data is None:
20922121
return
20932122

20942123
t0 = time.time()
20952124
# TODO: Assert data file wasn't changed.
2096-
self.tree = MypyFile.deserialize(data)
2125+
if isinstance(data, bytes):
2126+
data_io = Buffer(data)
2127+
self.tree = MypyFile.read(data_io)
2128+
else:
2129+
self.tree = MypyFile.deserialize(data)
20972130
t1 = time.time()
20982131
self.manager.add_stats(deserialize_time=t1 - t0)
20992132
if not temporary:
@@ -2481,7 +2514,11 @@ def write_cache(self) -> None:
24812514
):
24822515
if self.options.debug_serialize:
24832516
try:
2484-
self.tree.serialize()
2517+
if self.manager.options.fixed_format_cache:
2518+
data = Buffer()
2519+
self.tree.write(data)
2520+
else:
2521+
self.tree.serialize()
24852522
except Exception:
24862523
print(f"Error serializing {self.id}", file=self.manager.stdout)
24872524
raise # Propagate to display traceback
@@ -3326,8 +3363,29 @@ def process_graph(graph: Graph, manager: BuildManager) -> None:
33263363
#
33273364
# TODO: see if it's possible to determine if we need to process only a
33283365
# _subset_ of the past SCCs instead of having to process them all.
3366+
if (
3367+
platform.python_implementation() == "CPython"
3368+
and manager.gc_freeze_cycles < MAX_GC_FREEZE_CYCLES
3369+
):
3370+
# When deserializing cache we create huge amount of new objects, so even
3371+
# with our generous GC thresholds, GC is still doing a lot of pointless
3372+
# work searching for garbage. So, we temporarily disable it when
3373+
# processing fresh SCCs, and then move all the new objects to the oldest
3374+
# generation with the freeze()/unfreeze() trick below. This is arguably
3375+
# a hack, but it gives huge performance wins for large third-party
3376+
# libraries, like torch.
3377+
gc.collect()
3378+
gc.disable()
33293379
for prev_scc in fresh_scc_queue:
33303380
process_fresh_modules(graph, prev_scc, manager)
3381+
if (
3382+
platform.python_implementation() == "CPython"
3383+
and manager.gc_freeze_cycles < MAX_GC_FREEZE_CYCLES
3384+
):
3385+
manager.gc_freeze_cycles += 1
3386+
gc.freeze()
3387+
gc.unfreeze()
3388+
gc.enable()
33313389
fresh_scc_queue = []
33323390
size = len(scc)
33333391
if size == 1:

mypy/cache.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
from __future__ import annotations
2+
3+
from collections.abc import Sequence
4+
from typing import TYPE_CHECKING, Final
5+
6+
try:
7+
from native_internal import (
8+
Buffer as Buffer,
9+
read_bool as read_bool,
10+
read_float as read_float,
11+
read_int as read_int,
12+
read_str as read_str,
13+
read_tag as read_tag,
14+
write_bool as write_bool,
15+
write_float as write_float,
16+
write_int as write_int,
17+
write_str as write_str,
18+
write_tag as write_tag,
19+
)
20+
except ImportError:
21+
# TODO: temporary, remove this after we publish mypy-native on PyPI.
22+
if not TYPE_CHECKING:
23+
24+
class Buffer:
25+
def __init__(self, source: bytes = b"") -> None:
26+
raise NotImplementedError
27+
28+
def getvalue(self) -> bytes:
29+
raise NotImplementedError
30+
31+
def read_int(data: Buffer) -> int:
32+
raise NotImplementedError
33+
34+
def write_int(data: Buffer, value: int) -> None:
35+
raise NotImplementedError
36+
37+
def read_tag(data: Buffer) -> int:
38+
raise NotImplementedError
39+
40+
def write_tag(data: Buffer, value: int) -> None:
41+
raise NotImplementedError
42+
43+
def read_str(data: Buffer) -> str:
44+
raise NotImplementedError
45+
46+
def write_str(data: Buffer, value: str) -> None:
47+
raise NotImplementedError
48+
49+
def read_bool(data: Buffer) -> bool:
50+
raise NotImplementedError
51+
52+
def write_bool(data: Buffer, value: bool) -> None:
53+
raise NotImplementedError
54+
55+
def read_float(data: Buffer) -> float:
56+
raise NotImplementedError
57+
58+
def write_float(data: Buffer, value: float) -> None:
59+
raise NotImplementedError
60+
61+
62+
LITERAL_INT: Final = 1
63+
LITERAL_STR: Final = 2
64+
LITERAL_BOOL: Final = 3
65+
LITERAL_FLOAT: Final = 4
66+
LITERAL_COMPLEX: Final = 5
67+
LITERAL_NONE: Final = 6
68+
69+
70+
def read_literal(data: Buffer, tag: int) -> int | str | bool | float:
71+
if tag == LITERAL_INT:
72+
return read_int(data)
73+
elif tag == LITERAL_STR:
74+
return read_str(data)
75+
elif tag == LITERAL_BOOL:
76+
return read_bool(data)
77+
elif tag == LITERAL_FLOAT:
78+
return read_float(data)
79+
assert False, f"Unknown literal tag {tag}"
80+
81+
82+
def write_literal(data: Buffer, value: int | str | bool | float | complex | None) -> None:
83+
if isinstance(value, bool):
84+
write_tag(data, LITERAL_BOOL)
85+
write_bool(data, value)
86+
elif isinstance(value, int):
87+
write_tag(data, LITERAL_INT)
88+
write_int(data, value)
89+
elif isinstance(value, str):
90+
write_tag(data, LITERAL_STR)
91+
write_str(data, value)
92+
elif isinstance(value, float):
93+
write_tag(data, LITERAL_FLOAT)
94+
write_float(data, value)
95+
elif isinstance(value, complex):
96+
write_tag(data, LITERAL_COMPLEX)
97+
write_float(data, value.real)
98+
write_float(data, value.imag)
99+
else:
100+
write_tag(data, LITERAL_NONE)
101+
102+
103+
def read_int_opt(data: Buffer) -> int | None:
104+
if read_bool(data):
105+
return read_int(data)
106+
return None
107+
108+
109+
def write_int_opt(data: Buffer, value: int | None) -> None:
110+
if value is not None:
111+
write_bool(data, True)
112+
write_int(data, value)
113+
else:
114+
write_bool(data, False)
115+
116+
117+
def read_str_opt(data: Buffer) -> str | None:
118+
if read_bool(data):
119+
return read_str(data)
120+
return None
121+
122+
123+
def write_str_opt(data: Buffer, value: str | None) -> None:
124+
if value is not None:
125+
write_bool(data, True)
126+
write_str(data, value)
127+
else:
128+
write_bool(data, False)
129+
130+
131+
def read_int_list(data: Buffer) -> list[int]:
132+
size = read_int(data)
133+
return [read_int(data) for _ in range(size)]
134+
135+
136+
def write_int_list(data: Buffer, value: list[int]) -> None:
137+
write_int(data, len(value))
138+
for item in value:
139+
write_int(data, item)
140+
141+
142+
def read_str_list(data: Buffer) -> list[str]:
143+
size = read_int(data)
144+
return [read_str(data) for _ in range(size)]
145+
146+
147+
def write_str_list(data: Buffer, value: Sequence[str]) -> None:
148+
write_int(data, len(value))
149+
for item in value:
150+
write_str(data, item)
151+
152+
153+
def read_str_opt_list(data: Buffer) -> list[str | None]:
154+
size = read_int(data)
155+
return [read_str_opt(data) for _ in range(size)]
156+
157+
158+
def write_str_opt_list(data: Buffer, value: list[str | None]) -> None:
159+
write_int(data, len(value))
160+
for item in value:
161+
write_str_opt(data, item)

mypy/checker.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
from mypy.typeanal import check_for_explicit_any, has_any_from_unimported_type, make_optional_type
172172
from mypy.typeops import (
173173
bind_self,
174+
can_have_shared_disjoint_base,
174175
coerce_to_literal,
175176
custom_special_method,
176177
erase_def_to_union_or_bound,
@@ -2658,6 +2659,8 @@ def visit_class_def(self, defn: ClassDef) -> None:
26582659
for base in typ.mro[1:]:
26592660
if base.is_final:
26602661
self.fail(message_registry.CANNOT_INHERIT_FROM_FINAL.format(base.name), defn)
2662+
if not can_have_shared_disjoint_base(typ.bases):
2663+
self.fail(message_registry.INCOMPATIBLE_DISJOINT_BASES.format(typ.name), defn)
26612664
with self.tscope.class_scope(defn.info), self.enter_partial_types(is_class=True):
26622665
old_binder = self.binder
26632666
self.binder = ConditionalTypeBinder(self.options)
@@ -5549,10 +5552,10 @@ def visit_continue_stmt(self, s: ContinueStmt) -> None:
55495552
return
55505553

55515554
def visit_match_stmt(self, s: MatchStmt) -> None:
5552-
named_subject = self._make_named_statement_for_match(s)
55535555
# In sync with similar actions elsewhere, narrow the target if
55545556
# we are matching an AssignmentExpr
55555557
unwrapped_subject = collapse_walrus(s.subject)
5558+
named_subject = self._make_named_statement_for_match(s, unwrapped_subject)
55565559
with self.binder.frame_context(can_skip=False, fall_through=0):
55575560
subject_type = get_proper_type(self.expr_checker.accept(s.subject))
55585561

@@ -5643,9 +5646,8 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
56435646
with self.binder.frame_context(can_skip=False, fall_through=2):
56445647
pass
56455648

5646-
def _make_named_statement_for_match(self, s: MatchStmt) -> Expression:
5649+
def _make_named_statement_for_match(self, s: MatchStmt, subject: Expression) -> Expression:
56475650
"""Construct a fake NameExpr for inference if a match clause is complex."""
5648-
subject = s.subject
56495651
if self.binder.can_put_directly(subject):
56505652
# Already named - we should infer type of it as given
56515653
return subject
@@ -5826,6 +5828,10 @@ def _make_fake_typeinfo_and_full_name(
58265828
format_type_distinctly(*base_classes, options=self.options, bare=True), "and"
58275829
)
58285830

5831+
if not can_have_shared_disjoint_base(base_classes):
5832+
errors.append((pretty_names_list, "have distinct disjoint bases"))
5833+
return None
5834+
58295835
new_errors = []
58305836
for base in base_classes:
58315837
if base.type.is_final:

mypy/checker_shared.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ def checking_await_set(self) -> Iterator[None]:
272272
def get_precise_awaitable_type(self, typ: Type, local_errors: ErrorWatcher) -> Type | None:
273273
raise NotImplementedError
274274

275+
@abstractmethod
276+
def add_any_attribute_to_type(self, typ: Type, name: str) -> Type:
277+
raise NotImplementedError
278+
275279
@abstractmethod
276280
def is_defined_in_stub(self, typ: Instance, /) -> bool:
277281
raise NotImplementedError

mypy/checkexpr.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,10 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) ->
582582
and not node.node.no_args
583583
and not (
584584
isinstance(union_target := get_proper_type(node.node.target), UnionType)
585-
and union_target.uses_pep604_syntax
585+
and (
586+
union_target.uses_pep604_syntax
587+
or self.chk.options.python_version >= (3, 10)
588+
)
586589
)
587590
):
588591
self.msg.type_arguments_not_allowed(e)

mypy/checkmember.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,13 +1484,7 @@ def analyze_decorator_or_funcbase_access(
14841484
if isinstance(defn, Decorator):
14851485
return analyze_var(name, defn.var, itype, mx)
14861486
typ = function_type(defn, mx.chk.named_type("builtins.function"))
1487-
is_trivial_self = False
1488-
if isinstance(defn, Decorator):
1489-
# Use fast path if there are trivial decorators like @classmethod or @property
1490-
is_trivial_self = defn.func.is_trivial_self and not defn.decorators
1491-
elif isinstance(defn, (FuncDef, OverloadedFuncDef)):
1492-
is_trivial_self = defn.is_trivial_self
1493-
if is_trivial_self:
1487+
if isinstance(defn, (FuncDef, OverloadedFuncDef)) and defn.is_trivial_self:
14941488
return bind_self_fast(typ, mx.self_type)
14951489
typ = check_self_arg(typ, mx.self_type, defn.is_class, mx.context, name, mx.msg)
14961490
return bind_self(typ, original_type=mx.self_type, is_classmethod=defn.is_class)

0 commit comments

Comments
 (0)