Skip to content

Commit 4aff6a2

Browse files
PGrayCSCopilot
andcommitted
Don't cache partial protocol checks that skip __call__ (fixes #22151)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent b47311c commit 4aff6a2

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎mypy/subtypes.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,9 @@ def f(self) -> A: ...
13811381
if IS_CLASS_OR_STATIC in superflags and IS_CLASS_OR_STATIC not in subflags:
13821382
return False
13831383

1384+
if skip:
1385+
# The result is only valid for the partial check, don't pollute the subtype cache.
1386+
return True
13841387
if not proper_subtype:
13851388
# Nominal check currently ignores arg names, but __call__ is special for protocols
13861389
ignore_names = right.type.protocol_members != ["__call__"]

‎test-data/unit/check-protocols.test‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4819,3 +4819,25 @@ class Bad(Generic[T]):
48194819
bad: Bad[str] = Bad()
48204820
fail: SupportsFoo = bad # E: Incompatible types in assignment (expression has type "Bad[str]", variable has type "SupportsFoo")
48214821
[builtins fixtures/tuple.pyi]
4822+
4823+
[case testCallbackProtocolUnionInferenceOrderIndependent]
4824+
from typing import Protocol, TypeVar
4825+
4826+
T = TypeVar("T")
4827+
PT = TypeVar("PT", contravariant=True)
4828+
4829+
class OneArg(Protocol[PT]):
4830+
def __call__(self, x: PT, /) -> None: ...
4831+
4832+
class TwoArgs(Protocol[PT]):
4833+
def __call__(self, x: PT, y: int, /) -> None: ...
4834+
4835+
def first_arg(fn: OneArg[T] | TwoArgs[T]) -> T: ...
4836+
4837+
def one(x: int) -> None: ...
4838+
def two(x: str, y: int) -> None: ...
4839+
4840+
reveal_type(first_arg(one)) # N: Revealed type is "builtins.int"
4841+
reveal_type(first_arg(two)) # N: Revealed type is "builtins.str"
4842+
reveal_type(first_arg(one)) # N: Revealed type is "builtins.int"
4843+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)