Skip to content

Commit

Permalink
consider-using-sys-exit is no longer emitted when exit is impor…
Browse files Browse the repository at this point in the history
…ted in the local scope.

Close #3147
  • Loading branch information
PCManticore committed Oct 11, 2019
1 parent 15bae9f commit 5dcf377
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -30,6 +30,10 @@ Release date: TBA

Close #3161

* ``consider-using-sys-exit`` is no longer emitted when `exit` is imported in the local scope.

Close #3147


What's New in Pylint 2.4.2?
===========================
Expand Down
17 changes: 12 additions & 5 deletions pylint/checkers/refactoring.py
Expand Up @@ -686,13 +686,20 @@ def visit_call(self, node):
self._check_consider_using_comprehension_constructor(node)
self._check_quit_exit_call(node)

@staticmethod
def _has_exit_in_scope(scope):
exit_func = scope.locals.get("exit")
return bool(
exit_func and isinstance(exit_func[0], (astroid.ImportFrom, astroid.Import))
)

def _check_quit_exit_call(self, node):

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)
# If we have `exit` imported from `sys` in the current or global scope, exempt this instance.
local_scope = node.scope()
if self._has_exit_in_scope(local_scope) or self._has_exit_in_scope(
node.root()
):
return
self.add_message("consider-using-sys-exit", node=node)
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/c/consider_using_sys_exit_local_scope.py
@@ -0,0 +1,5 @@
# pylint: disable=missing-docstring,import-outside-toplevel,redefined-builtin

def run():
from sys import exit
exit()

0 comments on commit 5dcf377

Please sign in to comment.