Skip to content

Commit e23d4ea

Browse files
authored
[flake8-bugbear] Ignore __debug__ attribute in B010 (#18357)
<!-- Thank you for contributing to Ruff/ty! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? (Please prefix with `[ty]` for ty pull requests.) - Does this pull request include references to any relevant issues? --> ## Summary Fixes #18353 <!-- What's the purpose of the change? What does it do, and why? --> ## Test Plan <!-- How was it tested? --> Snapshot tests
1 parent 452f992 commit e23d4ea

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B009_B010.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,6 @@
6767

6868
import builtins
6969
builtins.getattr(foo, "bar")
70+
71+
# Regression test for: https://github.com/astral-sh/ruff/issues/18353
72+
setattr(foo, "__debug__", 0)

crates/ruff_linter/src/rules/flake8_bugbear/rules/setattr_with_constant.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ pub(crate) fn setattr_with_constant(checker: &Checker, expr: &Expr, func: &Expr,
7474
if !is_identifier(name.to_str()) {
7575
return;
7676
}
77+
// Ignore if the attribute name is `__debug__`. Assigning to the `__debug__` property is a
78+
// `SyntaxError`.
79+
if name.to_str() == "__debug__" {
80+
return;
81+
}
7782
if is_mangled_private(name.to_str()) {
7883
return;
7984
}

crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B009_B009_B010.py.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ B009_B010.py:69:1: B009 [*] Do not call `getattr` with a constant attribute valu
364364
68 | import builtins
365365
69 | builtins.getattr(foo, "bar")
366366
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B009
367+
70 |
368+
71 | # Regression test for: https://github.com/astral-sh/ruff/issues/18353
367369
|
368370
= help: Replace `getattr` with attribute access
369371

@@ -373,3 +375,6 @@ B009_B010.py:69:1: B009 [*] Do not call `getattr` with a constant attribute valu
373375
68 68 | import builtins
374376
69 |-builtins.getattr(foo, "bar")
375377
69 |+foo.bar
378+
70 70 |
379+
71 71 | # Regression test for: https://github.com/astral-sh/ruff/issues/18353
380+
72 72 | setattr(foo, "__debug__", 0)

0 commit comments

Comments
 (0)