Skip to content

Commit

Permalink
Parenthesize multi-line attributes in B009 (#7701)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Sep 28, 2023
1 parent f452813 commit 9611f81
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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__')
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__


0 comments on commit 9611f81

Please sign in to comment.