Skip to content

Commit

Permalink
Only raising NotImplementedError is a stub now
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Thiel committed Apr 17, 2024
1 parent 214e945 commit 8ccfc77
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -141,7 +141,6 @@ fn move_initialization(
indexer: &Indexer,
generator: Generator,
) -> Option<Fix> {
println!("{:?}", function_def.body);
let mut body = function_def.body.iter().peekable();

// Avoid attempting to fix single-line functions.
Expand Down Expand Up @@ -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,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()

0 comments on commit 8ccfc77

Please sign in to comment.