Skip to content

Commit

Permalink
Revert the B027 autofix logic (astral-sh#4310)
Browse files Browse the repository at this point in the history
  • Loading branch information
aacunningham authored May 9, 2023
1 parent 03f141f commit 48e1852
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 258 deletions.

This file was deleted.

1 change: 0 additions & 1 deletion crates/ruff/src/rules/flake8_bugbear/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ mod tests {
#[test_case(Rule::StarArgUnpackingAfterKeywordArg, Path::new("B026.py"); "B026")]
#[test_case(Rule::EmptyMethodWithoutAbstractDecorator, Path::new("B027.py"); "B027")]
#[test_case(Rule::EmptyMethodWithoutAbstractDecorator, Path::new("B027.pyi"); "B027_pyi")]
#[test_case(Rule::EmptyMethodWithoutAbstractDecorator, Path::new("B027_extended.py"); "B027_extended")]
#[test_case(Rule::NoExplicitStacklevel, Path::new("B028.py"); "B028")]
#[test_case(Rule::ExceptWithEmptyTuple, Path::new("B029.py"); "B029")]
#[test_case(Rule::ExceptWithNonExceptionClasses, Path::new("B030.py"); "B030")]
Expand Down
56 changes: 4 additions & 52 deletions crates/ruff/src/rules/flake8_bugbear/rules/abstract_base_class.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
use anyhow::{anyhow, Result};
use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword, Stmt, StmtKind};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix, Violation};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::source_code::{Locator, Stylist};
use ruff_python_ast::whitespace::indentation;
use ruff_python_semantic::analyze::visibility::{is_abstract, is_overload};
use ruff_python_semantic::context::Context;

use crate::autofix::actions::get_or_import_symbol;
use crate::checkers::ast::Checker;
use crate::importer::Importer;
use crate::registry::Rule;

#[violation]
Expand All @@ -25,24 +20,19 @@ impl Violation for AbstractBaseClassWithoutAbstractMethod {
format!("`{name}` is an abstract base class, but it has no abstract methods")
}
}

#[violation]
pub struct EmptyMethodWithoutAbstractDecorator {
pub name: String,
}

impl AlwaysAutofixableViolation for EmptyMethodWithoutAbstractDecorator {
impl Violation for EmptyMethodWithoutAbstractDecorator {
#[derive_message_formats]
fn message(&self) -> String {
let EmptyMethodWithoutAbstractDecorator { name } = self;
format!(
"`{name}` is an empty method in an abstract base class, but has no abstract decorator"
)
}

fn autofix_title(&self) -> String {
"Add the `@abstractmethod` decorator".to_string()
}
}

fn is_abc_class(context: &Context, bases: &[Expr], keywords: &[Keyword]) -> bool {
Expand Down Expand Up @@ -77,32 +67,6 @@ fn is_empty_body(body: &[Stmt]) -> bool {
})
}

fn fix_abstractmethod_missing(
context: &Context,
importer: &Importer,
locator: &Locator,
stylist: &Stylist,
stmt: &Stmt,
) -> Result<Fix> {
let indent = indentation(locator, stmt).ok_or(anyhow!("Unable to detect indentation"))?;
let (import_edit, binding) = get_or_import_symbol(
"abc",
"abstractmethod",
stmt.start(),
context,
importer,
locator,
)?;
let reference_edit = Edit::insertion(
format!(
"@{binding}{line_ending}{indent}",
line_ending = stylist.line_ending().as_str(),
),
stmt.range().start(),
);
Ok(Fix::unspecified_edits(import_edit, [reference_edit]))
}

/// B024
/// B027
pub fn abstract_base_class(
Expand Down Expand Up @@ -160,24 +124,12 @@ pub fn abstract_base_class(
&& is_empty_body(body)
&& !is_overload(&checker.ctx, decorator_list)
{
let mut diagnostic = Diagnostic::new(
checker.diagnostics.push(Diagnostic::new(
EmptyMethodWithoutAbstractDecorator {
name: format!("{name}.{method_name}"),
},
stmt.range(),
);
if checker.patch(Rule::EmptyMethodWithoutAbstractDecorator) {
diagnostic.try_set_fix(|| {
fix_abstractmethod_missing(
&checker.ctx,
&checker.importer,
checker.locator,
checker.stylist,
stmt,
)
});
}
checker.diagnostics.push(diagnostic);
));
}
}
if checker
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
---
B027.py:18:5: B027 [*] `AbstractClass.empty_1` is an empty method in an abstract base class, but has no abstract decorator
B027.py:18:5: B027 `AbstractClass.empty_1` is an empty method in an abstract base class, but has no abstract decorator
|
18 | class AbstractClass(ABC):
19 | def empty_1(self): # error
Expand All @@ -11,18 +11,8 @@ B027.py:18:5: B027 [*] `AbstractClass.empty_1` is an empty method in an abstract
21 |
22 | def empty_2(self): # error
|
= help: Add the `@abstractmethod` decorator

Suggested fix
15 15 |
16 16 |
17 17 | class AbstractClass(ABC):
18 |+ @notabstract
18 19 | def empty_1(self): # error
19 20 | ...
20 21 |

B027.py:21:5: B027 [*] `AbstractClass.empty_2` is an empty method in an abstract base class, but has no abstract decorator
B027.py:21:5: B027 `AbstractClass.empty_2` is an empty method in an abstract base class, but has no abstract decorator
|
21 | ...
22 |
Expand All @@ -33,18 +23,8 @@ B027.py:21:5: B027 [*] `AbstractClass.empty_2` is an empty method in an abstract
25 |
26 | def empty_3(self): # error
|
= help: Add the `@abstractmethod` decorator

Suggested fix
18 18 | def empty_1(self): # error
19 19 | ...
20 20 |
21 |+ @notabstract
21 22 | def empty_2(self): # error
22 23 | pass
23 24 |

B027.py:24:5: B027 [*] `AbstractClass.empty_3` is an empty method in an abstract base class, but has no abstract decorator
B027.py:24:5: B027 `AbstractClass.empty_3` is an empty method in an abstract base class, but has no abstract decorator
|
24 | pass
25 |
Expand All @@ -56,18 +36,8 @@ B027.py:24:5: B027 [*] `AbstractClass.empty_3` is an empty method in an abstract
29 |
30 | def empty_4(self): # error
|
= help: Add the `@abstractmethod` decorator

Suggested fix
21 21 | def empty_2(self): # error
22 22 | pass
23 23 |
24 |+ @notabstract
24 25 | def empty_3(self): # error
25 26 | """docstring"""
26 27 | ...

B027.py:28:5: B027 [*] `AbstractClass.empty_4` is an empty method in an abstract base class, but has no abstract decorator
B027.py:28:5: B027 `AbstractClass.empty_4` is an empty method in an abstract base class, but has no abstract decorator
|
28 | ...
29 |
Expand All @@ -82,15 +52,5 @@ B027.py:28:5: B027 [*] `AbstractClass.empty_4` is an empty method in an abstract
36 |
37 | @notabstract
|
= help: Add the `@abstractmethod` decorator

Suggested fix
25 25 | """docstring"""
26 26 | ...
27 27 |
28 |+ @notabstract
28 29 | def empty_4(self): # error
29 30 | """multiple ellipsis/pass"""
30 31 | ...


This file was deleted.

0 comments on commit 48e1852

Please sign in to comment.