Skip to content

Commit bea0591

Browse files
ilevkivskyiIvan Levkivskyi
authored and
Ivan Levkivskyi
committed
Implement a basic meet for overloaded types (#5336)
The algorithm is super basic. But it is still better than crashing with `NotImplementedError`.
1 parent bc0b551 commit bea0591

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

mypy/meet.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from mypy.types import (
66
Type, AnyType, TypeVisitor, UnboundType, NoneTyp, TypeVarType, Instance, CallableType,
77
TupleType, TypedDictType, ErasedType, UnionType, PartialType, DeletedType,
8-
UninhabitedType, TypeType, TypeOfAny
8+
UninhabitedType, TypeType, TypeOfAny, Overloaded, FunctionLike
99
)
1010
from mypy.subtypes import is_equivalent, is_subtype, is_protocol_implementation
1111

@@ -314,6 +314,21 @@ def visit_callable_type(self, t: CallableType) -> Type:
314314
else:
315315
return self.default(self.s)
316316

317+
def visit_overloaded(self, t: Overloaded) -> Type:
318+
# TODO: Implement a better algorithm that covers at least the same cases
319+
# as TypeJoinVisitor.visit_overloaded().
320+
s = self.s
321+
if isinstance(s, FunctionLike):
322+
if s.items() == t.items():
323+
return Overloaded(t.items())
324+
elif is_subtype(s, t):
325+
return s
326+
elif is_subtype(t, s):
327+
return t
328+
else:
329+
return meet_types(t.fallback, s.fallback)
330+
return meet_types(t.fallback, s)
331+
317332
def visit_tuple_type(self, t: TupleType) -> Type:
318333
if isinstance(self.s, TupleType) and self.s.length() == t.length():
319334
items = [] # type: List[Type]

test-data/unit/check-overloading.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4078,3 +4078,25 @@ g(3) # E: No overload variant of "g" matches argument type "int" \
40784078
# N: def g(x: A) -> None \
40794079
# N: def g(x: B) -> None \
40804080
# N: def g(x: C) -> None
4081+
4082+
[case testOverloadedInIter]
4083+
from lib import f, g
4084+
4085+
for fun in [f, g]:
4086+
reveal_type(fun) # E: Revealed type is 'Overload(def (x: builtins.int) -> builtins.str, def (x: builtins.str) -> builtins.int)'
4087+
[file lib.pyi]
4088+
from typing import overload
4089+
4090+
@overload
4091+
def f(x: int) -> str: ...
4092+
@overload
4093+
def f(x: str) -> int: ...
4094+
4095+
@overload
4096+
def g(x: int) -> str: ...
4097+
@overload
4098+
def g(x: str) -> int: ...
4099+
4100+
[builtins fixtures/list.pyi]
4101+
[typing fixtures/typing-full.pyi]
4102+
[out]

0 commit comments

Comments
 (0)