diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B009_B010.py b/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B009_B010.py index c47538b55e..ce6e5c291e 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B009_B010.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B009_B010.py @@ -67,3 +67,6 @@ getattr(self. import builtins builtins.getattr(foo, "bar") + +# Regression test for: https://github.com/astral-sh/ruff/issues/18353 +setattr(foo, "__debug__", 0) diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/rules/setattr_with_constant.rs b/crates/ruff_linter/src/rules/flake8_bugbear/rules/setattr_with_constant.rs index cfc2557341..a83710800d 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/rules/setattr_with_constant.rs +++ b/crates/ruff_linter/src/rules/flake8_bugbear/rules/setattr_with_constant.rs @@ -74,6 +74,11 @@ pub(crate) fn setattr_with_constant(checker: &Checker, expr: &Expr, func: &Expr, if !is_identifier(name.to_str()) { return; } + // Ignore if the attribute name is `__debug__`. Assigning to the `__debug__` property is a + // `SyntaxError`. + if name.to_str() == "__debug__" { + return; + } if is_mangled_private(name.to_str()) { return; } diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B009_B009_B010.py.snap b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B009_B009_B010.py.snap index 5748ea4eb7..8bddcba720 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B009_B009_B010.py.snap +++ b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B009_B009_B010.py.snap @@ -364,6 +364,8 @@ B009_B010.py:69:1: B009 [*] Do not call `getattr` with a constant attribute valu 68 | import builtins 69 | builtins.getattr(foo, "bar") | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B009 +70 | +71 | # Regression test for: https://github.com/astral-sh/ruff/issues/18353 | = help: Replace `getattr` with attribute access @@ -373,3 +375,6 @@ B009_B010.py:69:1: B009 [*] Do not call `getattr` with a constant attribute valu 68 68 | import builtins 69 |-builtins.getattr(foo, "bar") 69 |+foo.bar +70 70 | +71 71 | # Regression test for: https://github.com/astral-sh/ruff/issues/18353 +72 72 | setattr(foo, "__debug__", 0)