Skip to content

Commit 9934278

Browse files
authored
Do not show protocol compatibility note against unpacked sequence or mapping (#19358)
This was discovered in #19294 where an unrelated change produced a weird notice that should not be shown. Current behavior of the added testcase: ``` main.py:10: error: Argument 1 to "foo" has incompatible type "*list[object]"; expected "P" [arg-type] main.py:10: note: "list" is missing following "P" protocol member: main.py:10: note: arg main.py:11: error: Argument 1 to "foo" has incompatible type "**dict[str, object]"; expected "P" [arg-type] main.py:11: note: "dict" is missing following "P" protocol member: main.py:11: note: arg ``` https://mypy-play.net/?mypy=master&python=3.12&flags=strict&gist=d0228ba7d2802db8ac4457f374ccc148
1 parent bca959f commit 9934278

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

mypy/checkexpr.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,9 +2684,12 @@ def check_arg(
26842684
context=context,
26852685
outer_context=outer_context,
26862686
)
2687-
self.msg.incompatible_argument_note(
2688-
original_caller_type, callee_type, context, parent_error=error
2689-
)
2687+
if not caller_kind.is_star():
2688+
# For *args and **kwargs this note would be incorrect - we're comparing
2689+
# iterable/mapping type with union of relevant arg types.
2690+
self.msg.incompatible_argument_note(
2691+
original_caller_type, callee_type, context, parent_error=error
2692+
)
26902693
if not self.msg.prefer_simple_messages():
26912694
self.chk.check_possible_missing_await(
26922695
caller_type, callee_type, context, error.code

test-data/unit/check-functions.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3694,3 +3694,19 @@ def defer() -> int: ...
36943694
[out]
36953695
main: note: In function "a":
36963696
main:6: error: Unsupported operand types for + ("int" and "str")
3697+
3698+
[case testNoExtraNoteForUnpacking]
3699+
from typing import Protocol
3700+
3701+
class P(Protocol):
3702+
arg: int
3703+
# Something that list and dict also have
3704+
def __contains__(self, item: object) -> bool: ...
3705+
3706+
def foo(x: P, y: P) -> None: ...
3707+
3708+
args: list[object]
3709+
foo(*args) # E: Argument 1 to "foo" has incompatible type "*list[object]"; expected "P"
3710+
kwargs: dict[str, object]
3711+
foo(**kwargs) # E: Argument 1 to "foo" has incompatible type "**dict[str, object]"; expected "P"
3712+
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)