From 8ccfc778370af9b0515642572c58be67832243f2 Mon Sep 17 00:00:00 2001 From: Philipp Thiel Date: Wed, 17 Apr 2024 10:55:29 +0200 Subject: [PATCH] Only raising NotImplementedError is a stub now --- .../test/fixtures/flake8_bugbear/B006_8.py | 6 ++++- .../rules/mutable_argument_default.rs | 16 +++++++++--- ...flake8_bugbear__tests__B006_B006_8.py.snap | 26 ++++++++++++++++--- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B006_8.py b/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B006_8.py index 16a7cc9f8da4c..580f52843891d 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B006_8.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B006_8.py @@ -4,5 +4,9 @@ def foo(a: list = []): def bar(a: dict = {}): """ This one also has a docstring""" - raise NotImplementedError("") + raise NotImplementedError("and has some text in here") + +def baz(a: list = []): + """This one raises a different exception""" + raise IndexError() diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_argument_default.rs b/crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_argument_default.rs index 00cd533267ec0..3e3bc0c96b944 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_argument_default.rs +++ b/crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_argument_default.rs @@ -2,7 +2,7 @@ use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::is_docstring_stmt; use ruff_python_ast::name::QualifiedName; -use ruff_python_ast::{self as ast, Expr, Parameter, ParameterWithDefault, Stmt}; +use ruff_python_ast::{self as ast, Expr, Parameter, ParameterWithDefault, Stmt, StmtRaise}; use ruff_python_codegen::{Generator, Stylist}; use ruff_python_index::Indexer; use ruff_python_semantic::analyze::typing::{is_immutable_annotation, is_mutable_expr}; @@ -141,7 +141,6 @@ fn move_initialization( indexer: &Indexer, generator: Generator, ) -> Option { - println!("{:?}", function_def.body); let mut body = function_def.body.iter().peekable(); // Avoid attempting to fix single-line functions. @@ -224,7 +223,18 @@ fn is_stub(function_def: &ast::StmtFunctionDef) -> bool { Expr::StringLiteral(_) | Expr::EllipsisLiteral(_) ) } - Stmt::Raise(_) => true, + Stmt::Raise(StmtRaise { + range: _, + exc: exception, + cause: _, + }) => exception.as_ref().is_some_and(|exc| { + exc.as_call_expr().is_some_and(|exc| { + exc.func + .as_ref() + .as_name_expr() + .is_some_and(|name| name.id == "NotImplementedError") + }) + }), _ => false, }) } diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B006_B006_8.py.snap b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B006_B006_8.py.snap index 76cb1bfaf25ae..77347cd56a832 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B006_B006_8.py.snap +++ b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B006_B006_8.py.snap @@ -21,7 +21,7 @@ B006_8.py:5:19: B006 [*] Do not use mutable data structures for argument default 5 | def bar(a: dict = {}): | ^^ B006 6 | """ This one also has a docstring""" -7 | raise NotImplementedError("") +7 | raise NotImplementedError("and has some text in here") | = help: Replace with `None`; initialize within function @@ -32,5 +32,25 @@ B006_8.py:5:19: B006 [*] Do not use mutable data structures for argument default 5 |-def bar(a: dict = {}): 5 |+def bar(a: dict = None): 6 6 | """ This one also has a docstring""" -7 7 | raise NotImplementedError("") -8 8 | +7 7 | raise NotImplementedError("and has some text in here") +8 8 | + +B006_8.py:10:19: B006 [*] Do not use mutable data structures for argument defaults + | +10 | def baz(a: list = []): + | ^^ B006 +11 | """This one raises a different exception""" +12 | raise IndexError() + | + = help: Replace with `None`; initialize within function + +ℹ Unsafe fix +7 7 | raise NotImplementedError("and has some text in here") +8 8 | +9 9 | +10 |-def baz(a: list = []): + 10 |+def baz(a: list = None): +11 11 | """This one raises a different exception""" + 12 |+ if a is None: + 13 |+ a = [] +12 14 | raise IndexError()