Skip to content

Commit

Permalink
[flake8-self] Fix False Negative Issue on Rule SLF001 (#2527)
Browse files Browse the repository at this point in the history
  • Loading branch information
saadmk11 committed Feb 3, 2023
1 parent 0f8f250 commit c96ba6d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
30 changes: 29 additions & 1 deletion resources/test/fixtures/flake8_self/SLF001.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
class Foo:
class BazMeta(type):
_private_count = 1

def __new__(mcs, name, bases, attrs):
if mcs._private_count <= 5:
mcs.some_method()

return super().__new__(mcs, name, bases, attrs)

def some_method():
pass


class Bar:
_private = True

@classmethod
def is_private(cls):
return cls._private


class Foo(metaclass=BazMeta):

def __init__(self):
self.public_thing = "foo"
self._private_thing = "bar"
self.__really_private_thing = "baz"
self.bar = Bar()

def __str__(self):
return "foo"

def get_bar():
if self.bar._private: # SLF001
return None
return self.bar

def public_func(self):
pass

Expand All @@ -29,3 +56,4 @@ def __really_private_func(self, arg):
print(foo.__really_private_thing) # SLF001
print(foo._private_func()) # SLF001
print(foo.__really_private_func(1)) # SLF001
print(foo.bar._private) # SLF001
6 changes: 4 additions & 2 deletions src/rules/flake8_self/rules/private_member_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ const VALID_IDS: [&str; 3] = ["self", "cls", "mcs"];
pub fn private_member_access(checker: &mut Checker, expr: &Expr) {
if let ExprKind::Attribute { value, attr, .. } = &expr.node {
if !attr.ends_with("__") && (attr.starts_with('_') || attr.starts_with("__")) {
let ExprKind::Name { id, .. } = &value.node else {
return;
let id = match &value.node {
ExprKind::Name { id, .. } => id,
ExprKind::Attribute { attr, .. } => attr,
_ => return,
};

if !VALID_IDS.contains(&id.as_str()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,70 @@
source: src/rules/flake8_self/mod.rs
expression: diagnostics
---
- kind:
PrivateMemberAccess:
access: bar._private
location:
row: 34
column: 11
end_location:
row: 34
column: 28
fix: ~
parent: ~
- kind:
PrivateMemberAccess:
access: foo._private_thing
location:
row: 28
row: 55
column: 6
end_location:
row: 28
row: 55
column: 24
fix: ~
parent: ~
- kind:
PrivateMemberAccess:
access: foo.__really_private_thing
location:
row: 29
row: 56
column: 6
end_location:
row: 29
row: 56
column: 32
fix: ~
parent: ~
- kind:
PrivateMemberAccess:
access: foo._private_func
location:
row: 30
row: 57
column: 6
end_location:
row: 30
row: 57
column: 23
fix: ~
parent: ~
- kind:
PrivateMemberAccess:
access: foo.__really_private_func
location:
row: 31
row: 58
column: 6
end_location:
row: 31
row: 58
column: 31
fix: ~
parent: ~
- kind:
PrivateMemberAccess:
access: bar._private
location:
row: 59
column: 6
end_location:
row: 59
column: 22
fix: ~
parent: ~

0 comments on commit c96ba6d

Please sign in to comment.