Skip to content

Commit

Permalink
Add arguments to callee context in a call expression (#3116)
Browse files Browse the repository at this point in the history
Fix #3097.
  • Loading branch information
elazarg authored and JukkaL committed Aug 15, 2017
1 parent 249015a commit 69af980
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
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

0 comments on commit 69af980

Please sign in to comment.