Skip to content

Commit 967be77

Browse files
committed
Show name of type variable in "Cannot infer type argument" message
1 parent 8241059 commit 967be77

12 files changed

+32
-27
lines changed

mypy/checkexpr.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2341,7 +2341,9 @@ def apply_inferred_arguments(
23412341
for i, inferred_type in enumerate(inferred_args):
23422342
if not inferred_type or has_erased_component(inferred_type):
23432343
# Could not infer a non-trivial type for a type variable.
2344-
self.msg.could_not_infer_type_arguments(callee_type, i + 1, context)
2344+
self.msg.could_not_infer_type_arguments(
2345+
callee_type, callee_type.variables[i], context
2346+
)
23452347
inferred_args = [AnyType(TypeOfAny.from_error)] * len(inferred_args)
23462348
# Apply the inferred types to the function type. In this case the
23472349
# return type must be CallableType, since we give the right number of type

mypy/messages.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,11 +1370,14 @@ def incompatible_type_application(
13701370
self.fail(f"Type application has too few types ({s})", context)
13711371

13721372
def could_not_infer_type_arguments(
1373-
self, callee_type: CallableType, n: int, context: Context
1373+
self, callee_type: CallableType, tv: TypeVarLikeType, context: Context
13741374
) -> None:
13751375
callee_name = callable_name(callee_type)
1376-
if callee_name is not None and n > 0:
1377-
self.fail(f"Cannot infer type argument {n} of {callee_name}", context)
1376+
if callee_name is not None:
1377+
self.fail(
1378+
f"Cannot infer type argument to type parameter {format_type(tv, self.options)} of {callee_name}",
1379+
context,
1380+
)
13781381
if callee_name == "<dict>":
13791382
# Invariance in key type causes more of these errors than we would want.
13801383
self.note(

test-data/unit/check-dataclasses.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ class A(Generic[T]):
705705
return self.z # E: Incompatible return value type (got "list[T]", expected "T")
706706

707707
reveal_type(A) # N: Revealed type is "def [T] (x: T`1, y: T`1, z: builtins.list[T`1]) -> __main__.A[T`1]"
708-
A(1, 2, ["a", "b"]) # E: Cannot infer type argument 1 of "A"
708+
A(1, 2, ["a", "b"]) # E: Cannot infer type argument to type parameter "T" of "A"
709709
a = A(1, 2, [1, 2])
710710
reveal_type(a) # N: Revealed type is "__main__.A[builtins.int]"
711711
reveal_type(a.x) # N: Revealed type is "builtins.int"

test-data/unit/check-expressions.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,7 @@ a = {'a': 1}
18781878
b = {'z': 26, **a}
18791879
c = {**b}
18801880
d = {**a, **b, 'c': 3}
1881-
e = {1: 'a', **a} # E: Cannot infer type argument 1 of <dict> \
1881+
e = {1: 'a', **a} # E: Cannot infer type argument to type parameter "KT" of <dict> \
18821882
# N: Try assigning the literal to a variable annotated as dict[<key>, <val>]
18831883
f = {**b} # type: Dict[int, int] # E: Unpacked dict entry 0 has incompatible type "dict[str, int]"; expected "SupportsKeysAndGetItem[int, int]"
18841884
g = {**Thing()}
@@ -1893,7 +1893,7 @@ i = {**Thing()} # type: Dict[int, int] # E: Unpacked dict entry 0 has incompat
18931893
# N: def keys(self) -> Iterable[int] \
18941894
# N: Got: \
18951895
# N: def keys(self) -> Iterable[str]
1896-
j = {1: 'a', **Thing()} # E: Cannot infer type argument 1 of <dict> \
1896+
j = {1: 'a', **Thing()} # E: Cannot infer type argument to type parameter "KT" of <dict> \
18971897
# N: Try assigning the literal to a variable annotated as dict[<key>, <val>]
18981898
[builtins fixtures/dict.pyi]
18991899
[typing fixtures/typing-medium.pyi]

test-data/unit/check-generics.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ def func2(x: SameNode[T]) -> SameNode[T]:
584584
return x
585585
reveal_type(func2) # N: Revealed type is "def [T] (x: __main__.Node[T`-1, T`-1]) -> __main__.Node[T`-1, T`-1]"
586586

587-
func2(Node(1, 'x')) # E: Cannot infer type argument 1 of "func2"
587+
func2(Node(1, 'x')) # E: Cannot infer type argument to type parameter "T" of "func2"
588588
y = func2(Node('x', 'x'))
589589
reveal_type(y) # N: Revealed type is "__main__.Node[builtins.str, builtins.str]"
590590

@@ -888,7 +888,7 @@ def fun2(v: Vec[T], scale: T) -> Vec[T]:
888888

889889
reveal_type(fun1([(1, 1)])) # N: Revealed type is "builtins.int"
890890
fun1(1) # E: Argument 1 to "fun1" has incompatible type "int"; expected "list[tuple[bool, bool]]"
891-
fun1([(1, 'x')]) # E: Cannot infer type argument 1 of "fun1"
891+
fun1([(1, 'x')]) # E: Cannot infer type argument to type parameter "T" of "fun1"
892892

893893
reveal_type(fun2([(1, 1)], 1)) # N: Revealed type is "builtins.list[tuple[builtins.int, builtins.int]]"
894894
fun2([('x', 'x')], 'x') # E: Value of type variable "T" of "fun2" cannot be "str"
@@ -909,7 +909,7 @@ def f(x: Node[T, T]) -> TupledNode[T]:
909909
return Node(x.x, (x.x, x.x))
910910

911911
f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "Node[Never, Never]"
912-
f(Node(1, 'x')) # E: Cannot infer type argument 1 of "f"
912+
f(Node(1, 'x')) # E: Cannot infer type argument to type parameter "T" of "f"
913913
reveal_type(Node('x', 'x')) # N: Revealed type is "a.Node[builtins.str, builtins.str]"
914914

915915
[file a.py]

test-data/unit/check-inference-context.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ class D(C): ...
10091009

10101010
def f(x: List[T], y: List[T]) -> List[T]: ...
10111011

1012-
f([C()], [D()]) # E: Cannot infer type argument 1 of "f"
1012+
f([C()], [D()]) # E: Cannot infer type argument to type parameter "T" of "f"
10131013
[builtins fixtures/list.pyi]
10141014

10151015
[case testInferTypeVariableFromTwoGenericTypes3]

test-data/unit/check-inference.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,8 @@ class A(Generic[T]): pass
693693
class B: pass
694694

695695

696-
f(ao, ab) # E: Cannot infer type argument 1 of "f"
697-
f(ab, ao) # E: Cannot infer type argument 1 of "f"
696+
f(ao, ab) # E: Cannot infer type argument to type parameter "T" of "f"
697+
f(ab, ao) # E: Cannot infer type argument to type parameter "T" of "f"
698698
f(ao, ao)
699699
f(ab, ab)
700700

@@ -3774,8 +3774,8 @@ reveal_type(f(x, [])) # N: Revealed type is "builtins.str"
37743774
reveal_type(f(["yes"], [])) # N: Revealed type is "builtins.str"
37753775

37763776
empty: List[NoReturn]
3777-
f(x, empty) # E: Cannot infer type argument 1 of "f"
3778-
f(["no"], empty) # E: Cannot infer type argument 1 of "f"
3777+
f(x, empty) # E: Cannot infer type argument to type parameter "T" of "f"
3778+
f(["no"], empty) # E: Cannot infer type argument to type parameter "T" of "f"
37793779
[builtins fixtures/list.pyi]
37803780

37813781
[case testInferenceWorksWithEmptyCollectionsUnion]

test-data/unit/check-overloading.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3370,10 +3370,10 @@ def wrapper() -> None:
33703370
obj2: Union[W1[A], W2[B]]
33713371

33723372
reveal_type(foo(obj2)) # N: Revealed type is "Union[__main__.A, __main__.B]"
3373-
bar(obj2) # E: Cannot infer type argument 1 of "bar"
3373+
bar(obj2) # E: Cannot infer type argument to type parameter "T" of "bar"
33743374

33753375
b1_overload: A = foo(obj2) # E: Incompatible types in assignment (expression has type "Union[A, B]", variable has type "A")
3376-
b1_union: A = bar(obj2) # E: Cannot infer type argument 1 of "bar"
3376+
b1_union: A = bar(obj2) # E: Cannot infer type argument to type parameter "T" of "bar"
33773377

33783378
[case testOverloadingInferUnionReturnWithObjectTypevarReturn]
33793379
from typing import overload, Union, TypeVar, Generic
@@ -3496,7 +3496,7 @@ def t_is_same_bound(arg1: T1, arg2: S) -> Tuple[T1, S]:
34963496
# The arguments in the tuple are swapped
34973497
x3: Union[List[S], List[Tuple[S, T1]]]
34983498
y3: S
3499-
Dummy[T1]().foo(x3, y3) # E: Cannot infer type argument 1 of "foo" of "Dummy" \
3499+
Dummy[T1]().foo(x3, y3) # E: Cannot infer type argument to type parameter "S" of "foo" of "Dummy" \
35003500
# E: Argument 1 to "foo" of "Dummy" has incompatible type "Union[list[S], list[tuple[S, T1]]]"; expected "list[tuple[T1, Any]]"
35013501

35023502
x4: Union[List[int], List[Tuple[C, int]]]

test-data/unit/check-parameter-specification.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,7 @@ def d(f: Callable[P, None], fn: Callable[Concatenate[Callable[P, None], P], None
21352135

21362136
reveal_type(d(a, f1)) # N: Revealed type is "def (i: builtins.int)"
21372137
reveal_type(d(a, f2)) # N: Revealed type is "def (i: builtins.int)"
2138-
reveal_type(d(b, f1)) # E: Cannot infer type argument 1 of "d" \
2138+
reveal_type(d(b, f1)) # E: Cannot infer type argument to type parameter "P" of "d" \
21392139
# N: Revealed type is "def (*Any, **Any)"
21402140
reveal_type(d(b, f2)) # N: Revealed type is "def (builtins.int)"
21412141
[builtins fixtures/paramspec.pyi]

test-data/unit/check-plugin-attrs.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,8 @@ reveal_type(a) # N: Revealed type is "__main__.A[builtins.int]"
470470
reveal_type(a.x) # N: Revealed type is "builtins.list[builtins.int]"
471471
reveal_type(a.y) # N: Revealed type is "builtins.int"
472472

473-
A(['str'], 7) # E: Cannot infer type argument 1 of "A"
474-
A([1], '2') # E: Cannot infer type argument 1 of "A"
473+
A(['str'], 7) # E: Cannot infer type argument to type parameter "T" of "A"
474+
A([1], '2') # E: Cannot infer type argument to type parameter "T" of "A"
475475

476476
[builtins fixtures/list.pyi]
477477

0 commit comments

Comments
 (0)