Skip to content

Commit e694467

Browse files
fix: handle inconsistent-return-statements false positive with quit()/exit() (#10572)
Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
1 parent ed6b306 commit e694467

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix false positive ``inconsistent-return-statements`` when using ``quit()`` or ``exit()`` functions.
2+
3+
Closes #10508

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,11 +2021,11 @@ def _is_node_return_ended(self, node: nodes.NodeNG) -> bool:
20212021
# Recursion base case
20222022
return True
20232023
case nodes.Call():
2024+
if utils.is_terminating_func(node):
2025+
return True
20242026
return any(
2025-
(
2026-
isinstance(maybe_func, (nodes.FunctionDef, bases.BoundMethod))
2027-
and self._is_function_def_never_returning(maybe_func)
2028-
)
2027+
isinstance(maybe_func, (nodes.FunctionDef, bases.BoundMethod))
2028+
and self._is_function_def_never_returning(maybe_func)
20292029
for maybe_func in utils.infer_all(node.func)
20302030
)
20312031
case nodes.While():
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# pylint: disable=missing-docstring, invalid-name, unused-argument, consider-using-sys-exit, no-else-return
2+
"""Test that quit() and exit() are handled consistently with sys.exit()"""
3+
4+
import sys
5+
6+
# These functions should not trigger inconsistent-return-statements
7+
# because quit, exit, and sys.exit are never-returning functions (#10508)
8+
9+
def func_with_quit_1(i):
10+
"""quit() in else branch should be treated like sys.exit()"""
11+
if i == 1:
12+
return i
13+
quit(1)
14+
15+
def func_with_quit_2(i):
16+
"""quit() in if branch should be treated like sys.exit()"""
17+
if i == 1:
18+
quit(1)
19+
return 1
20+
21+
def func_with_exit_1(i):
22+
"""exit() in else branch should be treated like sys.exit()"""
23+
if i == 1:
24+
return i
25+
exit(1)
26+
27+
def func_with_exit_2(i):
28+
"""exit() in if branch should be treated like sys.exit()"""
29+
if i == 1:
30+
exit(1)
31+
return 1
32+
33+
def func_with_sys_exit_1(i):
34+
"""sys.exit() in else branch - baseline test"""
35+
if i == 1:
36+
return i
37+
sys.exit(1)
38+
39+
def func_with_sys_exit_2(i):
40+
"""sys.exit() in if branch - baseline test"""
41+
if i == 1:
42+
sys.exit(1)
43+
return 1
44+
45+
# Test mixed usage
46+
def func_mixed_exit_methods(i):
47+
"""Using different exit methods should all work consistently"""
48+
if i == 1:
49+
return "one"
50+
if i == 2:
51+
quit()
52+
if i == 3:
53+
exit()
54+
sys.exit()
55+
56+
# This should trigger inconsistent-return-statements
57+
def func_inconsistent_example(i): # [inconsistent-return-statements]
58+
"""This should trigger the warning as a negative test case"""
59+
if i == 1:
60+
return i
61+
print("Not exiting, just printing")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
inconsistent-return-statements:57:0:57:29:func_inconsistent_example:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED

0 commit comments

Comments
 (0)