Skip to content

Commit aa97427

Browse files
authored
Look at arguments when generating constraints for an overload (#8845)
This improves a lot of cases. I'm not totally sure why we skipped the arguments in the past.
1 parent d01799c commit aa97427

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

mypy/constraints.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,11 @@ def infer_against_overloaded(self, overloaded: Overloaded,
486486
template: CallableType) -> List[Constraint]:
487487
# Create constraints by matching an overloaded type against a template.
488488
# This is tricky to do in general. We cheat by only matching against
489-
# the first overload item, and by only matching the return type. This
489+
# the first overload item that is callable compatible. This
490490
# seems to work somewhat well, but we should really use a more
491491
# reliable technique.
492492
item = find_matching_overload_item(overloaded, template)
493-
return infer_constraints(template.ret_type, item.ret_type,
494-
self.direction)
493+
return infer_constraints(template, item, self.direction)
495494

496495
def visit_tuple_type(self, template: TupleType) -> List[Constraint]:
497496
actual = self.actual

test-data/unit/check-overloading.test

+25
Original file line numberDiff line numberDiff line change
@@ -5099,3 +5099,28 @@ def foo(x: MyInt) -> int: ...
50995099
def foo(x):
51005100
...
51015101
[builtins fixtures/tuple.pyi]
5102+
5103+
[case testOverloadedToGeneric]
5104+
from typing import TypeVar, Callable, NewType, overload, Union
5105+
5106+
# int in our stubs isn't overloaded
5107+
class fakeint:
5108+
@overload
5109+
def __init__(self, x: Union[str, bytes] = ...) -> None: ...
5110+
@overload
5111+
def __init__(self, x: Union[str, bytes], base: int) -> None: ...
5112+
def __init__(self, *args) -> None: pass # type: ignore
5113+
5114+
5115+
U = TypeVar('U')
5116+
V = TypeVar('V')
5117+
W = TypeVar('W')
5118+
def compose(f: Callable[[U], V], g: Callable[[W], U]) -> Callable[[W], V]:
5119+
return lambda x: f(g(x))
5120+
5121+
ID = NewType("ID", fakeint)
5122+
5123+
compose(ID, fakeint)("test")
5124+
reveal_type(compose(ID, fakeint)) # N: Revealed type is 'def (Union[builtins.str, builtins.bytes]) -> __main__.ID*'
5125+
5126+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)