From e23d4ea027ecbc05213d049ac6dfcf726f4ab167 Mon Sep 17 00:00:00 2001 From: Victor Hugo Gomes Date: Wed, 28 May 2025 17:24:52 -0300 Subject: [PATCH] [`flake8-bugbear`] Ignore `__debug__` attribute in `B010` (#18357) ## Summary Fixes #18353 ## Test Plan Snapshot tests --- .../resources/test/fixtures/flake8_bugbear/B009_B010.py | 3 +++ .../src/rules/flake8_bugbear/rules/setattr_with_constant.rs | 5 +++++ ...ter__rules__flake8_bugbear__tests__B009_B009_B010.py.snap | 5 +++++ 3 files changed, 13 insertions(+) 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)