From db7a9b0af718080d1c02f9f9b3f02a0bd66681e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Thu, 29 Jul 2021 12:27:49 +0200 Subject: [PATCH] Add ``no-breakpoint`` checker This adds a checker for calls to ``breakpoint()`` or ``sys.breakpointhook()``. This closes #3692 --- ChangeLog | 4 ++++ doc/whatsnew/2.10.rst | 4 ++++ pylint/checkers/stdlib.py | 10 ++++++++++ tests/functional/n/no/no_breakpoint_py37.py | 6 ++++++ tests/functional/n/no/no_breakpoint_py37.rc | 2 ++ tests/functional/n/no/no_breakpoint_py37.txt | 2 ++ 6 files changed, 28 insertions(+) create mode 100644 tests/functional/n/no/no_breakpoint_py37.py create mode 100644 tests/functional/n/no/no_breakpoint_py37.rc create mode 100644 tests/functional/n/no/no_breakpoint_py37.txt diff --git a/ChangeLog b/ChangeLog index 43886103f6..a8fb55491e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -37,6 +37,10 @@ Release date: TBA Closes #3878 +* Added ``no-breakpoint``: Emitted when breakpoint() or sys.breakpointhook() calls are found + + Closes #3692 + What's New in Pylint 2.9.6? diff --git a/doc/whatsnew/2.10.rst b/doc/whatsnew/2.10.rst index 7d7312153f..746672adfe 100644 --- a/doc/whatsnew/2.10.rst +++ b/doc/whatsnew/2.10.rst @@ -16,6 +16,10 @@ New checkers Closes #3826 +* Added ``no-breakpoint``: Emitted when breakpoint() or sys.breakpointhook() calls are found + + Closes #3692 + Other Changes ============= diff --git a/pylint/checkers/stdlib.py b/pylint/checkers/stdlib.py index 1fba67afd4..7b4f80ad9b 100644 --- a/pylint/checkers/stdlib.py +++ b/pylint/checkers/stdlib.py @@ -55,6 +55,7 @@ SUBPROCESS_POPEN = "subprocess.Popen" SUBPROCESS_RUN = "subprocess.run" OPEN_MODULE = "_io" +BREAKPOINTS = ("builtins.breakpoint", "sys.breakpointhook") DEPRECATED_MODULES = { @@ -434,6 +435,12 @@ class StdlibChecker(DeprecatedMixin, BaseChecker): "Using the system default implicitly can create problems on other operating systems. " "See https://www.python.org/dev/peps/pep-0597/", ), + "W1515": ( + "Leaving breakpoint() or sys.breakpointhook() in production code is unrecommended", + "no-breakpoint", + "Calls to breakpoint() and sys.breakpointhook() should be removed from code that " + "is not actively being debugged.", + ), } def __init__(self, linter=None): @@ -495,6 +502,7 @@ def _check_shallow_copy_environ(self, node): "subprocess-run-check", "deprecated-class", "unspecified-encoding", + "no-breakpoint", ) def visit_call(self, node): """Visit a Call node.""" @@ -531,6 +539,8 @@ def visit_call(self, node): self._check_env_function(node, inferred) elif name == SUBPROCESS_RUN: self._check_for_check_kw_in_run(node) + elif name in BREAKPOINTS: + self.add_message("no-breakpoint", node=node) self.check_deprecated_method(node, inferred) except astroid.InferenceError: return diff --git a/tests/functional/n/no/no_breakpoint_py37.py b/tests/functional/n/no/no_breakpoint_py37.py new file mode 100644 index 0000000000..7e8a63b2e5 --- /dev/null +++ b/tests/functional/n/no/no_breakpoint_py37.py @@ -0,0 +1,6 @@ +# pylint: disable=missing-docstring + +import sys + +breakpoint() # [no-breakpoint] +sys.breakpointhook() # [no-breakpoint] diff --git a/tests/functional/n/no/no_breakpoint_py37.rc b/tests/functional/n/no/no_breakpoint_py37.rc new file mode 100644 index 0000000000..a17bb22daf --- /dev/null +++ b/tests/functional/n/no/no_breakpoint_py37.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.7 diff --git a/tests/functional/n/no/no_breakpoint_py37.txt b/tests/functional/n/no/no_breakpoint_py37.txt new file mode 100644 index 0000000000..3e61521a03 --- /dev/null +++ b/tests/functional/n/no/no_breakpoint_py37.txt @@ -0,0 +1,2 @@ +no-breakpoint:5:0::"Leaving breakpoint() or sys.breakpointhook() in production code is unrecommended" +no-breakpoint:6:0::"Leaving breakpoint() or sys.breakpointhook() in production code is unrecommended"