Skip to content

Commit 69a8556

Browse files
li-danJukkaL
authored andcommitted
Improve error message for an invalid callable type (#4213)
If the user gives the wrong number of arguments to `Callable[]`, tell them how many arguments are expected.
1 parent e1c569c commit 69a8556

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

mypy/typeanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def analyze_callable_type(self, t: UnboundType) -> Type:
467467
self.fail('The first argument to Callable must be a list of types or "..."', t)
468468
return AnyType(TypeOfAny.from_error)
469469
else:
470-
self.fail('Invalid function type', t)
470+
self.fail('Please use "Callable[[<parameters>], <return type>]" or "Callable"', t)
471471
return AnyType(TypeOfAny.from_error)
472472
assert isinstance(ret, CallableType)
473473
return ret.accept(self)

test-data/unit/check-generics.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ class Node(Generic[T]):
812812
def __init__(self, x: T) -> None:
813813
...
814814

815-
BadC = Callable[T] # E: Invalid function type
815+
BadC = Callable[T] # E: Please use "Callable[[<parameters>], <return type>]" or "Callable"
816816

817817
C = Callable[..., T]
818818
C2 = Callable[[T, T], Node[T]]

test-data/unit/semanal-errors.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,8 @@ y = None # type: Callable[int]
819819
z = None # type: Callable[int, int, int]
820820
[out]
821821
main:2: error: The first argument to Callable must be a list of types or "..."
822-
main:3: error: Invalid function type
823-
main:4: error: Invalid function type
822+
main:3: error: Please use "Callable[[<parameters>], <return type>]" or "Callable"
823+
main:4: error: Please use "Callable[[<parameters>], <return type>]" or "Callable"
824824

825825
[case testAbstractGlobalFunction]
826826
import typing

0 commit comments

Comments
 (0)