mirror of https://github.com/astral-sh/ruff
Parenthesize multi-line attributes in B009 (#7701)
Closes https://github.com/astral-sh/ruff/issues/7455#issuecomment-1739800901.
This commit is contained in:
parent
f45281345d
commit
9611f8134f
|
|
@ -60,3 +60,7 @@ assert getattr(func, '_rpc')is True
|
||||||
# Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1732387247
|
# Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1732387247
|
||||||
getattr(*foo, "bar")
|
getattr(*foo, "bar")
|
||||||
setattr(*foo, "bar", None)
|
setattr(*foo, "bar", None)
|
||||||
|
|
||||||
|
# Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1739800901
|
||||||
|
getattr(self.
|
||||||
|
registration.registry, '__name__')
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,8 @@ pub(crate) fn getattr_with_constant(
|
||||||
if matches!(
|
if matches!(
|
||||||
obj,
|
obj,
|
||||||
Expr::Name(_) | Expr::Attribute(_) | Expr::Subscript(_) | Expr::Call(_)
|
Expr::Name(_) | Expr::Attribute(_) | Expr::Subscript(_) | Expr::Call(_)
|
||||||
) {
|
) && !checker.locator().contains_line_break(obj.range())
|
||||||
|
{
|
||||||
format!("{}.{}", checker.locator().slice(obj), value)
|
format!("{}.{}", checker.locator().slice(obj), value)
|
||||||
} else {
|
} else {
|
||||||
// Defensively parenthesize any other expressions. For example, attribute accesses
|
// Defensively parenthesize any other expressions. For example, attribute accesses
|
||||||
|
|
|
||||||
|
|
@ -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
|
60 60 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1732387247
|
||||||
61 61 | getattr(*foo, "bar")
|
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__
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue