From 771003802ffb976bc6c0aad9c3ff1135b9209097 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Wed, 15 Nov 2017 21:17:38 -0800 Subject: [PATCH 1/2] Do semantic analysis on lambda's bodies Lambdas were seen as dynamic functions when going to report errors. This changes them to not be dynamic functions and report semantic analysis errors in their bodies. Fixes #4098 --- mypy/nodes.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mypy/nodes.py b/mypy/nodes.py index fd060e0e7209..dcbf1e14e015 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -1480,6 +1480,9 @@ def expr(self) -> Expression: def accept(self, visitor: ExpressionVisitor[T]) -> T: return visitor.visit_lambda_expr(self) + def is_dynamic(self) -> bool: + return False + class ListExpr(Expression): """List literal expression [...].""" From ad1ddd2d2aa99d890b0691b956bd9873f70b4c02 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Thu, 16 Nov 2017 12:40:37 -0800 Subject: [PATCH 2/2] Add tests --- test-data/unit/check-functions.test | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test-data/unit/check-functions.test b/test-data/unit/check-functions.test index 5e99613eeba2..8001cec3c213 100644 --- a/test-data/unit/check-functions.test +++ b/test-data/unit/check-functions.test @@ -2212,3 +2212,19 @@ def i() -> List[Union[str, int]]: return x [builtins fixtures/dict.pyi] + +[case testLambdaSemanal] +f = lambda: xyz +[out] +main:1: error: Name 'xyz' is not defined + +[case testLambdaTypeCheck] +f = lambda: 1 + '1' +[out] +main:1: error: Unsupported operand types for + ("int" and "str") + +[case testLambdaTypeInference] +f = lambda: 5 +reveal_type(f) +[out] +main:2: error: Revealed type is 'def () -> builtins.int' \ No newline at end of file