Skip to content

Commit

Permalink
Add our own ignored-names abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 2, 2024
1 parent 25d9305 commit e585942
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 141 deletions.
5 changes: 3 additions & 2 deletions crates/ruff_linter/src/rules/pep8_naming/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod tests {

use crate::registry::Rule;
use crate::rules::pep8_naming;
use crate::rules::pep8_naming::settings::IgnoreNames;
use crate::settings::types::IdentifierPattern;
use crate::test::test_path;
use crate::{assert_messages, settings};
Expand Down Expand Up @@ -148,12 +149,12 @@ mod tests {
PathBuf::from_iter(["pep8_naming", "ignore_names", path]).as_path(),
&settings::LinterSettings {
pep8_naming: pep8_naming::settings::Settings {
ignore_names: vec![
ignore_names: IgnoreNames::UserProvided(vec![
IdentifierPattern::new("*allowed*").unwrap(),
IdentifierPattern::new("*Allowed*").unwrap(),
IdentifierPattern::new("*ALLOWED*").unwrap(),
IdentifierPattern::new("BA").unwrap(), // For N817.
],
]),
..Default::default()
},
..settings::LinterSettings::for_rule(rule_code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ruff_python_stdlib::str::{self};
use ruff_text_size::Ranged;

use crate::rules::pep8_naming::helpers;
use crate::settings::types::IdentifierPattern;
use crate::rules::pep8_naming::settings::IgnoreNames;

/// ## What it does
/// Checks for `CamelCase` imports that are aliased as acronyms.
Expand Down Expand Up @@ -54,12 +54,9 @@ pub(crate) fn camelcase_imported_as_acronym(
asname: &str,
alias: &Alias,
stmt: &Stmt,
ignore_names: &[IdentifierPattern],
ignore_names: &IgnoreNames,
) -> Option<Diagnostic> {
if ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(asname))
{
if ignore_names.matches(asname) {
return None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ruff_python_stdlib::str::{self};
use ruff_text_size::Ranged;

use crate::rules::pep8_naming::helpers;
use crate::settings::types::IdentifierPattern;
use crate::rules::pep8_naming::settings::IgnoreNames;

/// ## What it does
/// Checks for `CamelCase` imports that are aliased to constant-style names.
Expand Down Expand Up @@ -51,12 +51,9 @@ pub(crate) fn camelcase_imported_as_constant(
asname: &str,
alias: &Alias,
stmt: &Stmt,
ignore_names: &[IdentifierPattern],
ignore_names: &IgnoreNames,
) -> Option<Diagnostic> {
if ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(name))
{
if ignore_names.matches(asname) {
return None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ruff_macros::{derive_message_formats, violation};
use ruff_text_size::Ranged;

use crate::rules::pep8_naming::helpers;
use crate::settings::types::IdentifierPattern;
use crate::rules::pep8_naming::settings::IgnoreNames;

/// ## What it does
/// Checks for `CamelCase` imports that are aliased to lowercase names.
Expand Down Expand Up @@ -50,12 +50,9 @@ pub(crate) fn camelcase_imported_as_lowercase(
asname: &str,
alias: &Alias,
stmt: &Stmt,
ignore_names: &[IdentifierPattern],
ignore_names: &IgnoreNames,
) -> Option<Diagnostic> {
if ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(asname))
{
if ignore_names.matches(asname) {
return None;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use ruff_python_ast::{Alias, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{Alias, Stmt};
use ruff_python_stdlib::str;
use ruff_text_size::Ranged;

use crate::settings::types::IdentifierPattern;
use crate::rules::pep8_naming::settings::IgnoreNames;

/// ## What it does
/// Checks for constant imports that are aliased to non-constant-style
Expand Down Expand Up @@ -51,12 +50,9 @@ pub(crate) fn constant_imported_as_non_constant(
asname: &str,
alias: &Alias,
stmt: &Stmt,
ignore_names: &[IdentifierPattern],
ignore_names: &IgnoreNames,
) -> Option<Diagnostic> {
if ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(name))
{
if ignore_names.matches(asname) {
return None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ruff_python_ast::identifier::Identifier;
use ruff_python_semantic::analyze::visibility;
use ruff_python_semantic::{Scope, ScopeKind};

use crate::settings::types::IdentifierPattern;
use crate::rules::pep8_naming::settings::IgnoreNames;

/// ## What it does
/// Checks for functions with "dunder" names (that is, names with two
Expand Down Expand Up @@ -47,7 +47,7 @@ pub(crate) fn dunder_function_name(
scope: &Scope,
stmt: &Stmt,
name: &str,
ignore_names: &[IdentifierPattern],
ignore_names: &IgnoreNames,
) -> Option<Diagnostic> {
if matches!(scope.kind, ScopeKind::Class(_)) {
return None;
Expand All @@ -59,10 +59,7 @@ pub(crate) fn dunder_function_name(
if matches!(scope.kind, ScopeKind::Module) && (name == "__getattr__" || name == "__dir__") {
return None;
}
if ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(name))
{
if ignore_names.matches(name) {
return None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::identifier::Identifier;

use crate::settings::types::IdentifierPattern;
use crate::rules::pep8_naming::settings::IgnoreNames;

/// ## What it does
/// Checks for custom exception definitions that omit the `Error` suffix.
Expand Down Expand Up @@ -47,7 +47,7 @@ pub(crate) fn error_suffix_on_exception_name(
class_def: &Stmt,
arguments: Option<&Arguments>,
name: &str,
ignore_names: &[IdentifierPattern],
ignore_names: &IgnoreNames,
) -> Option<Diagnostic> {
if name.ends_with("Error") {
return None;
Expand All @@ -65,10 +65,7 @@ pub(crate) fn error_suffix_on_exception_name(
return None;
}

if ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(name))
{
if ignore_names.matches(name) {
return None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ruff_macros::{derive_message_formats, violation};
use ruff_python_stdlib::str;
use ruff_text_size::Ranged;

use crate::settings::types::IdentifierPattern;
use crate::rules::pep8_naming::settings::IgnoreNames;

/// ## What it does
/// Checks for argument names that do not follow the `snake_case` convention.
Expand Down Expand Up @@ -52,12 +52,9 @@ impl Violation for InvalidArgumentName {
pub(crate) fn invalid_argument_name(
name: &str,
parameter: &Parameter,
ignore_names: &[IdentifierPattern],
ignore_names: &IgnoreNames,
) -> Option<Diagnostic> {
if ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(name))
{
if ignore_names.matches(name) {
return None;
}
if !str::is_lowercase(name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::identifier::Identifier;

use crate::settings::types::IdentifierPattern;
use crate::rules::pep8_naming::settings::IgnoreNames;

/// ## What it does
/// Checks for class names that do not follow the `CamelCase` convention.
Expand Down Expand Up @@ -52,12 +52,9 @@ impl Violation for InvalidClassName {
pub(crate) fn invalid_class_name(
class_def: &Stmt,
name: &str,
ignore_names: &[IdentifierPattern],
ignore_names: &IgnoreNames,
) -> Option<Diagnostic> {
if ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(name))
{
if ignore_names.matches(name) {
return None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,7 @@ pub(crate) fn invalid_first_argument_name_for_class_method(
.or_else(|| parameters.args.first())
{
if &parameter.name != "cls" {
if checker
.settings
.pep8_naming
.ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(name))
{
if checker.settings.pep8_naming.ignore_names.matches(name) {
return None;
}
return Some(Diagnostic::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,7 @@ pub(crate) fn invalid_first_argument_name_for_method(
if &arg.parameter.name == "self" {
return None;
}
if checker
.settings
.pep8_naming
.ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(name))
{
if checker.settings.pep8_naming.ignore_names.matches(name) {
return None;
}
Some(Diagnostic::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ruff_python_semantic::analyze::visibility;
use ruff_python_semantic::SemanticModel;
use ruff_python_stdlib::str;

use crate::settings::types::IdentifierPattern;
use crate::rules::pep8_naming::settings::IgnoreNames;

/// ## What it does
/// Checks for functions names that do not follow the `snake_case` naming
Expand Down Expand Up @@ -60,14 +60,11 @@ pub(crate) fn invalid_function_name(
stmt: &Stmt,
name: &str,
decorator_list: &[Decorator],
ignore_names: &[IdentifierPattern],
ignore_names: &IgnoreNames,
semantic: &SemanticModel,
) -> Option<Diagnostic> {
// Ignore any explicitly-ignored function names.
if ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(name))
{
if ignore_names.matches(name) {
return None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ruff_macros::{derive_message_formats, violation};
use ruff_python_stdlib::identifiers::{is_migration_name, is_module_name};
use ruff_text_size::TextRange;

use crate::settings::types::IdentifierPattern;
use crate::rules::pep8_naming::settings::IgnoreNames;

/// ## What it does
/// Checks for module names that do not follow the `snake_case` naming
Expand Down Expand Up @@ -50,7 +50,7 @@ impl Violation for InvalidModuleName {
pub(crate) fn invalid_module_name(
path: &Path,
package: Option<&Path>,
ignore_names: &[IdentifierPattern],
ignore_names: &IgnoreNames,
) -> Option<Diagnostic> {
if !path
.extension()
Expand All @@ -66,10 +66,7 @@ pub(crate) fn invalid_module_name(
path.file_stem().unwrap().to_string_lossy()
};

if ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(&module_name))
{
if ignore_names.matches(&module_name) {
return None;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use ruff_python_ast::{Alias, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{Alias, Stmt};
use ruff_python_stdlib::str;
use ruff_text_size::Ranged;

use crate::settings::types::IdentifierPattern;
use crate::rules::pep8_naming::settings::IgnoreNames;

/// ## What it does
/// Checks for lowercase imports that are aliased to non-lowercase names.
Expand Down Expand Up @@ -50,12 +49,9 @@ pub(crate) fn lowercase_imported_as_non_lowercase(
asname: &str,
alias: &Alias,
stmt: &Stmt,
ignore_names: &[IdentifierPattern],
ignore_names: &IgnoreNames,
) -> Option<Diagnostic> {
if ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(asname))
{
if ignore_names.matches(asname) {
return None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@ pub(crate) fn mixed_case_variable_in_class_scope(
name: &str,
arguments: Option<&Arguments>,
) {
if checker
.settings
.pep8_naming
.ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(name))
{
if checker.settings.pep8_naming.ignore_names.matches(name) {
return;
}
if !helpers::is_mixed_case(name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,7 @@ impl Violation for MixedCaseVariableInGlobalScope {

/// N816
pub(crate) fn mixed_case_variable_in_global_scope(checker: &mut Checker, expr: &Expr, name: &str) {
if checker
.settings
.pep8_naming
.ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(name))
{
if checker.settings.pep8_naming.ignore_names.matches(name) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,7 @@ pub(crate) fn non_lowercase_variable_in_function(checker: &mut Checker, expr: &E
}

// Ignore explicitly-allowed names.
if checker
.settings
.pep8_naming
.ignore_names
.iter()
.any(|ignore_name| ignore_name.matches(name))
{
if checker.settings.pep8_naming.ignore_names.matches(name) {
return;
}

Expand Down
Loading

0 comments on commit e585942

Please sign in to comment.