[`flake8-bugbear`] Fix False Positives for `B024` & `B027` (#1851)

closes https://github.com/charliermarsh/ruff/issues/1848
This commit is contained in:
Maksudul Haque 2023-01-13 22:46:17 +06:00 committed by GitHub
parent 048e5774e8
commit 3ae01db226
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 30 deletions

View File

@ -1,15 +1,16 @@
""" """
Should emit: Should emit:
B024 - on lines 17, 34, 52, 58, 69, 74, 79, 84, 89 B024 - on lines 18, 71, 82, 87, 92, 141
""" """
import abc import abc
import abc as notabc import abc as notabc
from abc import ABC, ABCMeta from abc import ABC, ABCMeta
from abc import abstractmethod from abc import abstractmethod, abstractproperty
from abc import abstractmethod as abstract from abc import abstractmethod as abstract
from abc import abstractmethod as abstractaoeuaoeuaoeu from abc import abstractmethod as abstractaoeuaoeuaoeu
from abc import abstractmethod as notabstract from abc import abstractmethod as notabstract
from abc import abstractproperty as notabstract_property
import foo import foo
@ -49,12 +50,24 @@ class Base_6(ABC):
foo() foo()
class Base_7(ABC): # error class Base_7(ABC):
@notabstract @notabstract
def method(self): def method(self):
foo() foo()
class Base_8(ABC):
@notabstract_property
def method(self):
foo()
class Base_9(ABC):
@abstractproperty
def method(self):
foo()
class MetaBase_1(metaclass=ABCMeta): # error class MetaBase_1(metaclass=ABCMeta): # error
def method(self): def method(self):
foo() foo()

View File

@ -1,11 +1,12 @@
""" """
Should emit: Should emit:
B027 - on lines 12, 15, 18, 22, 30 B027 - on lines 13, 16, 19, 23
""" """
import abc import abc
from abc import ABC from abc import ABC
from abc import abstractmethod from abc import abstractmethod, abstractproperty
from abc import abstractmethod as notabstract from abc import abstractmethod as notabstract
from abc import abstractproperty as notabstract_property
class AbstractClass(ABC): class AbstractClass(ABC):
@ -42,6 +43,18 @@ class AbstractClass(ABC):
def abstract_3(self): def abstract_3(self):
... ...
@abc.abstractproperty
def abstract_4(self):
...
@abstractproperty
def abstract_5(self):
...
@notabstract_property
def abstract_6(self):
...
def body_1(self): def body_1(self):
print("foo") print("foo")
... ...

View File

@ -52,6 +52,20 @@ fn is_abstractmethod(
match_module_member(expr, "abc", "abstractmethod", from_imports, import_aliases) match_module_member(expr, "abc", "abstractmethod", from_imports, import_aliases)
} }
fn is_abstractproperty(
expr: &Expr,
from_imports: &FxHashMap<&str, FxHashSet<&str>>,
import_aliases: &FxHashMap<&str, &str>,
) -> bool {
match_module_member(
expr,
"abc",
"abstractproperty",
from_imports,
import_aliases,
)
}
fn is_overload( fn is_overload(
expr: &Expr, expr: &Expr,
from_imports: &FxHashMap<&str, FxHashSet<&str>>, from_imports: &FxHashMap<&str, FxHashSet<&str>>,
@ -102,9 +116,10 @@ pub fn abstract_base_class(
continue; continue;
}; };
let has_abstract_decorator = decorator_list let has_abstract_decorator = decorator_list.iter().any(|d| {
.iter() is_abstractmethod(d, &checker.from_imports, &checker.import_aliases)
.any(|d| is_abstractmethod(d, &checker.from_imports, &checker.import_aliases)); || is_abstractproperty(d, &checker.from_imports, &checker.import_aliases)
});
has_abstract_method |= has_abstract_decorator; has_abstract_method |= has_abstract_decorator;

View File

@ -1,64 +1,64 @@
--- ---
source: src/flake8_bugbear/mod.rs source: src/flake8_bugbear/mod.rs
expression: checks expression: diagnostics
--- ---
- kind: - kind:
AbstractBaseClassWithoutAbstractMethod: Base_1 AbstractBaseClassWithoutAbstractMethod: Base_1
location: location:
row: 17 row: 18
column: 0 column: 0
end_location: end_location:
row: 19 row: 20
column: 13 column: 13
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
AbstractBaseClassWithoutAbstractMethod: MetaBase_1 AbstractBaseClassWithoutAbstractMethod: MetaBase_1
location: location:
row: 58 row: 71
column: 0 column: 0
end_location: end_location:
row: 60 row: 73
column: 13 column: 13
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
AbstractBaseClassWithoutAbstractMethod: abc_Base_1 AbstractBaseClassWithoutAbstractMethod: abc_Base_1
location: location:
row: 69 row: 82
column: 0 column: 0
end_location: end_location:
row: 71 row: 84
column: 13 column: 13
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
AbstractBaseClassWithoutAbstractMethod: abc_Base_2 AbstractBaseClassWithoutAbstractMethod: abc_Base_2
location: location:
row: 74 row: 87
column: 0 column: 0
end_location: end_location:
row: 76 row: 89
column: 13 column: 13
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
AbstractBaseClassWithoutAbstractMethod: notabc_Base_1 AbstractBaseClassWithoutAbstractMethod: notabc_Base_1
location: location:
row: 79 row: 92
column: 0 column: 0
end_location: end_location:
row: 81 row: 94
column: 13 column: 13
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
AbstractBaseClassWithoutAbstractMethod: abc_set_class_variable_4 AbstractBaseClassWithoutAbstractMethod: abc_set_class_variable_4
location: location:
row: 128 row: 141
column: 0 column: 0
end_location: end_location:
row: 129 row: 142
column: 7 column: 7
fix: ~ fix: ~
parent: ~ parent: ~

View File

@ -1,44 +1,44 @@
--- ---
source: src/flake8_bugbear/mod.rs source: src/flake8_bugbear/mod.rs
expression: checks expression: diagnostics
--- ---
- kind: - kind:
EmptyMethodWithoutAbstractDecorator: AbstractClass EmptyMethodWithoutAbstractDecorator: AbstractClass
location: location:
row: 12 row: 13
column: 4 column: 4
end_location: end_location:
row: 13 row: 14
column: 11 column: 11
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
EmptyMethodWithoutAbstractDecorator: AbstractClass EmptyMethodWithoutAbstractDecorator: AbstractClass
location: location:
row: 15 row: 16
column: 4 column: 4
end_location: end_location:
row: 16 row: 17
column: 12 column: 12
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
EmptyMethodWithoutAbstractDecorator: AbstractClass EmptyMethodWithoutAbstractDecorator: AbstractClass
location: location:
row: 18 row: 19
column: 4 column: 4
end_location: end_location:
row: 20 row: 21
column: 11 column: 11
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
EmptyMethodWithoutAbstractDecorator: AbstractClass EmptyMethodWithoutAbstractDecorator: AbstractClass
location: location:
row: 22 row: 23
column: 4 column: 4
end_location: end_location:
row: 27 row: 28
column: 12 column: 12
fix: ~ fix: ~
parent: ~ parent: ~