Skip to content
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
5 changes: 2 additions & 3 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,11 @@ def infer_against_overloaded(self, overloaded: Overloaded,
template: CallableType) -> List[Constraint]:
# Create constraints by matching an overloaded type against a template.
# This is tricky to do in general. We cheat by only matching against
# the first overload item, and by only matching the return type. This
# the first overload item that is callable compatible. This

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is 8 years old. I think this answers your question in the PR description.

# seems to work somewhat well, but we should really use a more
# reliable technique.
item = find_matching_overload_item(overloaded, template)
return infer_constraints(template.ret_type, item.ret_type,
self.direction)
return infer_constraints(template, item, self.direction)

def visit_tuple_type(self, template: TupleType) -> List[Constraint]:
actual = self.actual
Expand Down
25 changes: 25 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -5099,3 +5099,28 @@ def foo(x: MyInt) -> int: ...
def foo(x):
...
[builtins fixtures/tuple.pyi]

[case testOverloadedToGeneric]
from typing import TypeVar, Callable, NewType, overload, Union

# int in our stubs isn't overloaded
class fakeint:
@overload
def __init__(self, x: Union[str, bytes] = ...) -> None: ...
@overload
def __init__(self, x: Union[str, bytes], base: int) -> None: ...
def __init__(self, *args) -> None: pass # type: ignore


U = TypeVar('U')
V = TypeVar('V')
W = TypeVar('W')
def compose(f: Callable[[U], V], g: Callable[[W], U]) -> Callable[[W], V]:
return lambda x: f(g(x))

ID = NewType("ID", fakeint)

compose(ID, fakeint)("test")
reveal_type(compose(ID, fakeint)) # N: Revealed type is 'def (Union[builtins.str, builtins.bytes]) -> __main__.ID*'

[builtins fixtures/tuple.pyi]