Skip to content

Commit 8f24c69

Browse files
authored
Only count obviously non-terminating while-loops as return-ended (#8292)
1 parent d064010 commit 8f24c69

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix false negative for inconsistent-returns with while-loops.
2+
3+
Closes #8280

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from pylint import checkers
2222
from pylint.checkers import utils
23+
from pylint.checkers.base.basic_error_checker import _loop_exits_early
2324
from pylint.checkers.utils import node_frame_class
2425
from pylint.interfaces import HIGH, INFERENCE, Confidence
2526

@@ -1945,10 +1946,12 @@ def _is_node_return_ended(self, node: nodes.NodeNG) -> bool:
19451946
return True
19461947
except astroid.InferenceError:
19471948
pass
1948-
# Avoid the check inside while loop as we don't know
1949-
# if they will be completed
19501949
if isinstance(node, nodes.While):
1951-
return True
1950+
# A while-loop is considered return-ended if it has a
1951+
# truthy test and no break statements
1952+
return (node.test.bool_value() and not _loop_exits_early(node)) or any(
1953+
self._is_node_return_ended(child) for child in node.orelse
1954+
)
19521955
if isinstance(node, nodes.Raise):
19531956
return self._is_raise_node_return_ended(node)
19541957
if isinstance(node, nodes.If):

tests/functional/i/inconsistent/inconsistent_returns.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pylint: disable=missing-docstring, no-else-return, no-else-break, invalid-name, unused-variable, superfluous-parens, try-except-raise
2-
#pylint: disable=disallowed-name
2+
#pylint: disable=disallowed-name,too-few-public-methods,no-member,useless-else-on-loop
33
"""Testing inconsistent returns"""
44
import math
55
import sys
@@ -353,3 +353,52 @@ def bug_pylint_4019_wrong(x): # [inconsistent-return-statements]
353353
if x == 1:
354354
return 42
355355
assert True
356+
357+
358+
# https://github.com/PyCQA/pylint/issues/8280
359+
class A:
360+
def get_the_answer(self): # [inconsistent-return-statements]
361+
while self.is_running:
362+
self.read_message()
363+
if self.comunication_finished():
364+
return "done"
365+
366+
367+
def bug_1772_break(): # [inconsistent-return-statements]
368+
counter = 1
369+
while True:
370+
counter += 1
371+
if counter == 100:
372+
return 7
373+
if counter is None:
374+
break
375+
376+
377+
def while_break_in_for():
378+
counter = 1
379+
while True:
380+
counter += 1
381+
if counter == 100:
382+
return 7
383+
for i in range(10):
384+
if i == 5:
385+
break
386+
387+
388+
def while_break_in_while():
389+
counter = 1
390+
while True:
391+
counter += 1
392+
if counter == 100:
393+
return 7
394+
while True:
395+
if counter == 5:
396+
break
397+
398+
399+
def wait_for_apps_ready(event, main_thread):
400+
while main_thread.is_alive():
401+
if event.wait(timeout=0.1):
402+
return True
403+
else:
404+
return False

tests/functional/i/inconsistent/inconsistent_returns.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ inconsistent-return-statements:267:0:267:12:bug_3468:Either all return statement
1515
inconsistent-return-statements:277:0:277:20:bug_3468_variant:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
1616
inconsistent-return-statements:322:0:322:21:bug_pylint_3873_1:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
1717
inconsistent-return-statements:349:0:349:25:bug_pylint_4019_wrong:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
18+
inconsistent-return-statements:360:4:360:22:A.get_the_answer:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
19+
inconsistent-return-statements:367:0:367:18:bug_1772_break:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED

0 commit comments

Comments
 (0)