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 18c819a7f951a..a889e7ef2f95c 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 @@ -60,3 +60,7 @@ # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1732387247 getattr(*foo, "bar") setattr(*foo, "bar", None) + +# Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1739800901 +getattr(self. + registration.registry, '__name__') diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/rules/getattr_with_constant.rs b/crates/ruff_linter/src/rules/flake8_bugbear/rules/getattr_with_constant.rs index 2a902ec414ff8..8e0bbdf992ff6 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/rules/getattr_with_constant.rs +++ b/crates/ruff_linter/src/rules/flake8_bugbear/rules/getattr_with_constant.rs @@ -91,7 +91,8 @@ pub(crate) fn getattr_with_constant( if matches!( obj, Expr::Name(_) | Expr::Attribute(_) | Expr::Subscript(_) | Expr::Call(_) - ) { + ) && !checker.locator().contains_line_break(obj.range()) + { format!("{}.{}", checker.locator().slice(obj), value) } else { // Defensively parenthesize any other expressions. For example, attribute accesses 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 51c3a9339db00..1a0d1da89b96b 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 @@ -336,4 +336,22 @@ B009_B010.py:58:8: B009 [*] Do not call `getattr` with a constant attribute valu 60 60 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1732387247 61 61 | getattr(*foo, "bar") +B009_B010.py:65:1: B009 [*] Do not call `getattr` with a constant attribute value. It is not any safer than normal property access. + | +64 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1739800901 +65 | / getattr(self. +66 | | registration.registry, '__name__') + | |_____________________________________^ B009 + | + = help: Replace `getattr` with attribute access + +ℹ Suggested fix +62 62 | setattr(*foo, "bar", None) +63 63 | +64 64 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1739800901 +65 |-getattr(self. +66 |- registration.registry, '__name__') + 65 |+(self. + 66 |+ registration.registry).__name__ +