Skip to content

Commit 5e5f48d

Browse files
DanielNoordcdce8pPierre-Sassoulas
authored
Add forgotten-debug-statement checker (#4771)
* Add ``no-breakpoint`` checker this adds a checker for calls to ``breakpoint()``, ``pdb.set_trace()``, or ``sys.breakpointhook()``. Closes #3692 Co-authored-by: Marc Mueller <[email protected]> Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent a051aa8 commit 5e5f48d

File tree

6 files changed

+34
-0
lines changed

6 files changed

+34
-0
lines changed

ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ Release date: TBA
4848

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

51+
* Added ``forgotten-debug-statement``: Emitted when ``breakpoint``, ``pdb.set_trace`` or ``sys.breakpointhook`` calls are found
52+
53+
Closes #3692
54+
5155

5256
What's New in Pylint 2.9.6?
5357
===========================

doc/whatsnew/2.10.rst

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ New checkers
2424

2525
Closes #4365
2626

27+
* Added ``forgotten-debug-statement``: Emitted when ``breakpoint``, ``pdb.set_trace`` or ``sys.breakpointhook`` calls are found
28+
29+
Closes #3692
30+
2731

2832
Extensions
2933
==========

pylint/checkers/stdlib.py

+10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
SUBPROCESS_POPEN = "subprocess.Popen"
5656
SUBPROCESS_RUN = "subprocess.run"
5757
OPEN_MODULE = "_io"
58+
DEBUG_BREAKPOINTS = ("builtins.breakpoint", "sys.breakpointhook", "pdb.set_trace")
5859

5960

6061
DEPRECATED_MODULES = {
@@ -434,6 +435,12 @@ class StdlibChecker(DeprecatedMixin, BaseChecker):
434435
"Using the system default implicitly can create problems on other operating systems. "
435436
"See https://www.python.org/dev/peps/pep-0597/",
436437
),
438+
"W1515": (
439+
"Leaving functions creating breakpoints in production code is not recommended",
440+
"forgotten-debug-statement",
441+
"Calls to breakpoint(), sys.breakpointhook() and pdb.set_trace() should be removed "
442+
"from code that is not actively being debugged.",
443+
),
437444
}
438445

439446
def __init__(self, linter=None):
@@ -495,6 +502,7 @@ def _check_shallow_copy_environ(self, node):
495502
"subprocess-run-check",
496503
"deprecated-class",
497504
"unspecified-encoding",
505+
"forgotten-debug-statement",
498506
)
499507
def visit_call(self, node):
500508
"""Visit a Call node."""
@@ -531,6 +539,8 @@ def visit_call(self, node):
531539
self._check_env_function(node, inferred)
532540
elif name == SUBPROCESS_RUN:
533541
self._check_for_check_kw_in_run(node)
542+
elif name in DEBUG_BREAKPOINTS:
543+
self.add_message("forgotten-debug-statement", node=node)
534544
self.check_deprecated_method(node, inferred)
535545
except astroid.InferenceError:
536546
return
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# pylint: disable=missing-docstring
2+
3+
import pdb
4+
import sys
5+
6+
breakpoint() # [forgotten-debug-statement]
7+
sys.breakpointhook() # [forgotten-debug-statement]
8+
pdb.set_trace() # [forgotten-debug-statement]
9+
b = breakpoint
10+
b() # [forgotten-debug-statement]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[testoptions]
2+
min_pyver=3.7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
forgotten-debug-statement:6:0::"Leaving functions creating breakpoints in production code is not recommended"
2+
forgotten-debug-statement:7:0::"Leaving functions creating breakpoints in production code is not recommended"
3+
forgotten-debug-statement:8:0::"Leaving functions creating breakpoints in production code is not recommended"
4+
forgotten-debug-statement:10:0::"Leaving functions creating breakpoints in production code is not recommended"

0 commit comments

Comments
 (0)