Skip to content

Commit 75e4e22

Browse files
committed
Just expand self type together with the callable
1 parent 00ffdcb commit 75e4e22

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

mypy/checkexpr.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,7 +2916,6 @@ def infer_overload_return_type(
29162916
# If we have a selftype overload, it should contribute to `any_causes_overload_ambiguity`
29172917
# check. Pretend that we're checking `Foo.func(instance, ...)` instead of
29182918
# `instance.func(...)`.
2919-
p_object_type = get_proper_type(object_type) if object_type is not None else None
29202919

29212920
def is_trivial_self(t: CallableType) -> bool:
29222921
if isinstance(t.definition, FuncDef):
@@ -2926,8 +2925,8 @@ def is_trivial_self(t: CallableType) -> bool:
29262925
return False
29272926

29282927
prepend_self = (
2929-
isinstance(p_object_type, Instance)
2930-
and has_any_type(p_object_type)
2928+
object_type is not None
2929+
and has_any_type(object_type)
29312930
and any(
29322931
typ.is_bound and typ.original_self_type is not None and not is_trivial_self(typ)
29332932
for typ in plausible_targets
@@ -2952,16 +2951,15 @@ def maybe_bind_self(t: Type) -> Type:
29522951
if prepend_self:
29532952
param = typ.original_self_type
29542953
assert param is not None, "Overload bound only partially?"
2955-
assert isinstance(p_object_type, Instance)
2956-
param = expand_type_by_instance(param, p_object_type)
2954+
typ = typ.copy_modified(
2955+
arg_types=[param] + typ.arg_types,
2956+
arg_kinds=[ARG_POS] + typ.arg_kinds,
2957+
arg_names=[None, *typ.arg_names],
2958+
is_bound=False,
2959+
original_self_type=None,
2960+
)
29572961
ret_type, infer_type = self.check_call(
2958-
callee=typ.copy_modified(
2959-
arg_types=[param] + typ.arg_types,
2960-
arg_kinds=[ARG_POS] + typ.arg_kinds,
2961-
arg_names=[None, *typ.arg_names],
2962-
is_bound=False,
2963-
original_self_type=None,
2964-
),
2962+
callee=typ,
29652963
args=args,
29662964
arg_kinds=arg_kinds,
29672965
arg_names=arg_names,

mypy/expandtype.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,11 @@ def visit_callable_type(self, t: CallableType) -> CallableType:
381381
arg_kinds=t.arg_kinds[:-2] + repl.arg_kinds,
382382
arg_names=t.arg_names[:-2] + repl.arg_names,
383383
ret_type=t.ret_type.accept(self),
384+
original_self_type=(
385+
t.original_self_type.accept(self)
386+
if t.original_self_type is not None
387+
else None
388+
),
384389
type_guard=(t.type_guard.accept(self) if t.type_guard is not None else None),
385390
type_is=(t.type_is.accept(self) if t.type_is is not None else None),
386391
imprecise_arg_kinds=(t.imprecise_arg_kinds or repl.imprecise_arg_kinds),
@@ -420,6 +425,9 @@ def visit_callable_type(self, t: CallableType) -> CallableType:
420425
expanded = t.copy_modified(
421426
arg_types=arg_types,
422427
ret_type=t.ret_type.accept(self),
428+
original_self_type=(
429+
t.original_self_type.accept(self) if t.original_self_type is not None else None
430+
),
423431
type_guard=(t.type_guard.accept(self) if t.type_guard is not None else None),
424432
type_is=(t.type_is.accept(self) if t.type_is is not None else None),
425433
)

mypy/typetraverser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ def visit_callable_type(self, t: CallableType, /) -> None:
8787
t.ret_type.accept(self)
8888
t.fallback.accept(self)
8989

90+
if t.original_self_type is not None:
91+
t.original_self_type.accept(self)
92+
9093
if t.type_guard is not None:
9194
t.type_guard.accept(self)
92-
9395
if t.type_is is not None:
9496
t.type_is.accept(self)
9597

0 commit comments

Comments
 (0)