Skip to content

Commit

Permalink
consider-using-sys-exit is exempted when exit() is imported fro…
Browse files Browse the repository at this point in the history
…m `sys`

Close #3145
  • Loading branch information
PCManticore committed Sep 30, 2019
1 parent ae3d421 commit 1d3b07a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -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?
===========================
Expand Down
10 changes: 9 additions & 1 deletion pylint/checkers/refactoring.py
Expand Up @@ -35,6 +35,7 @@
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):
Expand Down Expand Up @@ -686,7 +687,14 @@ def visit_call(self, node):
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):
Expand Down
5 changes: 5 additions & 0 deletions 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)

0 comments on commit 1d3b07a

Please sign in to comment.