summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2019-09-30 09:16:57 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2019-09-30 09:16:57 +0200
commit1d3b07a7280a1aad5eed1e937b8161113d1f049e (patch)
tree47ae4790520f7db0f3afe24762cd6867da85ca7b
parentae3d4211175e27c6e45d4f67e764425655d7e18e (diff)
downloadpylint-git-1d3b07a7280a1aad5eed1e937b8161113d1f049e.tar.gz
``consider-using-sys-exit`` is exempted when `exit()` is imported from `sys`
Close #3145
-rw-r--r--ChangeLog4
-rw-r--r--pylint/checkers/refactoring.py10
-rw-r--r--tests/functional/c/consider_using_sys_exit_exempted.py5
3 files changed, 18 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 0b3686ac9..cb8b79461 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -14,6 +14,10 @@ Release date: TBA
Close #2930
+* ``consider-using-sys-exit`` is exempted when `exit()` is imported from `sys`
+
+ Close #3145
+
What's New in Pylint 2.4.1?
===========================
diff --git a/pylint/checkers/refactoring.py b/pylint/checkers/refactoring.py
index 18f2642f9..e63d18081 100644
--- a/pylint/checkers/refactoring.py
+++ b/pylint/checkers/refactoring.py
@@ -35,6 +35,7 @@ from pylint import utils as lint_utils
from pylint.checkers import utils
KNOWN_INFINITE_ITERATORS = {"itertools.count"}
+BUILTIN_EXIT_FUNCS = frozenset(("quit", "exit"))
def _if_statement_is_always_returning(if_node, returning_node_class):
@@ -686,7 +687,14 @@ class RefactoringChecker(checkers.BaseTokenChecker):
self._check_quit_exit_call(node)
def _check_quit_exit_call(self, node):
- if isinstance(node.func, astroid.Name) and node.func.name in ("quit", "exit"):
+ if isinstance(node.func, astroid.Name) and node.func.name in BUILTIN_EXIT_FUNCS:
+ # If we have `exit` imported from `sys` in the scope, exempt this instance.
+ scope = node.root()
+ exit_func = scope.locals.get("exit")
+ if exit_func and isinstance(
+ exit_func[0], (astroid.ImportFrom, astroid.Import)
+ ):
+ return
self.add_message("consider-using-sys-exit", node=node)
def _check_raising_stopiteration_in_generator_next_call(self, node):
diff --git a/tests/functional/c/consider_using_sys_exit_exempted.py b/tests/functional/c/consider_using_sys_exit_exempted.py
new file mode 100644
index 000000000..ba66989cb
--- /dev/null
+++ b/tests/functional/c/consider_using_sys_exit_exempted.py
@@ -0,0 +1,5 @@
+# pylint: disable=missing-docstring,redefined-builtin
+
+from sys import exit
+
+exit(0)