Skip to content

Commit

Permalink
Use super visibility in helpers (#4995)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Jun 10, 2023
1 parent 1d756dc commit c1ac500
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 38 deletions.
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/flake8_bandit/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ static PASSWORD_CANDIDATE_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"(^|_)(?i)(pas+wo?r?d|pass(phrase)?|pwd|token|secrete?)($|_)").unwrap()
});

pub(crate) fn string_literal(expr: &Expr) -> Option<&str> {
pub(super) fn string_literal(expr: &Expr) -> Option<&str> {
match expr {
Expr::Constant(ast::ExprConstant {
value: Constant::Str(string),
Expand All @@ -18,11 +18,11 @@ pub(crate) fn string_literal(expr: &Expr) -> Option<&str> {
}
}

pub(crate) fn matches_password_name(string: &str) -> bool {
pub(super) fn matches_password_name(string: &str) -> bool {
PASSWORD_CANDIDATE_REGEX.is_match(string)
}

pub(crate) fn is_untyped_exception(type_: Option<&Expr>, model: &SemanticModel) -> bool {
pub(super) fn is_untyped_exception(type_: Option<&Expr>, model: &SemanticModel) -> bool {
type_.map_or(true, |type_| {
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = &type_ {
elts.iter().any(|type_| {
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/flake8_comprehensions/rules/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use rustpython_parser::ast::{self, Expr, Keyword};

pub(crate) fn expr_name(func: &Expr) -> Option<&str> {
pub(super) fn expr_name(func: &Expr) -> Option<&str> {
if let Expr::Name(ast::ExprName { id, .. }) = func {
Some(id)
} else {
None
}
}

pub(crate) fn exactly_one_argument_with_matching_function<'a>(
pub(super) fn exactly_one_argument_with_matching_function<'a>(
name: &str,
func: &Expr,
args: &'a [Expr],
Expand All @@ -26,7 +26,7 @@ pub(crate) fn exactly_one_argument_with_matching_function<'a>(
Some(&args[0])
}

pub(crate) fn first_argument_with_matching_function<'a>(
pub(super) fn first_argument_with_matching_function<'a>(
name: &str,
func: &Expr,
args: &'a [Expr],
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/rules/flake8_django/rules/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ use rustpython_parser::ast::Expr;
use ruff_python_semantic::model::SemanticModel;

/// Return `true` if a Python class appears to be a Django model, based on its base classes.
pub(crate) fn is_model(model: &SemanticModel, base: &Expr) -> bool {
pub(super) fn is_model(model: &SemanticModel, base: &Expr) -> bool {
model.resolve_call_path(base).map_or(false, |call_path| {
call_path.as_slice() == ["django", "db", "models", "Model"]
})
}

/// Return `true` if a Python class appears to be a Django model form, based on its base classes.
pub(crate) fn is_model_form(model: &SemanticModel, base: &Expr) -> bool {
pub(super) fn is_model_form(model: &SemanticModel, base: &Expr) -> bool {
model.resolve_call_path(base).map_or(false, |call_path| {
call_path.as_slice() == ["django", "forms", "ModelForm"]
|| call_path.as_slice() == ["django", "forms", "models", "ModelForm"]
})
}

/// Return `true` if the expression is constructor for a Django model field.
pub(crate) fn is_model_field(model: &SemanticModel, expr: &Expr) -> bool {
pub(super) fn is_model_field(model: &SemanticModel, expr: &Expr) -> bool {
model.resolve_call_path(expr).map_or(false, |call_path| {
call_path
.as_slice()
Expand All @@ -27,7 +27,7 @@ pub(crate) fn is_model_field(model: &SemanticModel, expr: &Expr) -> bool {
}

/// Return the name of the field type, if the expression is constructor for a Django model field.
pub(crate) fn get_model_field_name<'a>(
pub(super) fn get_model_field_name<'a>(
model: &'a SemanticModel,
expr: &'a Expr,
) -> Option<&'a str> {
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/flake8_return/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ruff_python_whitespace::UniversalNewlines;

/// Return `true` if a function's return statement include at least one
/// non-`None` value.
pub(crate) fn result_exists(returns: &[(&Stmt, Option<&Expr>)]) -> bool {
pub(super) fn result_exists(returns: &[(&Stmt, Option<&Expr>)]) -> bool {
returns.iter().any(|(_, expr)| {
expr.map(|expr| {
!matches!(
Expand All @@ -25,7 +25,7 @@ pub(crate) fn result_exists(returns: &[(&Stmt, Option<&Expr>)]) -> bool {
///
/// This method assumes that the statement is the last statement in its body; specifically, that
/// the statement isn't followed by a semicolon, followed by a multi-line statement.
pub(crate) fn end_of_last_statement(stmt: &Stmt, locator: &Locator) -> TextSize {
pub(super) fn end_of_last_statement(stmt: &Stmt, locator: &Locator) -> TextSize {
// End-of-file, so just return the end of the statement.
if stmt.end() == locator.text_len() {
stmt.end()
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/flynt/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn to_formatted_value_expr(inner: &Expr) -> Expr {
}

/// Convert a string to a constant string expression.
pub(crate) fn to_constant_string(s: &str) -> Expr {
pub(super) fn to_constant_string(s: &str) -> Expr {
let node = ast::ExprConstant {
value: Constant::Str(s.to_owned()),
kind: None,
Expand Down Expand Up @@ -47,7 +47,7 @@ fn is_simple_callee(func: &Expr) -> bool {
}

/// Convert an expression to a f-string element (if it looks like a good idea).
pub(crate) fn to_fstring_elem(expr: &Expr) -> Option<Expr> {
pub(super) fn to_fstring_elem(expr: &Expr) -> Option<Expr> {
match expr {
// These are directly handled by `unparse_fstring_elem`:
Expr::Constant(ast::ExprConstant {
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/isort/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::rules::isort::types::TrailingComma;

/// Return `true` if a `Stmt::ImportFrom` statement ends with a magic
/// trailing comma.
pub(crate) fn trailing_comma(stmt: &Stmt, locator: &Locator) -> TrailingComma {
pub(super) fn trailing_comma(stmt: &Stmt, locator: &Locator) -> TrailingComma {
let contents = locator.slice(stmt.range());
let mut count = 0u32;
let mut trailing_comma = TrailingComma::Absent;
Expand Down Expand Up @@ -36,7 +36,7 @@ pub(crate) fn trailing_comma(stmt: &Stmt, locator: &Locator) -> TrailingComma {
}

/// Return `true` if a [`Stmt`] is preceded by a "comment break"
pub(crate) fn has_comment_break(stmt: &Stmt, locator: &Locator) -> bool {
pub(super) fn has_comment_break(stmt: &Stmt, locator: &Locator) -> bool {
// Starting from the `Stmt` (`def f(): pass`), we want to detect patterns like
// this:
//
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/pandas_vet/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustpython_parser::ast::Expr;
use ruff_python_semantic::binding::{BindingKind, Importation};
use ruff_python_semantic::model::SemanticModel;

pub(crate) enum Resolution {
pub(super) enum Resolution {
/// The expression resolves to an irrelevant expression type (e.g., a constant).
IrrelevantExpression,
/// The expression resolves to an irrelevant binding (e.g., a function definition).
Expand All @@ -16,7 +16,7 @@ pub(crate) enum Resolution {
}

/// Test an [`Expr`] for relevance to Pandas-related operations.
pub(crate) fn test_expression(expr: &Expr, model: &SemanticModel) -> Resolution {
pub(super) fn test_expression(expr: &Expr, model: &SemanticModel) -> Resolution {
match expr {
Expr::Constant(_)
| Expr::Tuple(_)
Expand Down
14 changes: 7 additions & 7 deletions crates/ruff/src/rules/pep8_naming/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use rustpython_parser::ast::{self, Expr, Stmt};
use ruff_python_semantic::model::SemanticModel;
use ruff_python_stdlib::str::{is_lower, is_upper};

pub(crate) fn is_camelcase(name: &str) -> bool {
pub(super) fn is_camelcase(name: &str) -> bool {
!is_lower(name) && !is_upper(name) && !name.contains('_')
}

pub(crate) fn is_mixed_case(name: &str) -> bool {
pub(super) fn is_mixed_case(name: &str) -> bool {
!is_lower(name)
&& name
.strip_prefix('_')
Expand All @@ -18,11 +18,11 @@ pub(crate) fn is_mixed_case(name: &str) -> bool {
.map_or_else(|| false, char::is_lowercase)
}

pub(crate) fn is_acronym(name: &str, asname: &str) -> bool {
pub(super) fn is_acronym(name: &str, asname: &str) -> bool {
name.chars().filter(|c| c.is_uppercase()).join("") == asname
}

pub(crate) fn is_named_tuple_assignment(model: &SemanticModel, stmt: &Stmt) -> bool {
pub(super) fn is_named_tuple_assignment(model: &SemanticModel, stmt: &Stmt) -> bool {
let Stmt::Assign(ast::StmtAssign { value, .. }) = stmt else {
return false;
};
Expand All @@ -37,7 +37,7 @@ pub(crate) fn is_named_tuple_assignment(model: &SemanticModel, stmt: &Stmt) -> b
})
}

pub(crate) fn is_typed_dict_assignment(model: &SemanticModel, stmt: &Stmt) -> bool {
pub(super) fn is_typed_dict_assignment(model: &SemanticModel, stmt: &Stmt) -> bool {
let Stmt::Assign(ast::StmtAssign { value, .. }) = stmt else {
return false;
};
Expand All @@ -49,7 +49,7 @@ pub(crate) fn is_typed_dict_assignment(model: &SemanticModel, stmt: &Stmt) -> bo
})
}

pub(crate) fn is_type_var_assignment(model: &SemanticModel, stmt: &Stmt) -> bool {
pub(super) fn is_type_var_assignment(model: &SemanticModel, stmt: &Stmt) -> bool {
let Stmt::Assign(ast::StmtAssign { value, .. }) = stmt else {
return false;
};
Expand All @@ -62,7 +62,7 @@ pub(crate) fn is_type_var_assignment(model: &SemanticModel, stmt: &Stmt) -> bool
})
}

pub(crate) fn is_typed_dict_class(model: &SemanticModel, bases: &[Expr]) -> bool {
pub(super) fn is_typed_dict_class(model: &SemanticModel, bases: &[Expr]) -> bool {
bases
.iter()
.any(|base| model.match_typing_expr(base, "TypedDict"))
Expand Down
14 changes: 7 additions & 7 deletions crates/ruff/src/rules/pydocstyle/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ruff_python_semantic::model::SemanticModel;
use ruff_python_whitespace::UniversalNewlines;

/// Return the index of the first logical line in a string.
pub(crate) fn logical_line(content: &str) -> Option<usize> {
pub(super) fn logical_line(content: &str) -> Option<usize> {
// Find the first logical line.
let mut logical_line = None;
for (i, line) in content.universal_newlines().enumerate() {
Expand All @@ -28,12 +28,17 @@ pub(crate) fn logical_line(content: &str) -> Option<usize> {

/// Normalize a word by removing all non-alphanumeric characters
/// and converting it to lowercase.
pub(crate) fn normalize_word(first_word: &str) -> String {
pub(super) fn normalize_word(first_word: &str) -> String {
first_word
.replace(|c: char| !c.is_alphanumeric(), "")
.to_lowercase()
}

/// Return true if a line ends with an odd number of backslashes (i.e., ends with an escape).
pub(super) fn ends_with_backslash(line: &str) -> bool {
line.chars().rev().take_while(|c| *c == '\\').count() % 2 == 1
}

/// Check decorator list to see if function should be ignored.
pub(crate) fn should_ignore_definition(
model: &SemanticModel,
Expand Down Expand Up @@ -64,11 +69,6 @@ pub(crate) fn should_ignore_definition(
false
}

/// Return true if a line ends with an odd number of backslashes (i.e., ends with an escape).
pub(crate) fn ends_with_backslash(line: &str) -> bool {
line.chars().rev().take_while(|c| *c == '\\').count() % 2 == 1
}

/// Check if a docstring should be ignored.
pub(crate) fn should_ignore_docstring(contents: &str) -> bool {
// Avoid analyzing docstrings that contain implicit string concatenations.
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/pylint/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::fmt;

use crate::settings::Settings;

pub(crate) fn in_dunder_init(model: &SemanticModel, settings: &Settings) -> bool {
pub(super) fn in_dunder_init(model: &SemanticModel, settings: &Settings) -> bool {
let scope = model.scope();
let (
ScopeKind::Function(ast::StmtFunctionDef {
Expand Down Expand Up @@ -49,7 +49,7 @@ pub(crate) fn in_dunder_init(model: &SemanticModel, settings: &Settings) -> bool

/// A wrapper around [`Cmpop`] that implements `Display`.
#[derive(Debug)]
pub(crate) struct CmpopExt(Cmpop);
pub(super) struct CmpopExt(Cmpop);

impl From<&Cmpop> for CmpopExt {
fn from(cmpop: &Cmpop) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pyupgrade/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use regex::{Captures, Regex};

static CURLY_BRACES: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\\N\{[^}]+})|([{}])").unwrap());

pub(crate) fn curly_escape(text: &str) -> String {
pub(super) fn curly_escape(text: &str) -> String {
// Match all curly braces. This will include named unicode escapes (like
// \N{SNOWMAN}), which we _don't_ want to escape, so take care to preserve them.
CURLY_BRACES
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/tryceratops/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use ruff_python_semantic::analyze::logging;
use ruff_python_semantic::model::SemanticModel;

/// Collect `logging`-like calls from an AST.
pub(crate) struct LoggerCandidateVisitor<'a, 'b> {
pub(super) struct LoggerCandidateVisitor<'a, 'b> {
semantic_model: &'a SemanticModel<'b>,
pub(crate) calls: Vec<&'b ast::ExprCall>,
pub(super) calls: Vec<&'b ast::ExprCall>,
}

impl<'a, 'b> LoggerCandidateVisitor<'a, 'b> {
pub(crate) fn new(semantic_model: &'a SemanticModel<'b>) -> Self {
pub(super) fn new(semantic_model: &'a SemanticModel<'b>) -> Self {
LoggerCandidateVisitor {
semantic_model,
calls: Vec::new(),
Expand Down

0 comments on commit c1ac500

Please sign in to comment.