Skip to content

Commit 57f83ea

Browse files
committed
Use context managers for disable_errors()
1 parent ac61921 commit 57f83ea

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

mypy/checker.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,10 +1992,9 @@ def is_raising_or_empty(self, s: Statement) -> bool:
19921992
if isinstance(s.expr, EllipsisExpr):
19931993
return True
19941994
elif isinstance(s.expr, CallExpr):
1995-
self.expr_checker.msg.disable_errors()
1996-
typ = get_proper_type(self.expr_checker.accept(
1997-
s.expr, allow_none_return=True, always_allow_any=True))
1998-
self.expr_checker.msg.enable_errors()
1995+
with self.expr_checker.msg.disable_errors():
1996+
typ = get_proper_type(self.expr_checker.accept(
1997+
s.expr, allow_none_return=True, always_allow_any=True))
19991998

20001999
if isinstance(typ, UninhabitedType):
20012000
return True
@@ -3047,14 +3046,13 @@ def check_member_assignment(self, instance_type: Type, attribute_type: Type,
30473046
# For non-overloaded setters, the result should be type-checked like a regular assignment.
30483047
# Hence, we first only try to infer the type by using the rvalue as type context.
30493048
type_context = rvalue
3050-
self.msg.disable_errors()
3051-
_, inferred_dunder_set_type = self.expr_checker.check_call(
3052-
dunder_set_type,
3053-
[TempNode(instance_type, context=context), type_context],
3054-
[nodes.ARG_POS, nodes.ARG_POS],
3055-
context, object_type=attribute_type,
3056-
callable_name=callable_name)
3057-
self.msg.enable_errors()
3049+
with self.msg.disable_errors():
3050+
_, inferred_dunder_set_type = self.expr_checker.check_call(
3051+
dunder_set_type,
3052+
[TempNode(instance_type, context=context), type_context],
3053+
[nodes.ARG_POS, nodes.ARG_POS],
3054+
context, object_type=attribute_type,
3055+
callable_name=callable_name)
30583056

30593057
# And now we in fact type check the call, to show errors related to wrong arguments
30603058
# count, etc., replacing the type context for non-overloaded setters only.

mypy/checkexpr.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -864,12 +864,12 @@ def check_union_call_expr(self, e: CallExpr, object_type: UnionType, member: str
864864
res = [] # type: List[Type]
865865
for typ in object_type.relevant_items():
866866
# Member access errors are already reported when visiting the member expression.
867-
self.msg.disable_errors()
868-
item = analyze_member_access(member, typ, e, False, False, False,
869-
self.msg, original_type=object_type, chk=self.chk,
870-
in_literal_context=self.is_literal_context(),
871-
self_type=typ)
872-
self.msg.enable_errors()
867+
with self.msg.disable_errors():
868+
item = analyze_member_access(member, typ, e, False, False, False,
869+
self.msg, original_type=object_type, chk=self.chk,
870+
in_literal_context=self.is_literal_context(),
871+
self_type=typ)
872+
873873
narrowed = self.narrow_type_from_binder(e.callee, item, skip_non_overlapping=True)
874874
if narrowed is None:
875875
continue
@@ -1202,12 +1202,9 @@ def infer_function_type_arguments(self, callee_type: CallableType,
12021202
# due to partial available context information at this time, but
12031203
# these errors can be safely ignored as the arguments will be
12041204
# inferred again later.
1205-
self.msg.disable_errors()
1206-
1207-
arg_types = self.infer_arg_types_in_context(
1208-
callee_type, args, arg_kinds, formal_to_actual)
1209-
1210-
self.msg.enable_errors()
1205+
with self.msg.disable_errors():
1206+
arg_types = self.infer_arg_types_in_context(
1207+
callee_type, args, arg_kinds, formal_to_actual)
12111208

12121209
arg_pass_nums = self.get_arg_infer_passes(
12131210
callee_type.arg_types, formal_to_actual, len(args))
@@ -2809,12 +2806,16 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
28092806
left_map = None
28102807

28112808
if right_map is None:
2812-
self.msg.disable_errors()
2813-
try:
2814-
right_type = self.analyze_cond_branch(right_map, e.right, left_type)
2815-
finally:
2816-
if right_map is None:
2817-
self.msg.enable_errors()
2809+
with self.msg.disable_errors():
2810+
try:
2811+
right_type = self.analyze_cond_branch(right_map, e.right, left_type)
2812+
finally:
2813+
pass
2814+
else:
2815+
try:
2816+
right_type = self.analyze_cond_branch(right_map, e.right, left_type)
2817+
finally:
2818+
pass
28182819

28192820
if right_map is None:
28202821
# The boolean expression is statically known to be the left value

mypy/messages.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
import re
1414
import difflib
1515
from textwrap import dedent
16+
from contextlib import contextmanager
1617

17-
from typing import cast, List, Dict, Any, Sequence, Iterable, Tuple, Set, Optional, Union
18+
from typing import cast, List, Dict, Any, Sequence, Iterable, Tuple, Set, Optional, Union, Iterator
1819
from typing_extensions import Final
1920

2021
from mypy.erasetype import erase_type
@@ -136,8 +137,11 @@ def add_errors(self, messages: 'MessageBuilder') -> None:
136137
for info in errs:
137138
self.errors.add_error_info(info)
138139

139-
def disable_errors(self) -> None:
140+
@contextmanager
141+
def disable_errors(self) -> Iterator[None]:
140142
self.disable_count += 1
143+
yield
144+
self.disable_count -= 1
141145

142146
def enable_errors(self) -> None:
143147
self.disable_count -= 1

0 commit comments

Comments
 (0)