Skip to content
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
39 changes: 28 additions & 11 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ def __init__(
] = {}
self.freshen_cache: dict[tuple[Context, CallableType], CallableType] = {}
self.in_lambda_expr = False
# Classes whose "__call__" is currently being resolved in check_call(). used to
# detect a "__call__" attribute whose type leads back to the same class
self.resolving_call_types: set[TypeInfo] = set()

self._literal_true: Instance | None = None
self._literal_false: Instance | None = None
Expand Down Expand Up @@ -507,7 +510,13 @@ def visit_call_expr(self, e: CallExpr, allow_none_return: bool = False) -> Type:
self.visit_call_expr_inner(e)
# It's really a special form that only looks like a call.
return self.accept(e.analyzed, self.type_context[-1])
return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
# Nested call expressions (e.g. in arguments) resolve "__call__" independently
outer_resolving_call_types = self.resolving_call_types
self.resolving_call_types = set()
try:
return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
finally:
self.resolving_call_types = outer_resolving_call_types

def refers_to_typeddict(self, base: Expression) -> bool:
if not isinstance(base, RefExpr):
Expand Down Expand Up @@ -1624,6 +1633,10 @@ def check_call(
elif isinstance(callee, UnionType):
return self.check_union_call(callee, args, arg_kinds, arg_names, context)
elif isinstance(callee, Instance):
if callee.type in self.resolving_call_types:
# "__call__" is (transitivly) declared with the type of its own class
# e.g. "__call__: C" in class C. Following it would recurse forever
return self.msg.not_callable(callee, context), AnyType(TypeOfAny.from_error)
call_function = analyze_member_access(
"__call__",
callee,
Expand All @@ -1640,16 +1653,20 @@ def check_call(
call_function = self.transform_callee_type(
callable_name, call_function, args, arg_kinds, context, arg_names, callee
)
result = self.check_call(
call_function,
args,
arg_kinds,
context,
arg_names,
callable_node,
callable_name,
callee,
)
self.resolving_call_types.add(callee.type)
try:
result = self.check_call(
call_function,
args,
arg_kinds,
context,
arg_names,
callable_node,
callable_name,
callee,
)
finally:
self.resolving_call_types.discard(callee.type)
if callable_node:
# check_call() stored "call_function" as the type, which is incorrect.
# Override the type.
Expand Down
39 changes: 39 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -3409,6 +3409,45 @@ if int():
if int():
b = a(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")

[case testCallAttributeOfOwnTypeNoCrash]
from typing import Generic, Optional, TypeVar
from typing_extensions import Self

T = TypeVar("T")

class A:
__call__: A
class B:
__call__: Optional[B]
class S:
__call__: Optional[Self]
class G(Generic[T]):
__call__: G[list[T]]
class X:
__call__: Y
class Y:
__call__: X

def f(a: A, b: B, s: S, g: G[int], x: X) -> None:
a() # E: "A" not callable
b() # E: "B" not callable \
# E: "None" not callable
s() # E: "S" not callable \
# E: "None" not callable
g() # E: "G[list[int]]" not callable
x() # E: "X" not callable
[builtins fixtures/list.pyi]

[case testCallAttributeOfCallableTypeNestedCall]
class D:
def __call__(self, a: int) -> int: return a
class E:
__call__: D

def f(d: D, e: E) -> None:
reveal_type(d(d(1))) # N: Revealed type is "builtins.int"
reveal_type(e(e(1))) # N: Revealed type is "builtins.int"


-- __new__
-- --------
Expand Down
Loading