Skip to content

Commit

Permalink
Re-use is_magic where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jelly committed Aug 28, 2023
1 parent e615870 commit d7b44da
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ruff_python_ast::Stmt;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::identifier::Identifier;
use ruff_python_semantic::analyze::visibility;
use ruff_python_semantic::{Scope, ScopeKind};

use crate::settings::types::IdentifierPattern;
Expand Down Expand Up @@ -51,7 +52,7 @@ pub(crate) fn dunder_function_name(
if matches!(scope.kind, ScopeKind::Class(_)) {
return None;
}
if !(name.starts_with("__") && name.ends_with("__")) {
if !visibility::is_magic(name) {
return None;
}
// Allowed under PEP 562 (https://peps.python.org/pep-0562/).
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_python_semantic/src/analyze/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn is_test(name: &str) -> bool {

/// Returns `true` if a module name indicates public visibility.
fn is_public_module(module_name: &str) -> bool {
!module_name.starts_with('_') || (module_name.starts_with("__") && module_name.ends_with("__"))
!module_name.starts_with('_') || is_magic(module_name)
}

/// Returns `true` if a module name indicates private visibility.
Expand Down Expand Up @@ -201,7 +201,7 @@ pub(crate) fn method_visibility(function: &ast::StmtFunctionDef) -> Visibility {
}

// Is this a magic method?
if function.name.starts_with("__") && function.name.ends_with("__") {
if is_magic(&function.name) {
return Visibility::Public;
}

Expand Down

0 comments on commit d7b44da

Please sign in to comment.