Skip to content
Open
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: 3 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,9 @@ def f(self) -> A: ...
subtype_context=SubtypeContext(ignore_pos_arg_names=ignore_names),
proper_subtype=proper_subtype,
)
type_state.record_subtype_cache_entry(subtype_kind, left, right)
# A partial protocol check does not prove full subtype compatibility.
if not skip:
type_state.record_subtype_cache_entry(subtype_kind, left, right)
return True


Expand Down
40 changes: 40 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -2730,6 +2730,46 @@ def test(func: A[T, S]) -> Tuple[T, S]: ...
reveal_type(test(f)) # N: Revealed type is "tuple[builtins.str, builtins.int]"
[builtins fixtures/tuple.pyi]

[case testUnionOfCallbackProtocolsInference]
from typing import Protocol, TypeVar

T = TypeVar("T")
PT = TypeVar("PT", contravariant=True)

class OneArg(Protocol[PT]):
def __call__(self, x: PT, /) -> None: ...

class TwoArgs(Protocol[PT]):
def __call__(self, x: PT, y: int, /) -> None: ...

def first_arg(fn: OneArg[T] | TwoArgs[T]) -> T: ...

def one(x: int) -> None: ...
def two(x: str, y: int) -> None: ...

reveal_type(first_arg(one)) # N: Revealed type is "builtins.int"
reveal_type(first_arg(two)) # N: Revealed type is "builtins.str"

[case testUnionOfCallbackProtocolsInferenceReversed]
from typing import Protocol, TypeVar

T = TypeVar("T")
PT = TypeVar("PT", contravariant=True)

class OneArg(Protocol[PT]):
def __call__(self, x: PT, /) -> None: ...

class TwoArgs(Protocol[PT]):
def __call__(self, x: PT, y: int, /) -> None: ...

def first_arg(fn: OneArg[T] | TwoArgs[T]) -> T: ...

def one(x: int) -> None: ...
def two(x: str, y: int) -> None: ...

reveal_type(first_arg(two)) # N: Revealed type is "builtins.str"
reveal_type(first_arg(one)) # N: Revealed type is "builtins.int"

[case testProtocolsAlwaysABCs]
from typing import Protocol

Expand Down
Loading