From 95404278df53e29e4802dba8db0ff152b5d56496 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 23 Sep 2023 15:00:28 -0400 Subject: [PATCH] Avoid flagging B009 and B010 on starred expressions --- .../resources/test/fixtures/flake8_bugbear/B009_B010.py | 4 ++++ .../rules/flake8_bugbear/rules/getattr_with_constant.rs | 6 ++++++ .../rules/flake8_bugbear/rules/setattr_with_constant.rs | 7 +++++++ ...r__rules__flake8_bugbear__tests__B009_B009_B010.py.snap | 5 +++++ 4 files changed, 22 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 3dc26f8ecb060..18c819a7f951a 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 @@ -56,3 +56,7 @@ # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722458885 assert getattr(func, '_rpc')is True + +# Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1732387247 +getattr(*foo, "bar") +setattr(*foo, "bar", None) 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 6a7ad84d0f402..99097b4137d10 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 @@ -64,6 +64,9 @@ pub(crate) fn getattr_with_constant( let [obj, arg] = args else { return; }; + if obj.is_starred_expr() { + return; + } let Expr::Constant(ast::ExprConstant { value: Constant::Str(ast::StringConstant { value, .. }), .. @@ -77,6 +80,9 @@ pub(crate) fn getattr_with_constant( if is_mangled_private(value) { return; } + if !checker.semantic().is_builtin("getattr") { + return; + } let mut diagnostic = Diagnostic::new(GetAttrWithConstant, expr.range()); if checker.patch(diagnostic.kind.rule()) { 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 8b615947f7008..5dfbd8d2aa02b 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 @@ -78,6 +78,9 @@ pub(crate) fn setattr_with_constant( let [obj, name, value] = args else { return; }; + if obj.is_starred_expr() { + return; + } let Expr::Constant(ast::ExprConstant { value: Constant::Str(name), .. @@ -91,6 +94,10 @@ pub(crate) fn setattr_with_constant( if is_mangled_private(name) { return; } + if !checker.semantic().is_builtin("setattr") { + return; + } + // We can only replace a `setattr` call (which is an `Expr`) with an assignment // (which is a `Stmt`) if the `Expr` is already being used as a `Stmt` // (i.e., it's directly within an `Stmt::Expr`). 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 a88d7061b3359..51c3a9339db00 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 @@ -321,6 +321,8 @@ B009_B010.py:58:8: B009 [*] Do not call `getattr` with a constant attribute valu 57 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722458885 58 | assert getattr(func, '_rpc')is True | ^^^^^^^^^^^^^^^^^^^^^ B009 +59 | +60 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1732387247 | = help: Replace `getattr` with attribute access @@ -330,5 +332,8 @@ B009_B010.py:58:8: B009 [*] Do not call `getattr` with a constant attribute valu 57 57 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722458885 58 |-assert getattr(func, '_rpc')is True 58 |+assert func._rpc is True +59 59 | +60 60 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1732387247 +61 61 | getattr(*foo, "bar")