diff --git a/crates/ruff/resources/test/fixtures/flake8_self/SLF001.py b/crates/ruff/resources/test/fixtures/flake8_self/SLF001.py index 61b4377d3f..d395016904 100644 --- a/crates/ruff/resources/test/fixtures/flake8_self/SLF001.py +++ b/crates/ruff/resources/test/fixtures/flake8_self/SLF001.py @@ -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 diff --git a/crates/ruff/src/rules/flake8_self/rules/private_member_access.rs b/crates/ruff/src/rules/flake8_self/rules/private_member_access.rs index f9872eb826..cdc173560e 100644 --- a/crates/ruff/src/rules/flake8_self/rules/private_member_access.rs +++ b/crates/ruff/src/rules/flake8_self/rules/private_member_access.rs @@ -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, - }; - - if !VALID_IDS.contains(&id.as_str()) { - checker.diagnostics.push(Diagnostic::new( - PrivateMemberAccess { - access: format!("{}.{}", id, attr), - }, - Range::from_located(expr), - )); + let call_path = collect_call_path(value); + if VALID_IDS.iter().any(|id| call_path.as_slice() == [*id]) { + return; } + + checker.diagnostics.push(Diagnostic::new( + PrivateMemberAccess { + access: attr.to_string(), + }, + Range::from_located(expr), + )); } } } diff --git a/crates/ruff/src/rules/flake8_self/snapshots/ruff__rules__flake8_self__tests__private-member-access_SLF001.py.snap b/crates/ruff/src/rules/flake8_self/snapshots/ruff__rules__flake8_self__tests__private-member-access_SLF001.py.snap index 7874f7cfb5..c184d69741 100644 --- a/crates/ruff/src/rules/flake8_self/snapshots/ruff__rules__flake8_self__tests__private-member-access_SLF001.py.snap +++ b/crates/ruff/src/rules/flake8_self/snapshots/ruff__rules__flake8_self__tests__private-member-access_SLF001.py.snap @@ -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: ~