mirror of https://github.com/astral-sh/ruff
Flag private member accesses on calls et al (#2753)
This commit is contained in:
parent
9f84c497f9
commit
dadbfea497
|
|
@ -33,6 +33,8 @@ class Foo(metaclass=BazMeta):
|
|||
def get_bar():
|
||||
if self.bar._private: # SLF001
|
||||
return None
|
||||
if self.bar()._private: # SLF001
|
||||
return None
|
||||
return self.bar
|
||||
|
||||
def public_func(self):
|
||||
|
|
@ -51,9 +53,11 @@ print(foo.public_thing)
|
|||
print(foo.public_func())
|
||||
print(foo.__dict__)
|
||||
print(foo.__str__())
|
||||
print(foo().__class__)
|
||||
|
||||
print(foo._private_thing) # SLF001
|
||||
print(foo.__really_private_thing) # SLF001
|
||||
print(foo._private_func()) # SLF001
|
||||
print(foo.__really_private_func(1)) # SLF001
|
||||
print(foo.bar._private) # SLF001
|
||||
print(foo()._private_thing) # SLF001
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
use ruff_macros::{define_violation, derive_message_formats};
|
||||
use rustpython_parser::ast::{Expr, ExprKind};
|
||||
|
||||
use ruff_macros::{define_violation, derive_message_formats};
|
||||
|
||||
use crate::ast::helpers::collect_call_path;
|
||||
use crate::ast::types::Range;
|
||||
use crate::checkers::ast::Checker;
|
||||
use crate::registry::Diagnostic;
|
||||
|
|
@ -25,20 +27,17 @@ 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 id = match &value.node {
|
||||
ExprKind::Name { id, .. } => id,
|
||||
ExprKind::Attribute { attr, .. } => attr,
|
||||
_ => return,
|
||||
};
|
||||
let call_path = collect_call_path(value);
|
||||
if VALID_IDS.iter().any(|id| call_path.as_slice() == [*id]) {
|
||||
return;
|
||||
}
|
||||
|
||||
if !VALID_IDS.contains(&id.as_str()) {
|
||||
checker.diagnostics.push(Diagnostic::new(
|
||||
PrivateMemberAccess {
|
||||
access: format!("{}.{}", id, attr),
|
||||
access: attr.to_string(),
|
||||
},
|
||||
Range::from_located(expr),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
---
|
||||
source: src/rules/flake8_self/mod.rs
|
||||
source: crates/ruff/src/rules/flake8_self/mod.rs
|
||||
expression: diagnostics
|
||||
---
|
||||
- kind:
|
||||
PrivateMemberAccess:
|
||||
access: bar._private
|
||||
access: _private
|
||||
location:
|
||||
row: 34
|
||||
column: 11
|
||||
|
|
@ -15,57 +15,79 @@ expression: diagnostics
|
|||
parent: ~
|
||||
- kind:
|
||||
PrivateMemberAccess:
|
||||
access: foo._private_thing
|
||||
access: _private
|
||||
location:
|
||||
row: 55
|
||||
row: 36
|
||||
column: 11
|
||||
end_location:
|
||||
row: 36
|
||||
column: 30
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
PrivateMemberAccess:
|
||||
access: _private_thing
|
||||
location:
|
||||
row: 58
|
||||
column: 6
|
||||
end_location:
|
||||
row: 55
|
||||
row: 58
|
||||
column: 24
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
PrivateMemberAccess:
|
||||
access: foo.__really_private_thing
|
||||
access: __really_private_thing
|
||||
location:
|
||||
row: 56
|
||||
row: 59
|
||||
column: 6
|
||||
end_location:
|
||||
row: 56
|
||||
row: 59
|
||||
column: 32
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
PrivateMemberAccess:
|
||||
access: foo._private_func
|
||||
access: _private_func
|
||||
location:
|
||||
row: 57
|
||||
row: 60
|
||||
column: 6
|
||||
end_location:
|
||||
row: 57
|
||||
row: 60
|
||||
column: 23
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
PrivateMemberAccess:
|
||||
access: foo.__really_private_func
|
||||
access: __really_private_func
|
||||
location:
|
||||
row: 58
|
||||
row: 61
|
||||
column: 6
|
||||
end_location:
|
||||
row: 58
|
||||
row: 61
|
||||
column: 31
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
PrivateMemberAccess:
|
||||
access: bar._private
|
||||
access: _private
|
||||
location:
|
||||
row: 59
|
||||
row: 62
|
||||
column: 6
|
||||
end_location:
|
||||
row: 59
|
||||
row: 62
|
||||
column: 22
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
PrivateMemberAccess:
|
||||
access: _private_thing
|
||||
location:
|
||||
row: 63
|
||||
column: 6
|
||||
end_location:
|
||||
row: 63
|
||||
column: 26
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue