8000 Fix #1081. When comparing template to actual arg types, stop at shortest. by gvanrossum · Pull Request #1085 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix #1081. When comparing template to actual arg types, stop at shortest. #1085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
7 changes: 3 additions & 4 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,10 @@ def visit_callable_type(self, template: CallableType) -> List[Constraint]:
# We can't infer constraints from arguments if the template is Callable[..., T] (with
# literal '...').
if not template.is_ellipsis_args:
for i in range(len(template.arg_types)):
# The lengths should match, but don't crash (it will error elsewhere).
for t, a in zip(template.arg_types, cactual.arg_types):
# Negate constraints due function argument type contravariance.
res.extend(negate_constraints(infer_constraints(
template.arg_types[i], cactual.arg_types[i],
self.direction)))
res.extend(negate_constraints(infer_constraints(t, a, self.direction)))
res.extend(infer_constraints(template.ret_type, cactual.ret_type,
self.direction))
return res
Expand Down
9 changes: 9 additions & 0 deletions mypy/test/data/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ def f(x: B) -> B: pass
@overload
def f(x: C) -> C: pass

[case testInferConstraintsUnequalLengths]
from typing import Any, Callable, List
def f(fields: List[Callable[[Any], Any]]): pass
class C: pass
f([C]) # E: List item 0 has incompatible type
class D:
def __init__(self, a, b): pass
f([D]) # E: List item 0 has incompatible type
[builtins fixtures/list.py]

-- Default argument values
-- -----------------------
Expand Down
0