Skip to content

Commit f844487

Browse files
Merge branch 'master' into pos_only-in-stubgen
2 parents e08e5cd + 6600073 commit f844487

Some content is hidden

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

47 files changed

+1483
-340
lines changed

misc/profile_self_check.py renamed to misc/profile_check.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
"""Compile mypy using mypyc and profile self-check using perf.
1+
"""Compile mypy using mypyc and profile type checking using perf.
2+
3+
By default does a self check.
24
35
Notes:
46
- Only Linux is supported for now (TODO: add support for other profilers)
@@ -23,6 +25,8 @@
2325
CFLAGS="-O2 -g -fno-omit-frame-pointer"
2426
"""
2527

28+
from __future__ import annotations
29+
2630
import argparse
2731
import glob
2832
import os
@@ -41,24 +45,28 @@
4145
TARGET_DIR = "mypy.profile.tmpdir"
4246

4347

44-
def _profile_self_check(target_dir: str) -> None:
48+
def _profile_type_check(target_dir: str, code: str | None) -> None:
4549
cache_dir = os.path.join(target_dir, ".mypy_cache")
4650
if os.path.exists(cache_dir):
4751
shutil.rmtree(cache_dir)
48-
files = []
49-
for pat in "mypy/*.py", "mypy/*/*.py", "mypyc/*.py", "mypyc/test/*.py":
50-
files.extend(glob.glob(pat))
51-
self_check_cmd = ["python", "-m", "mypy", "--config-file", "mypy_self_check.ini"] + files
52-
cmdline = ["perf", "record", "-g"] + self_check_cmd
52+
args = []
53+
if code is None:
54+
args.extend(["--config-file", "mypy_self_check.ini"])
55+
for pat in "mypy/*.py", "mypy/*/*.py", "mypyc/*.py", "mypyc/test/*.py":
56+
args.extend(glob.glob(pat))
57+
else:
58+
args.extend(["-c", code])
59+
check_cmd = ["python", "-m", "mypy"] + args
60+
cmdline = ["perf", "record", "-g"] + check_cmd
5361
t0 = time.time()
5462
subprocess.run(cmdline, cwd=target_dir, check=True)
5563
elapsed = time.time() - t0
5664
print(f"{elapsed:.2f}s elapsed")
5765

5866

59-
def profile_self_check(target_dir: str) -> None:
67+
def profile_type_check(target_dir: str, code: str | None) -> None:
6068
try:
61-
_profile_self_check(target_dir)
69+
_profile_type_check(target_dir, code)
6270
except subprocess.CalledProcessError:
6371
print("\nProfiling failed! You may missing some permissions.")
6472
print("\nThis may help (note that it has security implications):")
@@ -92,7 +100,7 @@ def main() -> None:
92100
check_requirements()
93101

94102
parser = argparse.ArgumentParser(
95-
description="Compile mypy and profile self checking using 'perf'."
103+
description="Compile mypy and profile type checking using 'perf' (by default, self check)."
96104
)
97105
parser.add_argument(
98106
"--multi-file",
@@ -102,9 +110,17 @@ def main() -> None:
102110
parser.add_argument(
103111
"--skip-compile", action="store_true", help="use compiled mypy from previous run"
104112
)
113+
parser.add_argument(
114+
"-c",
115+
metavar="CODE",
116+
default=None,
117+
type=str,
118+
help="profile type checking Python code fragment instead of mypy self-check",
119+
)
105120
args = parser.parse_args()
106121
multi_file: bool = args.multi_file
107122
skip_compile: bool = args.skip_compile
123+
code: str | None = args.c
108124

109125
target_dir = TARGET_DIR
110126

@@ -116,7 +132,7 @@ def main() -> None:
116132
elif not os.path.isdir(target_dir):
117133
sys.exit("error: Can't find compile mypy from previous run -- can't use --skip-compile")
118134

119-
profile_self_check(target_dir)
135+
profile_type_check(target_dir, code)
120136

121137
print()
122138
print('NOTE: Compile CPython using CFLAGS="-O2 -g -fno-omit-frame-pointer" for good results')

mypy/checker.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ def accept_loop(
618618
if on_enter_body is not None:
619619
on_enter_body()
620620

621-
with IterationErrorWatcher(self.msg.errors, iter_errors) as watcher:
621+
with IterationErrorWatcher(self.msg.errors, iter_errors):
622622
self.accept(body)
623623

624624
partials_new = sum(len(pts.map) for pts in self.partial_types)
@@ -641,10 +641,7 @@ def accept_loop(
641641
if iter == 20:
642642
raise RuntimeError("Too many iterations when checking a loop")
643643

644-
for error_info in watcher.yield_error_infos():
645-
self.msg.fail(*error_info[:2], code=error_info[2])
646-
for note_info in watcher.yield_note_infos(self.options):
647-
self.note(*note_info)
644+
self.msg.iteration_dependent_errors(iter_errors)
648645

649646
# If exit_condition is set, assume it must be False on exit from the loop:
650647
if exit_condition:
@@ -3041,7 +3038,7 @@ def is_noop_for_reachability(self, s: Statement) -> bool:
30413038
if isinstance(s.expr, EllipsisExpr):
30423039
return True
30433040
elif isinstance(s.expr, CallExpr):
3044-
with self.expr_checker.msg.filter_errors():
3041+
with self.expr_checker.msg.filter_errors(filter_revealed_type=True):
30453042
typ = get_proper_type(
30463043
self.expr_checker.accept(
30473044
s.expr, allow_none_return=True, always_allow_any=True
@@ -4969,7 +4966,7 @@ def visit_try_stmt(self, s: TryStmt) -> None:
49694966
if s.finally_body:
49704967
# First we check finally_body is type safe on all abnormal exit paths
49714968
iter_errors = IterationDependentErrors()
4972-
with IterationErrorWatcher(self.msg.errors, iter_errors) as watcher:
4969+
with IterationErrorWatcher(self.msg.errors, iter_errors):
49734970
self.accept(s.finally_body)
49744971

49754972
if s.finally_body:
@@ -4986,13 +4983,9 @@ def visit_try_stmt(self, s: TryStmt) -> None:
49864983
# that follows the try statement.)
49874984
assert iter_errors is not None
49884985
if not self.binder.is_unreachable():
4989-
with IterationErrorWatcher(self.msg.errors, iter_errors) as watcher:
4986+
with IterationErrorWatcher(self.msg.errors, iter_errors):
49904987
self.accept(s.finally_body)
4991-
4992-
for error_info in watcher.yield_error_infos():
4993-
self.msg.fail(*error_info[:2], code=error_info[2])
4994-
for note_info in watcher.yield_note_infos(self.options):
4995-
self.msg.note(*note_info)
4988+
self.msg.iteration_dependent_errors(iter_errors)
49964989

49974990
def visit_try_without_finally(self, s: TryStmt, try_frame: bool) -> None:
49984991
"""Type check a try statement, ignoring the finally block.

mypy/errors.py

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from mypy.nodes import Context
1616
from mypy.options import Options
1717
from mypy.scope import Scope
18+
from mypy.types import Type
1819
from mypy.util import DEFAULT_SOURCE_OFFSET, is_typeshed_file
1920
from mypy.version import __version__ as mypy_version
2021

@@ -166,18 +167,24 @@ class ErrorWatcher:
166167
out by one of the ErrorWatcher instances.
167168
"""
168169

170+
# public attribute for the special treatment of `reveal_type` by
171+
# `MessageBuilder.reveal_type`:
172+
filter_revealed_type: bool
173+
169174
def __init__(
170175
self,
171176
errors: Errors,
172177
*,
173178
filter_errors: bool | Callable[[str, ErrorInfo], bool] = False,
174179
save_filtered_errors: bool = False,
175180
filter_deprecated: bool = False,
181+
filter_revealed_type: bool = False,
176182
) -> None:
177183
self.errors = errors
178184
self._has_new_errors = False
179185
self._filter = filter_errors
180186
self._filter_deprecated = filter_deprecated
187+
self.filter_revealed_type = filter_revealed_type
181188
self._filtered: list[ErrorInfo] | None = [] if save_filtered_errors else None
182189

183190
def __enter__(self) -> Self:
@@ -236,15 +243,41 @@ class IterationDependentErrors:
236243
# the error report occurs but really all unreachable lines.
237244
unreachable_lines: list[set[int]]
238245

239-
# One set of revealed types for each `reveal_type` statement. Each created set can
240-
# grow during the iteration. Meaning of the tuple items: function_or_member, line,
241-
# column, end_line, end_column:
242-
revealed_types: dict[tuple[str | None, int, int, int, int], set[str]]
246+
# One list of revealed types for each `reveal_type` statement. Each created list
247+
# can grow during the iteration. Meaning of the tuple items: line, column,
248+
# end_line, end_column:
249+
revealed_types: dict[tuple[int, int, int | None, int | None], list[Type]]
243250

244251
def __init__(self) -> None:
245252
self.uselessness_errors = []
246253
self.unreachable_lines = []
247-
self.revealed_types = defaultdict(set)
254+
self.revealed_types = defaultdict(list)
255+
256+
def yield_uselessness_error_infos(self) -> Iterator[tuple[str, Context, ErrorCode]]:
257+
"""Report only those `unreachable`, `redundant-expr`, and `redundant-casts`
258+
errors that could not be ruled out in any iteration step."""
259+
260+
persistent_uselessness_errors = set()
261+
for candidate in set(chain(*self.uselessness_errors)):
262+
if all(
263+
(candidate in errors) or (candidate[2] in lines)
264+
for errors, lines in zip(self.uselessness_errors, self.unreachable_lines)
265+
):
266+
persistent_uselessness_errors.add(candidate)
267+
for error_info in persistent_uselessness_errors:
268+
context = Context(line=error_info[2], column=error_info[3])
269+
context.end_line = error_info[4]
270+
context.end_column = error_info[5]
271+
yield error_info[1], context, error_info[0]
272+
273+
def yield_revealed_type_infos(self) -> Iterator[tuple[list[Type], Context]]:
274+
"""Yield all types revealed in at least one iteration step."""
275+
276+
for note_info, types in self.revealed_types.items():
277+
context = Context(line=note_info[0], column=note_info[1])
278+
context.end_line = note_info[2]
279+
context.end_column = note_info[3]
280+
yield types, context
248281

249282

250283
class IterationErrorWatcher(ErrorWatcher):
@@ -287,53 +320,8 @@ def on_error(self, file: str, info: ErrorInfo) -> bool:
287320
iter_errors.unreachable_lines[-1].update(range(info.line, info.end_line + 1))
288321
return True
289322

290-
if info.code == codes.MISC and info.message.startswith("Revealed type is "):
291-
key = info.function_or_member, info.line, info.column, info.end_line, info.end_column
292-
types = info.message.split('"')[1]
293-
if types.startswith("Union["):
294-
iter_errors.revealed_types[key].update(types[6:-1].split(", "))
295-
else:
296-
iter_errors.revealed_types[key].add(types)
297-
return True
298-
299323
return super().on_error(file, info)
300324

301-
def yield_error_infos(self) -> Iterator[tuple[str, Context, ErrorCode]]:
302-
"""Report only those `unreachable`, `redundant-expr`, and `redundant-casts`
303-
errors that could not be ruled out in any iteration step."""
304-
305-
persistent_uselessness_errors = set()
306-
iter_errors = self.iteration_dependent_errors
307-
for candidate in set(chain(*iter_errors.uselessness_errors)):
308-
if all(
309-
(candidate in errors) or (candidate[2] in lines)
310-
for errors, lines in zip(
311-
iter_errors.uselessness_errors, iter_errors.unreachable_lines
312-
)
313-
):
314-
persistent_uselessness_errors.add(candidate)
315-
for error_info in persistent_uselessness_errors:
316-
context = Context(line=error_info[2], column=error_info[3])
317-
context.end_line = error_info[4]
318-
context.end_column = error_info[5]
319-
yield error_info[1], context, error_info[0]
320-
321-
def yield_note_infos(self, options: Options) -> Iterator[tuple[str, Context]]:
322-
"""Yield all types revealed in at least one iteration step."""
323-
324-
for note_info, types in self.iteration_dependent_errors.revealed_types.items():
325-
sorted_ = sorted(types, key=lambda typ: typ.lower())
326-
if len(types) == 1:
327-
revealed = sorted_[0]
328-
elif options.use_or_syntax():
329-
revealed = " | ".join(sorted_)
330-
else:
331-
revealed = f"Union[{', '.join(sorted_)}]"
332-
context = Context(line=note_info[1], column=note_info[2])
333-
context.end_line = note_info[3]
334-
context.end_column = note_info[4]
335-
yield f'Revealed type is "{revealed}"', context
336-
337325

338326
class Errors:
339327
"""Container for compile errors.
@@ -596,18 +584,19 @@ def _add_error_info(self, file: str, info: ErrorInfo) -> None:
596584
if info.code in (IMPORT, IMPORT_UNTYPED, IMPORT_NOT_FOUND):
597585
self.seen_import_error = True
598586

587+
def get_watchers(self) -> Iterator[ErrorWatcher]:
588+
"""Yield the `ErrorWatcher` stack from top to bottom."""
589+
i = len(self._watchers)
590+
while i > 0:
591+
i -= 1
592+
yield self._watchers[i]
593+
599594
def _filter_error(self, file: str, info: ErrorInfo) -> bool:
600595
"""
601596
process ErrorWatcher stack from top to bottom,
602597
stopping early if error needs to be filtered out
603598
"""
604-
i = len(self._watchers)
605-
while i > 0:
606-
i -= 1
607-
w = self._watchers[i]
608-
if w.on_error(file, info):
609-
return True
610-
return False
599+
return any(w.on_error(file, info) for w in self.get_watchers())
611600

612601
def add_error_info(self, info: ErrorInfo) -> None:
613602
file, lines = info.origin

mypy/messages.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
from mypy import errorcodes as codes, message_registry
2424
from mypy.erasetype import erase_type
2525
from mypy.errorcodes import ErrorCode
26-
from mypy.errors import ErrorInfo, Errors, ErrorWatcher
26+
from mypy.errors import (
27+
ErrorInfo,
28+
Errors,
29+
ErrorWatcher,
30+
IterationDependentErrors,
31+
IterationErrorWatcher,
32+
)
2733
from mypy.nodes import (
2834
ARG_NAMED,
2935
ARG_NAMED_OPT,
@@ -188,12 +194,14 @@ def filter_errors(
188194
filter_errors: bool | Callable[[str, ErrorInfo], bool] = True,
189195
save_filtered_errors: bool = False,
190196
filter_deprecated: bool = False,
197+
filter_revealed_type: bool = False,
191198
) -> ErrorWatcher:
192199
return ErrorWatcher(
193200
self.errors,
194201
filter_errors=filter_errors,
195202
save_filtered_errors=save_filtered_errors,
196203
filter_deprecated=filter_deprecated,
204+
filter_revealed_type=filter_revealed_type,
197205
)
198206

199207
def add_errors(self, errors: list[ErrorInfo]) -> None:
@@ -1738,6 +1746,24 @@ def invalid_signature_for_special_method(
17381746
)
17391747

17401748
def reveal_type(self, typ: Type, context: Context) -> None:
1749+
1750+
# Search for an error watcher that modifies the "normal" behaviour (we do not
1751+
# rely on the normal `ErrorWatcher` filtering approach because we might need to
1752+
# collect the original types for a later unionised response):
1753+
for watcher in self.errors.get_watchers():
1754+
# The `reveal_type` statement should be ignored:
1755+
if watcher.filter_revealed_type:
1756+
return
1757+
# The `reveal_type` statement might be visited iteratively due to being
1758+
# placed in a loop or so. Hence, we collect the respective types of
1759+
# individual iterations so that we can report them all in one step later:
1760+
if isinstance(watcher, IterationErrorWatcher):
1761+
watcher.iteration_dependent_errors.revealed_types[
1762+
(context.line, context.column, context.end_line, context.end_column)
1763+
].append(typ)
1764+
return
1765+
1766+
# Nothing special here; just create the note:
17411767
visitor = TypeStrVisitor(options=self.options)
17421768
self.note(f'Revealed type is "{typ.accept(visitor)}"', context)
17431769

@@ -2481,6 +2507,12 @@ def match_statement_inexhaustive_match(self, typ: Type, context: Context) -> Non
24812507
code=codes.EXHAUSTIVE_MATCH,
24822508
)
24832509

2510+
def iteration_dependent_errors(self, iter_errors: IterationDependentErrors) -> None:
2511+
for error_info in iter_errors.yield_uselessness_error_infos():
2512+
self.fail(*error_info[:2], code=error_info[2])
2513+
for types, context in iter_errors.yield_revealed_type_infos():
2514+
self.reveal_type(mypy.typeops.make_simplified_union(types), context)
2515+
24842516

24852517
def quote_type_string(type_string: str) -> str:
24862518
"""Quotes a type representation for use in messages."""

mypy/plugins/constants.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Constant definitions for plugins kept here to help with import cycles."""
2+
3+
from typing import Final
4+
5+
from mypy.semanal_enum import ENUM_BASES
6+
7+
SINGLEDISPATCH_TYPE: Final = "functools._SingleDispatchCallable"
8+
SINGLEDISPATCH_REGISTER_METHOD: Final = f"{SINGLEDISPATCH_TYPE}.register"
9+
SINGLEDISPATCH_CALLABLE_CALL_METHOD: Final = f"{SINGLEDISPATCH_TYPE}.__call__"
10+
SINGLEDISPATCH_REGISTER_RETURN_CLASS: Final = "_SingleDispatchRegisterCallable"
11+
SINGLEDISPATCH_REGISTER_CALLABLE_CALL_METHOD: Final = (
12+
f"functools.{SINGLEDISPATCH_REGISTER_RETURN_CLASS}.__call__"
13+
)
14+
15+
ENUM_NAME_ACCESS: Final = {f"{prefix}.name" for prefix in ENUM_BASES} | {
16+
f"{prefix}._name_" for prefix in ENUM_BASES
17+
}
18+
ENUM_VALUE_ACCESS: Final = {f"{prefix}.value" for prefix in ENUM_BASES} | {
19+
f"{prefix}._value_" for prefix in ENUM_BASES
20+
}

0 commit comments

Comments
 (0)