Skip to content

Implement a basic meet for overloaded types #5336

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
merged 2 commits into from
Jul 10, 2018
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
17 changes: 16 additions & 1 deletion mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from mypy.types import (
Type, AnyType, TypeVisitor, UnboundType, NoneTyp, TypeVarType, Instance, CallableType,
TupleType, TypedDictType, ErasedType, UnionType, PartialType, DeletedType,
UninhabitedType, TypeType, TypeOfAny
UninhabitedType, TypeType, TypeOfAny, Overloaded, FunctionLike
)
from mypy.subtypes import is_equivalent, is_subtype, is_protocol_implementation

Expand Down Expand Up @@ -314,6 +314,21 @@ def visit_callable_type(self, t: CallableType) -> Type:
else:
return self.default(self.s)

def visit_overloaded(self, t: Overloaded) -> Type:
# TODO: Implement a better algorithm that covers at least the same cases
# as TypeJoinVisitor.visit_overloaded().
s = self.s
if isinstance(s, FunctionLike):
if s.items() == t.items():
return Overloaded(t.items())
elif is_subtype(s, t):
return s
elif is_subtype(t, s):
return t
else:
return meet_types(t.fallback, s.fallback)
return meet_types(t.fallback, s)

def visit_tuple_type(self, t: TupleType) -> Type:
if isinstance(self.s, TupleType) and self.s.length() == t.length():
items = [] # type: List[Type]
Expand Down
22 changes: 22 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -4078,3 +4078,25 @@ g(3) # E: No overload variant of "g" matches argument type "int" \
# N: def g(x: A) -> None \
# N: def g(x: B) -> None \
# N: def g(x: C) -> None

[case testOverloadedInIter]
from lib import f, g

for fun in [f, g]:
reveal_type(fun) # E: Revealed type is 'Overload(def (x: builtins.int) -> builtins.str, def (x: builtins.str) -> builtins.int)'
[file lib.pyi]
from typing import overload

@overload
def f(x: int) -> str: ...
@overload
def f(x: str) -> int: ...

@overload
def g(x: int) -> str: ...
@overload
def g(x: str) -> int: ...

[builtins fixtures/list.pyi]
[typing fixtures/typing-full.pyi]
[out]