Skip to content

Tighten metaclass __call__ handling in protocols #19191

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

Merged
merged 1 commit into from
Jun 1, 2025
Merged
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
4 changes: 2 additions & 2 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,8 +1066,8 @@ def infer_constraints_from_protocol_members(
inst, erase_typevars(temp), ignore_pos_arg_names=True
):
continue
# This exception matches the one in subtypes.py, see PR #14121 for context.
if member == "__call__" and instance.type.is_metaclass():
# This exception matches the one in typeops.py, see PR #14121 for context.
if member == "__call__" and instance.type.is_metaclass(precise=True):
continue
res.extend(infer_constraints(temp, inst, self.direction))
if mypy.subtypes.IS_SETTABLE in mypy.subtypes.get_member_flags(member, protocol):
Expand Down
4 changes: 2 additions & 2 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3359,11 +3359,11 @@ def calculate_metaclass_type(self) -> mypy.types.Instance | None:
return c
return None

def is_metaclass(self) -> bool:
def is_metaclass(self, *, precise: bool = False) -> bool:
return (
self.has_base("builtins.type")
or self.fullname == "abc.ABCMeta"
or self.fallback_to_any
or (self.fallback_to_any and not precise)
)

def has_base(self, fullname: str) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ def named_type(fullname: str) -> Instance:

return type_object_type(left.type, named_type)

if member == "__call__" and left.type.is_metaclass():
if member == "__call__" and left.type.is_metaclass(precise=True):
# Special case: we want to avoid falling back to metaclass __call__
# if constructor signature didn't match, this can cause many false negatives.
return None
Expand Down
22 changes: 22 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -4505,3 +4505,25 @@ def bad() -> Proto:
class Impl:
@defer
def f(self) -> int: ...

[case testInferCallableProtoWithAnySubclass]
from typing import Any, Generic, Protocol, TypeVar

T = TypeVar("T", covariant=True)

Unknown: Any
class Mock(Unknown):
def __init__(self, **kwargs: Any) -> None: ...
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...

class Factory(Protocol[T]):
def __call__(self, **kwargs: Any) -> T: ...


class Test(Generic[T]):
def __init__(self, f: Factory[T]) -> None:
...

t = Test(Mock())
reveal_type(t) # N: Revealed type is "__main__.Test[Any]"
[builtins fixtures/dict.pyi]