Skip to content

Commit a48ffed

Browse files
Show name of type variable in "Cannot infer type argument" message (#19290)
Fixes #19289 The type argument index currently shown can be wrong if other type arguments were substituted during an earlier pass. Given: ```python def foo[T1, T2]( a: T1, b: T2, c: Callable[[T2], T2], ) -> tuple[T1, T2]: ... def bar(y: float) -> float: ... reveal_type(foo(1, None, bar)) # Expect T1=int, T2=<failed> ``` Before: ``` main.py:9: error: Cannot infer type argument 1 of "foo" [misc] main.py:9: note: Revealed type is "tuple[builtins.int, Any]" ``` After: ``` main.py:9: error: Cannot infer type argument to type parameter "T2" of "foo" [misc] main.py:9: note: Revealed type is "tuple[builtins.int, Any]" ```
1 parent b62957b commit a48ffed

13 files changed

+49
-30
lines changed

mypy/checkexpr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,10 +2338,10 @@ def apply_inferred_arguments(
23382338
# Report error if some of the variables could not be solved. In that
23392339
# case assume that all variables have type Any to avoid extra
23402340
# bogus error messages.
2341-
for i, inferred_type in enumerate(inferred_args):
2341+
for inferred_type, tv in zip(inferred_args, callee_type.variables):
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(callee_type, tv, context)
23452345
inferred_args = [AnyType(TypeOfAny.from_error)] * len(inferred_args)
23462346
# Apply the inferred types to the function type. In this case the
23472347
# 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 value of 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 value of 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 value of 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 value of 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 value of 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 value of 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 value of 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 value of type parameter "T" of "f"
10131013
[builtins fixtures/list.pyi]
10141014

10151015
[case testInferTypeVariableFromTwoGenericTypes3]

test-data/unit/check-inference.test

Lines changed: 20 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 value of type parameter "T" of "f"
697+
f(ab, ao) # E: Cannot infer value of 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 value of type parameter "T" of "f"
3778+
f(["no"], empty) # E: Cannot infer value of type parameter "T" of "f"
37793779
[builtins fixtures/list.pyi]
37803780

37813781
[case testInferenceWorksWithEmptyCollectionsUnion]
@@ -4149,3 +4149,19 @@ class Foo:
41494149
else:
41504150
self.qux = {} # E: Need type annotation for "qux" (hint: "qux: dict[<type>, <type>] = ...")
41514151
[builtins fixtures/dict.pyi]
4152+
4153+
[case testConstraintSolvingFailureShowsCorrectArgument]
4154+
from typing import Callable, TypeVar
4155+
4156+
T1 = TypeVar('T1')
4157+
T2 = TypeVar('T2')
4158+
def foo(
4159+
a: T1,
4160+
b: T2,
4161+
c: Callable[[T2], T2],
4162+
) -> tuple[T1, T2]: ...
4163+
4164+
def bar(y: float) -> float: ...
4165+
4166+
foo(1, None, bar) # E: Cannot infer value of type parameter "T2" of "foo"
4167+
[builtins fixtures/tuple.pyi]

test-data/unit/check-literal.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2989,9 +2989,9 @@ def g(a: T, t: A[T]) -> T: ...
29892989

29902990
def check(obj: A[Literal[1]]) -> None:
29912991
reveal_type(f(obj, 1)) # N: Revealed type is "Literal[1]"
2992-
reveal_type(f(obj, '')) # E: Cannot infer type argument 1 of "f" \
2992+
reveal_type(f(obj, '')) # E: Cannot infer value of type parameter "T" of "f" \
29932993
# N: Revealed type is "Any"
29942994
reveal_type(g(1, obj)) # N: Revealed type is "Literal[1]"
2995-
reveal_type(g('', obj)) # E: Cannot infer type argument 1 of "g" \
2995+
reveal_type(g('', obj)) # E: Cannot infer value of type parameter "T" of "g" \
29962996
# N: Revealed type is "Any"
29972997
[builtins fixtures/tuple.pyi]

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 value of 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 value of 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 value of 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 value of 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 value of type parameter "T" of "A"
474+
A([1], '2') # E: Cannot infer value of type parameter "T" of "A"
475475

476476
[builtins fixtures/list.pyi]
477477

test-data/unit/check-protocols.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4217,10 +4217,10 @@ def g2(a: Input[bytes], b: Output[bytes]) -> None:
42174217
f(a, b)
42184218

42194219
def g3(a: Input[str], b: Output[bytes]) -> None:
4220-
f(a, b) # E: Cannot infer type argument 1 of "f"
4220+
f(a, b) # E: Cannot infer value of type parameter "AnyStr" of "f"
42214221

42224222
def g4(a: Input[bytes], b: Output[str]) -> None:
4223-
f(a, b) # E: Cannot infer type argument 1 of "f"
4223+
f(a, b) # E: Cannot infer value of type parameter "AnyStr" of "f"
42244224

42254225
[builtins fixtures/tuple.pyi]
42264226

test-data/unit/check-typevar-tuple.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,19 +2372,19 @@ def pointwise_multiply(x: Array[Unpack[Ts]], y: Array[Unpack[Ts]]) -> Array[Unpa
23722372

23732373
def a1(x: Array[int], y: Array[str], z: Array[int, str]) -> None:
23742374
reveal_type(pointwise_multiply(x, x)) # N: Revealed type is "__main__.Array[builtins.int]"
2375-
reveal_type(pointwise_multiply(x, y)) # E: Cannot infer type argument 1 of "pointwise_multiply" \
2375+
reveal_type(pointwise_multiply(x, y)) # E: Cannot infer value of type parameter "Ts" of "pointwise_multiply" \
23762376
# N: Revealed type is "__main__.Array[Unpack[builtins.tuple[Any, ...]]]"
2377-
reveal_type(pointwise_multiply(x, z)) # E: Cannot infer type argument 1 of "pointwise_multiply" \
2377+
reveal_type(pointwise_multiply(x, z)) # E: Cannot infer value of type parameter "Ts" of "pointwise_multiply" \
23782378
# N: Revealed type is "__main__.Array[Unpack[builtins.tuple[Any, ...]]]"
23792379

23802380
def func(x: Array[Unpack[Ts]], *args: Unpack[Ts]) -> Tuple[Unpack[Ts]]:
23812381
...
23822382

23832383
def a2(x: Array[int, str]) -> None:
23842384
reveal_type(func(x, 2, "Hello")) # N: Revealed type is "tuple[builtins.int, builtins.str]"
2385-
reveal_type(func(x, 2)) # E: Cannot infer type argument 1 of "func" \
2385+
reveal_type(func(x, 2)) # E: Cannot infer value of type parameter "Ts" of "func" \
23862386
# N: Revealed type is "builtins.tuple[Any, ...]"
2387-
reveal_type(func(x, 2, "Hello", True)) # E: Cannot infer type argument 1 of "func" \
2387+
reveal_type(func(x, 2, "Hello", True)) # E: Cannot infer value of type parameter "Ts" of "func" \
23882388
# N: Revealed type is "builtins.tuple[Any, ...]"
23892389
[builtins fixtures/tuple.pyi]
23902390

0 commit comments

Comments
 (0)