Skip to content

Commit

Permalink
Add forgotten-debug-statement checker (pylint-dev#4771)
Browse files Browse the repository at this point in the history
* Add ``no-breakpoint`` checker this adds a checker for calls to ``breakpoint()``, ``pdb.set_trace()``, or ``sys.breakpointhook()``.

Closes pylint-dev#3692

Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
3 people committed Jul 29, 2021
1 parent a051aa8 commit 5e5f48d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -48,6 +48,10 @@ Release date: TBA

* Extended ``consider-using-tuple`` check to cover ``in`` comparisons.

* Added ``forgotten-debug-statement``: Emitted when ``breakpoint``, ``pdb.set_trace`` or ``sys.breakpointhook`` calls are found

Closes #3692


What's New in Pylint 2.9.6?
===========================
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.10.rst
Expand Up @@ -24,6 +24,10 @@ New checkers

Closes #4365

* Added ``forgotten-debug-statement``: Emitted when ``breakpoint``, ``pdb.set_trace`` or ``sys.breakpointhook`` calls are found

Closes #3692


Extensions
==========
Expand Down
10 changes: 10 additions & 0 deletions pylint/checkers/stdlib.py
Expand Up @@ -55,6 +55,7 @@
SUBPROCESS_POPEN = "subprocess.Popen"
SUBPROCESS_RUN = "subprocess.run"
OPEN_MODULE = "_io"
DEBUG_BREAKPOINTS = ("builtins.breakpoint", "sys.breakpointhook", "pdb.set_trace")


DEPRECATED_MODULES = {
Expand Down Expand Up @@ -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 functions creating breakpoints in production code is not recommended",
"forgotten-debug-statement",
"Calls to breakpoint(), sys.breakpointhook() and pdb.set_trace() should be removed "
"from code that is not actively being debugged.",
),
}

def __init__(self, linter=None):
Expand Down Expand Up @@ -495,6 +502,7 @@ def _check_shallow_copy_environ(self, node):
"subprocess-run-check",
"deprecated-class",
"unspecified-encoding",
"forgotten-debug-statement",
)
def visit_call(self, node):
"""Visit a Call node."""
Expand Down Expand Up @@ -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 DEBUG_BREAKPOINTS:
self.add_message("forgotten-debug-statement", node=node)
self.check_deprecated_method(node, inferred)
except astroid.InferenceError:
return
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/f/forgotten_debug_statement_py37.py
@@ -0,0 +1,10 @@
# pylint: disable=missing-docstring

import pdb
import sys

breakpoint() # [forgotten-debug-statement]
sys.breakpointhook() # [forgotten-debug-statement]
pdb.set_trace() # [forgotten-debug-statement]
b = breakpoint
b() # [forgotten-debug-statement]
2 changes: 2 additions & 0 deletions tests/functional/f/forgotten_debug_statement_py37.rc
@@ -0,0 +1,2 @@
[testoptions]
min_pyver=3.7
4 changes: 4 additions & 0 deletions tests/functional/f/forgotten_debug_statement_py37.txt
@@ -0,0 +1,4 @@
forgotten-debug-statement:6:0::"Leaving functions creating breakpoints in production code is not recommended"
forgotten-debug-statement:7:0::"Leaving functions creating breakpoints in production code is not recommended"
forgotten-debug-statement:8:0::"Leaving functions creating breakpoints in production code is not recommended"
forgotten-debug-statement:10:0::"Leaving functions creating breakpoints in production code is not recommended"

0 comments on commit 5e5f48d

Please sign in to comment.