Allow private accesses on super calls (#2815)

This commit is contained in:
Charlie Marsh 2023-02-12 11:11:25 -05:00 committed by GitHub
parent b8835c2e35
commit 9ddd5e4cfe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 8 deletions

View file

@ -38,13 +38,13 @@ class Foo(metaclass=BazMeta):
return self.bar
def public_func(self):
pass
super().public_func()
def _private_func(self):
pass
super()._private_func()
def __really_private_func(self, arg):
pass
super().__really_private_func(arg)
foo = Foo()

View file

@ -21,15 +21,23 @@ impl Violation for PrivateMemberAccess {
}
}
const VALID_IDS: [&str; 3] = ["self", "cls", "mcs"];
/// SLF001
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 call_path = collect_call_path(value);
if VALID_IDS.iter().any(|id| call_path.as_slice() == [*id]) {
return;
if let ExprKind::Call { func, .. } = &value.node {
let call_path = collect_call_path(func);
if call_path.as_slice() == ["super"] {
return;
}
} else {
let call_path = collect_call_path(value);
if call_path.as_slice() == ["self"]
|| call_path.as_slice() == ["cls"]
|| call_path.as_slice() == ["mcs"]
{
return;
}
}
checker.diagnostics.push(Diagnostic::new(