Skip to content

Commit a3eb219

Browse files
authored
Fix overload diagnostic when vararg and varkwarg can match (#19614)
Fixes #19612
1 parent de6e742 commit a3eb219

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

mypy/typeops.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,15 +521,18 @@ def callable_corresponding_argument(
521521

522522
# def right(a: int = ...) -> None: ...
523523
# def left(__a: int = ..., *, a: int = ...) -> None: ...
524-
from mypy.subtypes import is_equivalent
524+
from mypy.subtypes import is_subtype
525525

526526
if (
527527
not (by_name.required or by_pos.required)
528528
and by_pos.name is None
529529
and by_name.pos is None
530-
and is_equivalent(by_name.typ, by_pos.typ)
531530
):
532-
return FormalArgument(by_name.name, by_pos.pos, by_name.typ, False)
531+
# We actually want the intersection of by_name.typ and by_pos.typ
532+
if is_subtype(by_name.typ, by_pos.typ):
533+
return FormalArgument(by_name.name, by_pos.pos, by_name.typ, False)
534+
if is_subtype(by_pos.typ, by_name.typ):
535+
return FormalArgument(by_name.name, by_pos.pos, by_pos.typ, False)
533536
return by_name if by_name is not None else by_pos
534537

535538

test-data/unit/check-overloading.test

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,21 @@ def f(x: 'A') -> Any: # E: Overloaded function implementation does not accept al
231231

232232
reveal_type(f(A())) # N: Revealed type is "__main__.B"
233233
reveal_type(f(B())) # N: Revealed type is "__main__.A"
234-
235234
[builtins fixtures/isinstance.pyi]
236235

236+
[case testTypeCheckOverloadImplOverlapVarArgsAndKwargs]
237+
from __future__ import annotations
238+
from typing import overload
239+
240+
@overload
241+
def foo(x: int) -> None: ...
242+
@overload
243+
def foo(a: str, /) -> None: ...
244+
245+
def foo(*args: int | str, **kw: int) -> None:
246+
pass
247+
[builtins fixtures/tuple.pyi]
248+
237249
[case testTypeCheckOverloadWithImplTooSpecificRetType]
238250
from typing import overload, Any
239251

0 commit comments

Comments
 (0)