Skip to content

Commit e2b7218

Browse files
committed
Convert MessageBuilder.disable_errors to a context manager
1 parent 4da09c7 commit e2b7218

File tree

4 files changed

+29
-37
lines changed

4 files changed

+29
-37
lines changed

mypy/checker.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,10 +1995,9 @@ def is_raising_or_empty(self, s: Statement) -> bool:
19951995
if isinstance(s.expr, EllipsisExpr):
19961996
return True
19971997
elif isinstance(s.expr, CallExpr):
1998-
self.expr_checker.msg.disable_errors()
1999-
typ = get_proper_type(self.expr_checker.accept(
2000-
s.expr, allow_none_return=True, always_allow_any=True))
2001-
self.expr_checker.msg.enable_errors()
1998+
with self.expr_checker.msg.disable_errors():
1999+
typ = get_proper_type(self.expr_checker.accept(
2000+
s.expr, allow_none_return=True, always_allow_any=True))
20022001

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

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

mypy/checkexpr.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Expression type checker. This file is conceptually part of TypeChecker."""
22

33
from mypy.ordered_dict import OrderedDict
4-
from contextlib import contextmanager
4+
from contextlib import contextmanager, nullcontext
55
import itertools
66
from typing import (
77
Any, cast, Dict, Set, List, Tuple, Callable, Union, Optional, Sequence, Iterator
@@ -865,12 +865,11 @@ def check_union_call_expr(self, e: CallExpr, object_type: UnionType, member: str
865865
res = [] # type: List[Type]
866866
for typ in object_type.relevant_items():
867867
# Member access errors are already reported when visiting the member expression.
868-
self.msg.disable_errors()
869-
item = analyze_member_access(member, typ, e, False, False, False,
870-
self.msg, original_type=object_type, chk=self.chk,
871-
in_literal_context=self.is_literal_context(),
872-
self_type=typ)
873-
self.msg.enable_errors()
868+
with self.msg.disable_errors():
869+
item = analyze_member_access(member, typ, e, False, False, False,
870+
self.msg, original_type=object_type, chk=self.chk,
871+
in_literal_context=self.is_literal_context(),
872+
self_type=typ)
874873
narrowed = self.narrow_type_from_binder(e.callee, item, skip_non_overlapping=True)
875874
if narrowed is None:
876875
continue
@@ -1203,12 +1202,9 @@ def infer_function_type_arguments(self, callee_type: CallableType,
12031202
# due to partial available context information at this time, but
12041203
# these errors can be safely ignored as the arguments will be
12051204
# inferred again later.
1206-
self.msg.disable_errors()
1207-
1208-
arg_types = self.infer_arg_types_in_context(
1209-
callee_type, args, arg_kinds, formal_to_actual)
1210-
1211-
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)
12121208

12131209
arg_pass_nums = self.get_arg_infer_passes(
12141210
callee_type.arg_types, formal_to_actual, len(args))
@@ -2815,13 +2811,8 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
28152811
elif e.right_always:
28162812
left_map = None
28172813

2818-
if right_map is None:
2819-
self.msg.disable_errors()
2820-
try:
2814+
with (self.msg.disable_errors() if right_map is None else nullcontext()):
28212815
right_type = self.analyze_cond_branch(right_map, e.right, left_type)
2822-
finally:
2823-
if right_map is None:
2824-
self.msg.enable_errors()
28252816

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

mypy/checkmember.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def analyze_type_type_member_access(name: str,
268268
item = None
269269
fallback = mx.builtin_type('builtins.type')
270270
ignore_messages = mx.msg.copy()
271-
ignore_messages.disable_errors()
271+
ignore_messages.disable_errors().__enter__()
272272
if isinstance(typ.item, Instance):
273273
item = typ.item
274274
elif isinstance(typ.item, AnyType):

mypy/messages.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
Historically we tried to avoid all message string literals in the type
99
checker but we are moving away from this convention.
1010
"""
11+
from contextlib import contextmanager
1112

1213
from mypy.ordered_dict import OrderedDict
1314
import re
1415
import difflib
1516
from textwrap import dedent
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, ContextManager
1819
from typing_extensions import Final
1920

2021
from mypy.erasetype import erase_type
@@ -136,11 +137,13 @@ 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) -> ContextManager[None]:
140142
self.disable_count += 1
141-
142-
def enable_errors(self) -> None:
143-
self.disable_count -= 1
143+
try:
144+
yield
145+
finally:
146+
self.disable_count -= 1
144147

145148
def is_errors(self) -> bool:
146149
return self.errors.is_errors()

0 commit comments

Comments
 (0)