8000 Fix #1081. When comparing template to actual arg types, stop at short… · python/mypy@02e88c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 02e88c4

Browse files
Guido van RossumJukkaL
Guido van Rossum
authored andcommitted
Fix #1081. When comparing template to actual arg types, stop at shortest.
1 parent 6bc48f4 commit 02e88c4

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

mypy/constraints.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,10 @@ def visit_callable_type(self, template: CallableType) -> List[Constraint]:
225225
# We can't infer constraints from arguments if the template is Callable[..., T] (with
226226
# literal '...').
227227
if not template.is_ellipsis_args:
228-
for i in range(len(template.arg_types)):
228+
# The lengths should match, but don't crash (it will error elsewhere).
229+
for t, a in zip(template.arg_types, cactual.arg_types):
229230
# Negate constraints due function argument type contravariance.
230-
res.extend(negate_constraints(infer_constraints(
231-
template.arg_types[i], cactual.arg_types[i],
232-
self.direction)))
231+
res.extend(negate_constraints(infer_constraints(t, a, self.direction)))
233232
res.extend(infer_constraints(template.ret_type, cactual.ret_type,
234233
self.direction))
235234
return res

mypy/test/data/check-functions.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ def f(x: B) -> B: pass
178178
@overload
179179
def f(x: C) -> C: pass
180180

181+
[case testInferConstraintsUnequalLengths]
182+
from typing import Any, Callable, List
183+
def f(fields: List[Callable[[Any], Any]]): pass
184+
class C: pass
185+
f([C]) # E: List item 0 has incompatible type
186+
class D:
187+
def __init__(self, a, b): pass
188+
f([D]) # E: List item 0 has incompatible type
189+
[builtins fixtures/list.py]
181190

182191
-- Default argument values
183192
-- -----------------------

0 commit comments

Comments
 (0)
0