Skip to content

Add UnionType support when incompatible protocol happens #10154

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
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
13 changes: 10 additions & 3 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,16 @@ def incompatible_argument_note(self,
callee_type: ProperType,
context: Context,
code: Optional[ErrorCode]) -> None:
if (isinstance(original_caller_type, (Instance, TupleType, TypedDictType)) and
isinstance(callee_type, Instance) and callee_type.type.is_protocol):
self.report_protocol_problems(original_caller_type, callee_type, context, code=code)
if isinstance(original_caller_type, (Instance, TupleType, TypedDictType)):
if isinstance(callee_type, Instance) and callee_type.type.is_protocol:
self.report_protocol_problems(original_caller_type, callee_type,
context, code=code)
if isinstance(callee_type, UnionType):
for item in callee_type.items:
item = get_proper_type(item)
if isinstance(item, Instance) and item.type.is_protocol:
self.report_protocol_problems(original_caller_type, item,
context, code=code)
if (isinstance(callee_type, CallableType) and
isinstance(original_caller_type, Instance)):
call = find_member('__call__', original_caller_type, original_caller_type,
Expand Down
32 changes: 32 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,38 @@ main:14: note: Got:
main:14: note: def g(self, x: str) -> None
main:14: note: <2 more conflict(s) not shown>

[case testProtocolIncompatibilityWithUnionType]
from typing import Any, Optional, Protocol

class A(Protocol):
def execute(self, statement: Any, *args: Any, **kwargs: Any) -> None: ...

class B(Protocol):
def execute(self, stmt: Any, *args: Any, **kwargs: Any) -> None: ...
def cool(self) -> None: ...

def func1(arg: A) -> None: ...
def func2(arg: Optional[A]) -> None: ...

x: B
func1(x)
func2(x)
[builtins fixtures/tuple.pyi]
[builtins fixtures/dict.pyi]
[out]
main:14: error: Argument 1 to "func1" has incompatible type "B"; expected "A"
main:14: note: Following member(s) of "B" have conflicts:
main:14: note: Expected:
main:14: note: def execute(self, statement: Any, *args: Any, **kwargs: Any) -> None
main:14: note: Got:
main:14: note: def execute(self, stmt: Any, *args: Any, **kwargs: Any) -> None
main:15: error: Argument 1 to "func2" has incompatible type "B"; expected "Optional[A]"
main:15: note: Following member(s) of "B" have conflicts:
main:15: note: Expected:
main:15: note: def execute(self, statement: Any, *args: Any, **kwargs: Any) -> None
main:15: note: Got:
main:15: note: def execute(self, stmt: Any, *args: Any, **kwargs: Any) -> None

[case testDontShowNotesForTupleAndIterableProtocol]
from typing import Iterable, Sequence, Protocol, NamedTuple

Expand Down