From 214eb0d92a667993c2ed7c4532750e72603b4b00 Mon Sep 17 00:00:00 2001 From: Xiao Liang Date: Wed, 21 Aug 2019 01:15:21 +0800 Subject: [PATCH 1/2] test(No return rule): add test - looping over generator case For unconditionally looping over non-exhaustive generators, it should be acceptable to have no `return` statement. For unconditionally looping over exhaustive generators, the function should have `return` statement. --- test-data/unit/check-warnings.test | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test-data/unit/check-warnings.test b/test-data/unit/check-warnings.test index 99b611529980..d968e770a570 100644 --- a/test-data/unit/check-warnings.test +++ b/test-data/unit/check-warnings.test @@ -100,6 +100,31 @@ def j() -> int: [out] main:7: error: Missing return statement +[case testNoReturnForExhaustiveGenerator] +# flags: --warn-no-return +from itertools import count, cycle +def h() -> int: + for _i in count(1): + if bool(): + return 1 + +def i() -> int: + for _i in range(3): + if bool(): + return 1 + if bool(): + break + +def j() -> int: + for _i in cycle('ABC'): + if bool(): + return 1 + if bool(): + continue +[builtins fixtures/list.pyi] +[out] +main:8: error: Missing return statement + [case testNoReturnExcept] # flags: --warn-no-return def f() -> int: From 8e3a7dc0d74116eb7b22c37fea1b749808037c91 Mon Sep 17 00:00:00 2001 From: Xiao Liang Date: Wed, 21 Aug 2019 01:36:03 +0800 Subject: [PATCH 2/2] test(No return rule/testNoReturnForExhaustiveGenerator): correcting the test case --- test-data/unit/check-warnings.test | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test-data/unit/check-warnings.test b/test-data/unit/check-warnings.test index d968e770a570..0d425ff4dcc5 100644 --- a/test-data/unit/check-warnings.test +++ b/test-data/unit/check-warnings.test @@ -102,21 +102,21 @@ main:7: error: Missing return statement [case testNoReturnForExhaustiveGenerator] # flags: --warn-no-return -from itertools import count, cycle +from itertools import count, cycle, repeat def h() -> int: - for _i in count(1): + for _i in count(1): # count is non-exhaustive if bool(): return 1 def i() -> int: - for _i in range(3): + for _i in repeat(10, 3): if bool(): return 1 if bool(): break def j() -> int: - for _i in cycle('ABC'): + for _i in cycle((1, 2, 3)): # cycle is non-exhaustive if bool(): return 1 if bool():