Skip to content
Closed
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
4 changes: 4 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,10 @@ def no_variant_matches_arguments(self, overload: Overloaded, arg_types: List[Typ
.format(name, arg_types), context)
else:
self.fail('No overload variant matches argument types {}'.format(arg_types), context)
OFFSET = 2
MAX_ITEMS = 2
self.note('Possible overload variants:', context)
self.pretty_overload(overload, context, OFFSET, MAX_ITEMS)

def wrong_number_values_to_unpack(self, provided: int, expected: int,
context: Context) -> None:
Expand Down
25 changes: 21 additions & 4 deletions test-data/unit/check-abstract.test
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,21 @@ class B(A):
def f(self, x: int) -> int: pass
@overload
def f(self, x: str) -> str: pass
A() # E: Cannot instantiate abstract class 'A' with abstract attribute 'f'
A()
B()
B().f(1)
a = B() # type: A
a.f(1)
a.f('')
a.f(B()) # E: No overload variant of "f" of "A" matches argument types [foo.B]
a.f(B())
[out]
tmp/foo.pyi:17: error: Cannot instantiate abstract class 'A' with abstract attribute 'f'
tmp/foo.pyi:23: error: No overload variant of "f" of "A" matches argument types [foo.B]
tmp/foo.pyi:23: note: Possible overload variants:
tmp/foo.pyi:23: note: @overload
tmp/foo.pyi:23: note: def f(self, x: int) -> int
tmp/foo.pyi:23: note: @overload
tmp/foo.pyi:23: note: def f(self, x: str) -> str

[case testOverloadedAbstractMethodWithAlternativeDecoratorOrder]
from foo import *
Expand All @@ -546,13 +554,22 @@ class B(A):
def f(self, x: int) -> int: pass
@overload
def f(self, x: str) -> str: pass
A() # E: Cannot instantiate abstract class 'A' with abstract attribute 'f'
A()
B()
B().f(1)
a = B() # type: A
a.f(1)
a.f('')
a.f(B()) # E: No overload variant of "f" of "A" matches argument types [foo.B]
a.f(B())
[out]
tmp/foo.pyi:17: error: Cannot instantiate abstract class 'A' with abstract attribute 'f'
tmp/foo.pyi:23: error: No overload variant of "f" of "A" matches argument types [foo.B]
tmp/foo.pyi:23: note: Possible overload variants:
tmp/foo.pyi:23: note: @overload
tmp/foo.pyi:23: note: def f(self, x: int) -> int
tmp/foo.pyi:23: note: @overload
tmp/foo.pyi:23: note: def f(self, x: str) -> str


[case testOverloadedAbstractMethodVariantMissingDecorator1]
from foo import *
Expand Down
16 changes: 13 additions & 3 deletions test-data/unit/check-class-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,19 @@ class Overloader(NamedTuple):
def method(self, y):
return y

reveal_type(Overloader(1).method('string')) # E: Revealed type is 'builtins.str'
reveal_type(Overloader(1).method(1)) # E: Revealed type is 'builtins.int'
Overloader(1).method(('tuple',)) # E: No overload variant of "method" of "Overloader" matches argument types [Tuple[builtins.str]]
reveal_type(Overloader(1).method('string'))
reveal_type(Overloader(1).method(1))
Overloader(1).method(('tuple',))
[out]
main:12: error: Revealed type is 'builtins.str'
main:13: error: Revealed type is 'builtins.int'
main:14: error: No overload variant of "method" of "Overloader" matches argument types [Tuple[builtins.str]]
main:14: note: Possible overload variants:
main:14: note: @overload
main:14: note: def method(self, y: str) -> str
main:14: note: @overload
main:14: note: def method(self, y: int) -> int


[case testNewNamedTupleMethodInheritance]
from typing import NamedTuple, TypeVar
Expand Down
73 changes: 65 additions & 8 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -1225,8 +1225,18 @@ class D:
[out]
main:5: error: Revealed type is 'Any'
main:5: error: No overload variant of "__get__" of "D" matches argument types [builtins.None, Type[__main__.A]]
main:5: note: Possible overload variants:
main:5: note: @overload
main:5: note: def __get__(self, inst: None, own: Type[Base]) -> D
main:5: note: @overload
main:5: note: def __get__(self, inst: Base, own: Type[Base]) -> str
main:6: error: Revealed type is 'Any'
main:6: error: No overload variant of "__get__" of "D" matches argument types [__main__.A, Type[__main__.A]]
main:6: note: Possible overload variants:
main:6: note: @overload
main:6: note: def __get__(self, inst: None, own: Type[Base]) -> D
main:6: note: @overload
main:6: note: def __get__(self, inst: Base, own: Type[Base]) -> str


[case testAccessingGenericNonDataDescriptor]
Expand Down Expand Up @@ -1325,6 +1335,11 @@ class D(Generic[T, V]):
[out]
main:5: error: Revealed type is 'Any'
main:5: error: No overload variant of "__get__" of "D" matches argument types [builtins.None, Type[__main__.A]]
main:5: note: Possible overload variants:
main:5: note: @overload
main:5: note: def __get__(self, inst: None, own: None) -> D[A, int]
main:5: note: @overload
main:5: note: def __get__(self, inst: A, own: Type[A]) -> int

[case testAccessingNonDataDescriptorSubclass]
from typing import Any
Expand Down Expand Up @@ -2092,10 +2107,18 @@ class C:
obj = object.__new__(cls)
return obj
c = C(1)
c.a # E: "C" has no attribute "a"
c.a
C('', '')
C('') # E: No overload variant of "C" matches argument types [builtins.str]
C('')
[builtins fixtures/__new__.pyi]
[out]
tmp/foo.pyi:12: error: "C" has no attribute "a"
tmp/foo.pyi:14: error: No overload variant of "C" matches argument types [builtins.str]
tmp/foo.pyi:14: note: Possible overload variants:
tmp/foo.pyi:14: note: @overload
tmp/foo.pyi:14: note: def __new__(cls, foo: int) -> C
tmp/foo.pyi:14: note: @overload
tmp/foo.pyi:14: note: def __new__(cls, x: str, y: str) -> C


-- Special cases
Expand Down Expand Up @@ -2517,6 +2540,11 @@ u = new(User)
[builtins fixtures/classmethod.pyi]
[out]
tmp/foo.pyi:16: error: No overload variant of "User" matches argument types [builtins.str]
tmp/foo.pyi:16: note: Possible overload variants:
tmp/foo.pyi:16: note: @overload
tmp/foo.pyi:16: note: def __init__(self) -> U
tmp/foo.pyi:16: note: @overload
tmp/foo.pyi:16: note: def __init__(self, arg: int) -> U
tmp/foo.pyi:17: error: Too many arguments for "foo" of "User"

[case testTypeUsingTypeCInUpperBound]
Expand Down Expand Up @@ -2712,9 +2740,15 @@ def f(a: int) -> None: pass
def mock() -> type: return User

f(User)
f(mock()) # E: No overload variant of "f" matches argument types [builtins.type]
f(mock())
[builtins fixtures/classmethod.pyi]
[out]
tmp/foo.pyi:13: error: No overload variant of "f" matches argument types [builtins.type]
tmp/foo.pyi:13: note: Possible overload variants:
tmp/foo.pyi:13: note: @overload
tmp/foo.pyi:13: note: def f(a: Type[User]) -> None
tmp/foo.pyi:13: note: @overload
tmp/foo.pyi:13: note: def f(a: int) -> None

[case testNonTypeDoesNotMatchOverloadedFunctions]
from foo import *
Expand All @@ -2728,9 +2762,15 @@ def f(a: Type[User]) -> None: pass
@overload
def f(a: type) -> None: pass

f(3) # E: No overload variant of "f" matches argument types [builtins.int]
f(3)
[builtins fixtures/classmethod.pyi]
[out]
tmp/foo.pyi:10: error: No overload variant of "f" matches argument types [builtins.int]
tmp/foo.pyi:10: note: Possible overload variants:
tmp/foo.pyi:10: note: @overload
tmp/foo.pyi:10: note: def f(a: Type[User]) -> None
tmp/foo.pyi:10: note: @overload
tmp/foo.pyi:10: note: def f(a: type) -> None

[case testInstancesDoNotMatchTypeInOverloadedFunctions]
from foo import *
Expand All @@ -2745,9 +2785,15 @@ def f(a: Type[User]) -> None: pass
def f(a: int) -> None: pass

f(User)
f(User()) # E: No overload variant of "f" matches argument types [foo.User]
f(User())
[builtins fixtures/classmethod.pyi]
[out]
tmp/foo.pyi:11: error: No overload variant of "f" matches argument types [foo.User]
tmp/foo.pyi:11: note: Possible overload variants:
tmp/foo.pyi:11: note: @overload
tmp/foo.pyi:11: note: def f(a: Type[User]) -> None
tmp/foo.pyi:11: note: @overload
tmp/foo.pyi:11: note: def f(a: int) -> None

[case testTypeCovarianceWithOverloadedFunctions]
from foo import *
Expand All @@ -2766,15 +2812,26 @@ def f(a: Type[B]) -> None: pass
@overload
def f(a: int) -> None: pass

f(A) # E: No overload variant of "f" matches argument types [def () -> foo.A]
f(A)
f(B)
f(C)
f(AType) # E: No overload variant of "f" matches argument types [Type[foo.A]]
f(AType)
f(BType)
f(CType)
[builtins fixtures/classmethod.pyi]
[out]

tmp/foo.pyi:15: error: No overload variant of "f" matches argument types [def () -> foo.A]
tmp/foo.pyi:15: note: Possible overload variants:
tmp/foo.pyi:15: note: @overload
tmp/foo.pyi:15: note: def f(a: Type[B]) -> None
tmp/foo.pyi:15: note: @overload
tmp/foo.pyi:15: note: def f(a: int) -> None
tmp/foo.pyi:18: error: No overload variant of "f" matches argument types [Type[foo.A]]
tmp/foo.pyi:18: note: Possible overload variants:
tmp/foo.pyi:18: note: @overload
tmp/foo.pyi:18: note: def f(a: Type[B]) -> None
tmp/foo.pyi:18: note: @overload
tmp/foo.pyi:18: note: def f(a: int) -> None

[case testOverloadedCovariantTypesFail]
from foo import *
Expand Down
14 changes: 11 additions & 3 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -866,12 +866,12 @@ from typing import overload
a, b, c = None, None, None # type: (A, B, C)
a[b]
a[c]
a[1] # E: No overload variant of "__getitem__" of "A" matches argument types [builtins.int]
a[1]

i, s = None, None # type: (int, str)
i = a[b]
s = a[b] # E: Incompatible types in assignment (expression has type "int", variable has type "str")
i = a[c] # E: Incompatible types in assignment (expression has type "str", variable has type "int")
s = a[b]
i = a[c]
s = a[c]

class A:
Expand All @@ -884,6 +884,14 @@ class A:
class B: pass
class C: pass
[out]
tmp/foo.pyi:6: error: No overload variant of "__getitem__" of "A" matches argument types [builtins.int]
tmp/foo.pyi:6: note: Possible overload variants:
tmp/foo.pyi:6: note: @overload
tmp/foo.pyi:6: note: def __getitem__(self, B) -> int
tmp/foo.pyi:6: note: @overload
tmp/foo.pyi:6: note: def __getitem__(self, C) -> str
tmp/foo.pyi:10: error: Incompatible types in assignment (expression has type "int", variable has type "str")
tmp/foo.pyi:11: error: Incompatible types in assignment (expression has type "str", variable has type "int")


-- Cast expression
Expand Down
9 changes: 8 additions & 1 deletion test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,14 @@ class A:
a = None # type: A
a.g()
a.g(B())
a.g(a) # E: No overload variant matches argument types [foo.A]
a.g(a)
[out]
tmp/foo.pyi:12: error: No overload variant matches argument types [foo.A]
tmp/foo.pyi:12: note: Possible overload variants:
tmp/foo.pyi:12: note: @overload
tmp/foo.pyi:12: note: def f(self) -> None
tmp/foo.pyi:12: note: @overload
tmp/foo.pyi:12: note: def f(self, b: B) -> None

[case testMethodAsDataAttributeInferredFromDynamicallyTypedMethod]

Expand Down
11 changes: 9 additions & 2 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -1469,10 +1469,17 @@ def f(blocks: Any): # E: Name 'Any' is not defined

[case testSpecialCaseEmptyListInitialization2]
def f(blocks: object):
to_process = [] # E: Need type annotation for 'to_process'
to_process = list(blocks) # E: No overload variant of "list" matches argument types [builtins.object]
to_process = []
to_process = list(blocks)
[builtins fixtures/list.pyi]
[out]
main:2: error: Need type annotation for 'to_process'
main:3: error: No overload variant of "list" matches argument types [builtins.object]
main:3: note: Possible overload variants:
main:3: note: @overload
main:3: note: def [T] __init__(self) -> List[T]
main:3: note: @overload
main:3: note: def [T] __init__(self, x: Iterable[T]) -> List[T]


-- Inferring types of variables first initialized to None (partial types)
Expand Down
Loading