diff options
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | pylint/checkers/refactoring.py | 10 | ||||
-rw-r--r-- | tests/functional/c/consider_using_sys_exit_exempted.py | 5 |
3 files changed, 18 insertions, 1 deletions
@@ -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) |