Skip to content
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

Add arguments to callee context in a call expression #3116

Merged
merged 8 commits into from
Aug 15, 2017
Merged
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
15 changes: 14 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,20 @@ def visit_call_expr(self, e: CallExpr, allow_none_return: bool = False) -> Type:
elif typ.node.is_newtype:
self.msg.fail(messages.CANNOT_ISINSTANCE_NEWTYPE, e)
self.try_infer_partial_type(e)
callee_type = self.accept(e.callee, always_allow_any=True)
if isinstance(e.callee, LambdaExpr):
formal_to_actual = map_actuals_to_formals(
e.arg_kinds, e.arg_names,
e.callee.arg_kinds, e.callee.arg_names,
lambda i: self.accept(e.args[i]))

arg_types = [join.join_type_list([self.accept(e.args[j]) for j in formal_to_actual[i]])
for i in range(len(e.callee.arg_kinds))]
type_context = CallableType(arg_types, e.callee.arg_kinds, e.callee.arg_names,
ret_type=self.object_type(),
fallback=self.named_type('builtins.function'))
else:
type_context = None
callee_type = self.accept(e.callee, type_context, always_allow_any=True)
if (self.chk.options.disallow_untyped_calls and
self.chk.in_checked_function() and
isinstance(callee_type, CallableType)
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-inference-context.test
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,22 @@ class A: pass
class B: pass
[builtins fixtures/list.pyi]

[case testInferLambdaTypeUsingContext]
x : str = (lambda x: x + 1)(1) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
reveal_type((lambda x, y: x + y)(1, 2)) # E: Revealed type is 'builtins.int'
(lambda x, y: x + y)(1, "") # E: Unsupported operand types for + ("int" and "str")
(lambda *, x, y: x + y)(x=1, y="") # E: Unsupported operand types for + ("int" and "str")
reveal_type((lambda s, i: s)(i=0, s='x')) # E: Revealed type is 'builtins.str'
reveal_type((lambda s, i: i)(i=0, s='x')) # E: Revealed type is 'builtins.int'
reveal_type((lambda x, s, i: x)(1.0, i=0, s='x')) # E: Revealed type is 'builtins.float'
(lambda x, s, i: x)() # E: Too few arguments
(lambda: 0)(1) # E: Too many arguments
-- varargs are not handled, but it should not crash
reveal_type((lambda *k, s, i: i)(type, i=0, s='x')) # E: Revealed type is 'Any'
reveal_type((lambda s, *k, i: i)(i=0, s='x')) # E: Revealed type is 'Any'
reveal_type((lambda s, i, **k: i)(i=0, s='x')) # E: Revealed type is 'Any'
[builtins fixtures/dict.pyi]

[case testInferLambdaAsGenericFunctionArgument]
from typing import TypeVar, List, Any, Callable
t = TypeVar('t')
Expand Down