From 4869b5b4a72e10c088e806d3899bcf2367f62190 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sun, 11 Jun 2023 12:19:55 +0100 Subject: [PATCH 1/2] Allways allow returning Any form lambda --- mypy/checker.py | 1 + test-data/unit/check-flags.test | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/mypy/checker.py b/mypy/checker.py index c1c31538b7de..4593d5164b3d 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -4270,6 +4270,7 @@ def check_return_stmt(self, s: ReturnStmt) -> None: isinstance(return_type, Instance) and return_type.type.fullname == "builtins.object" ) + and not is_lambda ): self.msg.incorrectly_returning_any(return_type, s) return diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 4a43224260d1..feb4feba2c58 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -2184,3 +2184,11 @@ def f(x: bytes, y: bytearray, z: memoryview) -> None: follow_imports = skip follow_imports_for_stubs = true [builtins fixtures/dict.pyi] + +[case testReturnAnyLambda] +# flags: --warn-return-any +from typing import Any, Callable + +def cb(f: Callable[[int], int]) -> None: ... +a: Any +cb(lambda x: a) # OK From 063504c117bf9ab45c7ea3e412a0370de38af5f5 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 11 Jun 2023 12:36:07 -0700 Subject: [PATCH 2/2] also test aliased lambda --- test-data/unit/check-flags.test | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index feb4feba2c58..6ec0849146c0 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -2192,3 +2192,6 @@ from typing import Any, Callable def cb(f: Callable[[int], int]) -> None: ... a: Any cb(lambda x: a) # OK + +fn = lambda x: a +cb(fn)