Skip to content

disable_errors() and enable_errors() function pair refactored #9701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1992,10 +1992,9 @@ def is_raising_or_empty(self, s: Statement) -> bool:
if isinstance(s.expr, EllipsisExpr):
return True
elif isinstance(s.expr, CallExpr):
self.expr_checker.msg.disable_errors()
typ = get_proper_type(self.expr_checker.accept(
s.expr, allow_none_return=True, always_allow_any=True))
self.expr_checker.msg.enable_errors()
with self.expr_checker.msg.disable_errors():
typ = get_proper_type(self.expr_checker.accept(
s.expr, allow_none_return=True, always_allow_any=True))

if isinstance(typ, UninhabitedType):
return True
Expand Down Expand Up @@ -3046,14 +3045,13 @@ def check_member_assignment(self, instance_type: Type, attribute_type: Type,

# Here we just infer the type, the result should be type-checked like a normal assignment.
# For this we use the rvalue as type context.
self.msg.disable_errors()
_, inferred_dunder_set_type = self.expr_checker.check_call(
dunder_set_type,
[TempNode(instance_type, context=context), rvalue],
[nodes.ARG_POS, nodes.ARG_POS],
context, object_type=attribute_type,
callable_name=callable_name)
self.msg.enable_errors()
with self.msg.disable_errors():
_, inferred_dunder_set_type = self.expr_checker.check_call(
dunder_set_type,
[TempNode(instance_type, context=context), rvalue],
[nodes.ARG_POS, nodes.ARG_POS],
context, object_type=attribute_type,
callable_name=callable_name)

# And now we type check the call second time, to show errors related
# to wrong arguments count, etc.
Expand Down
19 changes: 8 additions & 11 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,12 +859,11 @@ def check_union_call_expr(self, e: CallExpr, object_type: UnionType, member: str
res = [] # type: List[Type]
for typ in object_type.relevant_items():
# Member access errors are already reported when visiting the member expression.
self.msg.disable_errors()
item = analyze_member_access(member, typ, e, False, False, False,
self.msg, original_type=object_type, chk=self.chk,
in_literal_context=self.is_literal_context(),
self_type=typ)
self.msg.enable_errors()
with self.msg.disable_errors():
item = analyze_member_access(member, typ, e, False, False, False,
self.msg, original_type=object_type, chk=self.chk,
in_literal_context=self.is_literal_context(),
self_type=typ)
narrowed = self.narrow_type_from_binder(e.callee, item, skip_non_overlapping=True)
if narrowed is None:
continue
Expand Down Expand Up @@ -1197,12 +1196,10 @@ def infer_function_type_arguments(self, callee_type: CallableType,
# due to partial available context information at this time, but
# these errors can be safely ignored as the arguments will be
# inferred again later.
self.msg.disable_errors()

arg_types = self.infer_arg_types_in_context(
callee_type, args, arg_kinds, formal_to_actual)
with self.msg.disable_errors():

self.msg.enable_errors()
arg_types = self.infer_arg_types_in_context(
callee_type, args, arg_kinds, formal_to_actual)

arg_pass_nums = self.get_arg_infer_passes(
callee_type.arg_types, formal_to_actual, len(args))
Expand Down
4 changes: 4 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import re
import difflib
from textwrap import dedent
from contextlib import contextmanager

from typing import cast, List, Dict, Any, Sequence, Iterable, Tuple, Set, Optional, Union
from typing_extensions import Final
Expand Down Expand Up @@ -136,8 +137,11 @@ def add_errors(self, messages: 'MessageBuilder') -> None:
for info in errs:
self.errors.add_error_info(info)

@contextmanager
def disable_errors(self) -> None:
self.disable_count += 1
yield
self.disable_count -= 1

def enable_errors(self) -> None:
self.disable_count -= 1
Expand Down