Skip to content

Commit 165cbc4

Browse files
committed
Do not overuse current_node_deferred flag
1 parent 4fb187f commit 165cbc4

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

mypy/checker.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,11 +1512,7 @@ def check_func_def(
15121512
)
15131513

15141514
if self.options.warn_no_return:
1515-
if (
1516-
not self.current_node_deferred
1517-
and not isinstance(return_type, (NoneType, AnyType))
1518-
and show_error
1519-
):
1515+
if not isinstance(return_type, (NoneType, AnyType)) and show_error:
15201516
# Control flow fell off the end of a function that was
15211517
# declared to return a non-None type.
15221518
if isinstance(return_type, UninhabitedType):
@@ -3128,7 +3124,6 @@ def should_report_unreachable_issues(self) -> bool:
31283124
return (
31293125
self.in_checked_function()
31303126
and self.options.warn_unreachable
3131-
and not self.current_node_deferred
31323127
and not self.binder.is_unreachable_warning_suppressed()
31333128
)
31343129

@@ -5092,6 +5087,9 @@ def visit_try_without_finally(self, s: TryStmt, try_frame: bool) -> None:
50925087
# To support local variables, we make this a definition line,
50935088
# causing assignment to set the variable's type.
50945089
var.is_inferred_def = True
5090+
# In case current node was deferred, start from blank slate.
5091+
if isinstance(var.node, Var):
5092+
var.node.type = None
50955093
self.check_assignment(var, self.temp_node(t, var))
50965094
self.accept(s.handlers[i])
50975095
var = s.vars[i]

mypy/checkexpr.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2865,7 +2865,7 @@ def infer_overload_return_type(
28652865
matches: list[CallableType] = []
28662866
return_types: list[Type] = []
28672867
inferred_types: list[Type] = []
2868-
args_contain_any = any(map(has_any_type, arg_types))
2868+
args_contain_any = any(has_any_type(a, include_special_form=True) for a in arg_types)
28692869
type_maps: list[dict[Expression, Type]] = []
28702870

28712871
for typ in plausible_targets:
@@ -5977,7 +5977,7 @@ def accept(
59775977
):
59785978
self.msg.disallowed_any_type(typ, node)
59795979

5980-
if not self.chk.in_checked_function() or self.chk.current_node_deferred:
5980+
if not self.chk.in_checked_function():
59815981
result: Type = AnyType(TypeOfAny.unannotated)
59825982
else:
59835983
result = typ
@@ -6311,19 +6311,29 @@ def has_abstract_type(self, caller_type: ProperType, callee_type: ProperType) ->
63116311
)
63126312

63136313

6314-
def has_any_type(t: Type, ignore_in_type_obj: bool = False) -> bool:
6314+
def has_any_type(
6315+
t: Type, ignore_in_type_obj: bool = False, include_special_form: bool = False
6316+
) -> bool:
63156317
"""Whether t contains an Any type"""
6316-
return t.accept(HasAnyType(ignore_in_type_obj))
6318+
return t.accept(HasAnyType(ignore_in_type_obj, include_special_form))
63176319

63186320

63196321
class HasAnyType(types.BoolTypeQuery):
6320-
def __init__(self, ignore_in_type_obj: bool) -> None:
6322+
def __init__(self, ignore_in_type_obj: bool, include_special_form: bool) -> None:
63216323
super().__init__(types.ANY_STRATEGY)
63226324
self.ignore_in_type_obj = ignore_in_type_obj
6325+
self.include_special_form = include_special_form
63236326

63246327
def visit_any(self, t: AnyType) -> bool:
6328+
if self.include_special_form:
6329+
return True
63256330
return t.type_of_any != TypeOfAny.special_form # special forms are not real Any types
63266331

6332+
def visit_tuple_type(self, t: TupleType) -> bool:
6333+
# TODO: should we use tuple_fallback() in (Bool)TypeQuery itself?
6334+
# Using partial_fallback is error prone as it may be bogus (hence the name).
6335+
return self.query_types(t.items + [tuple_fallback(t)])
6336+
63276337
def visit_callable_type(self, t: CallableType) -> bool:
63286338
if self.ignore_in_type_obj and t.is_type_obj():
63296339
return False
@@ -6547,7 +6557,7 @@ def any_causes_overload_ambiguity(
65476557
# We ignore Anys in type object callables as ambiguity
65486558
# creators, since that can lead to falsely claiming ambiguity
65496559
# for overloads between Type and Callable.
6550-
if has_any_type(arg_type, ignore_in_type_obj=True):
6560+
if has_any_type(arg_type, ignore_in_type_obj=True, include_special_form=True):
65516561
matching_formals_unfiltered = [
65526562
(item_idx, lookup[arg_idx])
65536563
for item_idx, lookup in enumerate(actual_to_formal)

mypy/plugins/proper_plugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ def isinstance_proper_hook(ctx: FunctionContext) -> Type:
5858

5959
right = get_proper_type(ctx.arg_types[1][0])
6060
for arg in ctx.arg_types[0]:
61+
p_arg = get_proper_type(arg)
6162
if (
62-
is_improper_type(arg) or isinstance(get_proper_type(arg), AnyType)
63+
is_improper_type(arg) or isinstance(p_arg, AnyType) and not p_arg.is_special_form
6364
) and is_dangerous_target(right):
6465
if is_special_target(right):
6566
return ctx.default_return_type

mypy/types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,15 @@ def __init__(
11601160
def is_from_error(self) -> bool:
11611161
return self.type_of_any == TypeOfAny.from_error
11621162

1163+
@property
1164+
def is_special_form(self) -> bool:
1165+
if self.type_of_any == TypeOfAny.special_form:
1166+
return True
1167+
if self.type_of_any == TypeOfAny.from_another_any:
1168+
assert self.source_any is not None
1169+
return self.source_any.type_of_any == TypeOfAny.special_form
1170+
return False
1171+
11631172
def accept(self, visitor: TypeVisitor[T]) -> T:
11641173
return visitor.visit_any(self)
11651174

test-data/unit/check-overloading.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5219,7 +5219,7 @@ _R = TypeVar('_R')
52195219
@overload
52205220
def partial(__func: Callable[[_T], _S], __arg: _T) -> Callable[[], _S]: ...
52215221
@overload
5222-
def partial(__func: Callable[[_T, _S], _S], __arg: _T) -> Callable[[_S], _R]: ...
5222+
def partial(__func: Callable[[_T, _S], _R], __arg: _T) -> Callable[[_S], _R]: ...
52235223
def partial(*args: Any) -> Any:
52245224
pass
52255225

0 commit comments

Comments
 (0)