Skip to content

Commit e7b431b

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

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

mypy/checkexpr.py

Lines changed: 11 additions & 17 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):
@@ -2925,13 +2924,9 @@ def is_trivial_self(t: CallableType) -> bool:
29252924
return t.definition.func.is_trivial_self
29262925
return False
29272926

2928-
prepend_self = (
2929-
isinstance(p_object_type, Instance)
2930-
and has_any_type(p_object_type)
2931-
and any(
2932-
typ.is_bound and typ.original_self_type is not None and not is_trivial_self(typ)
2933-
for typ in plausible_targets
2934-
)
2927+
prepend_self = has_any_type(object_type) and any(
2928+
typ.is_bound and typ.original_self_type is not None and not is_trivial_self(typ)
2929+
for typ in plausible_targets
29352930
)
29362931
if prepend_self:
29372932
assert object_type is not None
@@ -2952,16 +2947,15 @@ def maybe_bind_self(t: Type) -> Type:
29522947
if prepend_self:
29532948
param = typ.original_self_type
29542949
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)
2950+
typ = typ.copy_modified(
2951+
arg_types=[param] + typ.arg_types,
2952+
arg_kinds=[ARG_POS] + typ.arg_kinds,
2953+
arg_names=[None, *typ.arg_names],
2954+
is_bound=False,
2955+
original_self_type=None,
2956+
)
29572957
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-
),
2958+
callee=typ,
29652959
args=args,
29662960
arg_kinds=arg_kinds,
29672961
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)