Skip to content

Commit a1d7781

Browse files
authored
Do not call best_matches() unless we show the message (#21307)
This function is very slow. While playing with parallel checking for home-assistant/core I found that we spend ~30% in this function (compiled, 12 workers).
1 parent 643c2ac commit a1d7781

5 files changed

Lines changed: 23 additions & 14 deletions

File tree

mypy/build.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3921,6 +3921,7 @@ def module_not_found(
39213921
if (
39223922
reason == ModuleNotFoundReason.NOT_FOUND
39233923
and not errors.prefer_simple_messages()
3924+
and errors.is_error_code_enabled(code)
39243925
and line not in errors.ignored_lines.get(caller_state.xpath, {})
39253926
):
39263927
top_level_target = target.split(".")[0]

mypy/checker.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7578,7 +7578,11 @@ def check_subtype(
75787578
if isinstance(msg, str):
75797579
msg = ErrorMessage(msg, code=code)
75807580

7581-
if self.msg.prefer_simple_messages():
7581+
if (
7582+
self.msg.prefer_simple_messages()
7583+
or code is not None
7584+
and not self.errors.is_error_code_enabled(code)
7585+
):
75827586
self.fail(msg, context) # Fast path -- skip all fancy logic
75837587
return False
75847588

mypy/errors.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -876,12 +876,8 @@ def is_ignored_error(self, line: int, info: ErrorInfo, ignores: dict[int, list[s
876876
return False
877877

878878
def is_error_code_enabled(self, error_code: ErrorCode) -> bool:
879-
if self.options:
880-
current_mod_disabled = self.options.disabled_error_codes
881-
current_mod_enabled = self.options.enabled_error_codes
882-
else:
883-
current_mod_disabled = set()
884-
current_mod_enabled = set()
879+
current_mod_disabled = self.options.disabled_error_codes
880+
current_mod_enabled = self.options.enabled_error_codes
885881

886882
if error_code in current_mod_disabled:
887883
return False

mypy/messages.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,9 +1007,12 @@ def unexpected_keyword_argument(
10071007
matching_type_args.append(callee_arg_name)
10081008
else:
10091009
not_matching_type_args.append(callee_arg_name)
1010-
matches = best_matches(name, matching_type_args, n=3)
1011-
if not matches:
1012-
matches = best_matches(name, not_matching_type_args, n=3)
1010+
if not self.prefer_simple_messages():
1011+
matches = best_matches(name, matching_type_args, n=3)
1012+
if not matches:
1013+
matches = best_matches(name, not_matching_type_args, n=3)
1014+
else:
1015+
matches = []
10131016
self.unexpected_keyword_argument_for_function(
10141017
for_function(callee), name, context, matches=matches
10151018
)
@@ -1128,9 +1131,12 @@ def no_variant_matches_arguments(
11281131
if item_has_type_match:
11291132
has_matching_variant = True
11301133

1131-
matches = best_matches(kwarg_name, matching_type_args, n=3)
1132-
if not matches:
1133-
matches = best_matches(kwarg_name, not_matching_type_args, n=3)
1134+
if not self.prefer_simple_messages():
1135+
matches = best_matches(kwarg_name, matching_type_args, n=3)
1136+
if not matches:
1137+
matches = best_matches(kwarg_name, not_matching_type_args, n=3)
1138+
else:
1139+
matches = []
11341140

11351141
if matches:
11361142
kwargs_with_suggestions.append((kwarg_name, matches))

mypy/semanal.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3135,7 +3135,9 @@ def report_missing_module_attribute(
31353135
f'Module "{import_id}" does not explicitly export attribute "{source_id}"'
31363136
)
31373137
elif not (
3138-
self.options.ignore_errors or self.cur_mod_node.path in self.errors.ignored_files
3138+
self.options.ignore_errors
3139+
or self.cur_mod_node.path in self.errors.ignored_files
3140+
or self.errors.prefer_simple_messages()
31393141
):
31403142
alternatives = set(module.names.keys()).difference({source_id})
31413143
matches = best_matches(source_id, alternatives, n=3)

0 commit comments

Comments
 (0)