Skip to content

Commit 806266c

Browse files
committed
Opt out of unnecessary checks in checker.py
Note that there are a few places where unreachable code can happen where this still doesn't help. I'm not sure what to do about them.
1 parent edbd57b commit 806266c

2 files changed

Lines changed: 9 additions & 16 deletions

File tree

mypy/checker.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ def check_first_pass(self) -> None:
529529
self.msg.unreachable_statement(d)
530530
break
531531
else:
532-
self.accept(d)
532+
self.accept_with_delayed_errors(d)
533533

534534
assert not self.current_node_deferred
535535

@@ -637,14 +637,17 @@ def handle_cannot_determine_type(self, name: str, context: Context) -> None:
637637
else:
638638
self.msg.cannot_determine_type(name, context)
639639

640-
def accept(self, stmt: Statement) -> None:
641-
"""Type check a node in the given type context."""
640+
def accept_with_delayed_errors(self, stmt: Statement) -> None:
642641
curr_module = self.scope.stack[0]
643642
if isinstance(curr_module, MypyFile):
644643
key = (curr_module.fullname, stmt.line, stmt.column)
645644
if key in self.semanal_delayed_errors:
646645
self.msg.add_errors(self.semanal_delayed_errors[key])
647646

647+
self.accept(stmt)
648+
649+
def accept(self, stmt: Statement) -> None:
650+
"""Type check a node in the given type context."""
648651
try:
649652
stmt.accept(self)
650653
except Exception as err:
@@ -1235,16 +1238,6 @@ def check_func_item(
12351238
"""
12361239
self.dynamic_funcs.append(defn.is_dynamic() and not type_override)
12371240

1238-
# top-level function definitions are one of the main
1239-
# things errors can be associated with, and are sometimes masked
1240-
# e.g. by a decorator. it's better to make sure to flush any errors
1241-
# just in case.
1242-
curr_module = self.scope.stack[0]
1243-
if isinstance(curr_module, MypyFile):
1244-
key = (curr_module.fullname, defn.line, defn.column)
1245-
if key in self.semanal_delayed_errors:
1246-
self.msg.add_errors(self.semanal_delayed_errors[key])
1247-
12481241
enclosing_node_deferred = self.current_node_deferred
12491242
with self.enter_partial_types(is_function=True):
12501243
typ = self.function_type(defn)
@@ -3174,7 +3167,7 @@ def visit_block(self, b: Block) -> None:
31743167
self.msg.unreachable_statement(s)
31753168
break
31763169
else:
3177-
self.accept(s)
3170+
self.accept_with_delayed_errors(s)
31783171
# Clear expression cache after each statement to avoid unlimited growth.
31793172
self.expr_checker.expr_cache.clear()
31803173

mypy/checkexpr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5495,8 +5495,8 @@ def visit_lambda_expr(self, e: LambdaExpr) -> Type:
54955495
# Lambdas can have more than one element in body,
54965496
# when we add "fictional" AssignmentStatement nodes, like in:
54975497
# `lambda (a, b): a`
5498-
for stmt in e.body.body:
5499-
self.chk.accept(stmt)
5498+
self.chk.accept(e.body)
5499+
55005500
# Only type check the return expression, not the return statement.
55015501
# There's no useful type context.
55025502
ret_type = self.accept(e.expr(), allow_none_return=True)

0 commit comments

Comments
 (0)