Skip to content

Commit 3a0d45c

Browse files
authored
[flake8-debugger] Also flag sys.breakpointhook and sys.__breakpointhook__ (T100) (#16191)
## Summary Fixes #16189. Only `sys.breakpointhook` is flagged by the upstream linter: https://github.com/pylint-dev/pylint/blob/007a745c8619c2cbf59f829a8f09fc6afa6eb0f1/pylint/checkers/stdlib.py#L38 but I think it makes sense to flag [`__breakpointhook__`](https://docs.python.org/3/library/sys.html#sys.__breakpointhook__) too, as suggested in the issue because it > contain[s] the original value of breakpointhook [...] in case [it happens] to get replaced with broken or alternative objects. ## Test Plan New T100 test cases
1 parent 1f17916 commit 3a0d45c

3 files changed

Lines changed: 75 additions & 2 deletions

File tree

crates/ruff_linter/resources/test/fixtures/flake8_debugger/T100.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,21 @@
2323
enable_attach()
2424
break_into_debugger()
2525
wait_for_attach()
26+
27+
28+
# also flag `breakpointhook` from `sys` but obviously not `sys` itself. see
29+
# https://github.com/astral-sh/ruff/issues/16189
30+
import sys # ok
31+
32+
def scope():
33+
from sys import breakpointhook # error
34+
35+
breakpointhook() # error
36+
37+
def scope():
38+
from sys import __breakpointhook__ # error
39+
40+
__breakpointhook__() # error
41+
42+
sys.breakpointhook() # error
43+
sys.__breakpointhook__() # error

crates/ruff_linter/src/rules/flake8_debugger/rules/debugger.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,15 @@ fn is_debugger_call(qualified_name: &QualifiedName) -> bool {
109109
| ["builtins" | "", "breakpoint"]
110110
| ["debugpy", "breakpoint" | "listen" | "wait_for_client"]
111111
| ["ptvsd", "break_into_debugger" | "wait_for_attach"]
112+
| ["sys", "breakpointhook" | "__breakpointhook__"]
112113
)
113114
}
114115

115116
fn is_debugger_import(qualified_name: &QualifiedName) -> bool {
116117
// Constructed by taking every pattern in `is_debugger_call`, removing the last element in
117118
// each pattern, and de-duplicating the values.
118-
// As a special-case, we omit `builtins` to allow `import builtins`, which is far more general
119-
// than (e.g.) `import celery.contrib.rdb`.
119+
// As special-cases, we omit `builtins` and `sys` to allow `import builtins` and `import sys`
120+
// which are far more general than (e.g.) `import celery.contrib.rdb`.
120121
matches!(
121122
qualified_name.segments(),
122123
["pdb" | "pudb" | "ipdb" | "debugpy" | "ptvsd"]

crates/ruff_linter/src/rules/flake8_debugger/snapshots/ruff_linter__rules__flake8_debugger__tests__T100_T100.py.snap

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,57 @@ T100.py:25:1: T100 Trace found: `ptvsd.wait_for_attach` used
183183
25 | wait_for_attach()
184184
| ^^^^^^^^^^^^^^^^^ T100
185185
|
186+
187+
T100.py:33:5: T100 Import for `sys.breakpointhook` found
188+
|
189+
32 | def scope():
190+
33 | from sys import breakpointhook # error
191+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ T100
192+
34 |
193+
35 | breakpointhook() # error
194+
|
195+
196+
T100.py:35:5: T100 Trace found: `sys.breakpointhook` used
197+
|
198+
33 | from sys import breakpointhook # error
199+
34 |
200+
35 | breakpointhook() # error
201+
| ^^^^^^^^^^^^^^^^ T100
202+
36 |
203+
37 | def scope():
204+
|
205+
206+
T100.py:38:5: T100 Import for `sys.__breakpointhook__` found
207+
|
208+
37 | def scope():
209+
38 | from sys import __breakpointhook__ # error
210+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ T100
211+
39 |
212+
40 | __breakpointhook__() # error
213+
|
214+
215+
T100.py:40:5: T100 Trace found: `sys.__breakpointhook__` used
216+
|
217+
38 | from sys import __breakpointhook__ # error
218+
39 |
219+
40 | __breakpointhook__() # error
220+
| ^^^^^^^^^^^^^^^^^^^^ T100
221+
41 |
222+
42 | sys.breakpointhook() # error
223+
|
224+
225+
T100.py:42:1: T100 Trace found: `sys.breakpointhook` used
226+
|
227+
40 | __breakpointhook__() # error
228+
41 |
229+
42 | sys.breakpointhook() # error
230+
| ^^^^^^^^^^^^^^^^^^^^ T100
231+
43 | sys.__breakpointhook__() # error
232+
|
233+
234+
T100.py:43:1: T100 Trace found: `sys.__breakpointhook__` used
235+
|
236+
42 | sys.breakpointhook() # error
237+
43 | sys.__breakpointhook__() # error
238+
| ^^^^^^^^^^^^^^^^^^^^^^^^ T100
239+
|

0 commit comments

Comments
 (0)