Skip to content

Commit

Permalink
Add separate file; tweak docs
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Dec 18, 2023
1 parent ba38c95 commit 5c62432
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def func() -> None: # Ok
def func() -> None: # OK
# 15 is max default
first = 1
second = 2
Expand All @@ -16,6 +16,7 @@ def func() -> None: # Ok
fourteenth = 14
fifteenth = 15


def func() -> None: # PLR0914
first = 1
second = 2
Expand Down
22 changes: 1 addition & 21 deletions crates/ruff_linter/src/checkers/ast/analyze/deferred_scopes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use ruff_diagnostics::Diagnostic;
use ruff_python_ast::identifier::Identifier;
use ruff_python_semantic::analyze::visibility;
use ruff_python_semantic::{Binding, BindingKind, ScopeKind};
use ruff_text_size::Ranged;
Expand Down Expand Up @@ -340,26 +339,7 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
}

if checker.enabled(Rule::TooManyLocals) {
// PLR0914
let num_locals = scope
.binding_ids()
.filter(|&id| {
let binding = checker.semantic.binding(id);
matches!(binding.kind, BindingKind::Assignment)
})
.count();

if num_locals > checker.settings.pylint.max_locals {
if let ScopeKind::Function(func) = scope.kind {
diagnostics.push(Diagnostic::new(
pylint::rules::TooManyLocals {
current_amount: num_locals,
max_amount: checker.settings.pylint.max_locals,
},
func.identifier(),
));
};
}
pylint::rules::too_many_locals(checker, scope, &mut diagnostics);
}
}
}
Expand Down
36 changes: 31 additions & 5 deletions crates/ruff_linter/src/rules/pylint/rules/too_many_locals.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use ruff_diagnostics::Violation;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::identifier::Identifier;
use ruff_python_semantic::{Scope, ScopeKind};

use crate::checkers::ast::Checker;

/// ## What it does
/// Checks for functions/methods that include too many local variables.
/// Checks for functions that include too many local variables.
///
/// By default, this rule allows up to fifteen arguments, as configured by the
/// By default, this rule allows up to fifteen locals, as configured by the
/// [`pylint.max-locals`] option.
///
/// ## Why is this bad?
Expand All @@ -17,8 +21,8 @@ use ruff_macros::{derive_message_formats, violation};
/// - `pylint.max-locals`
#[violation]
pub struct TooManyLocals {
pub(crate) current_amount: usize,
pub(crate) max_amount: usize,
current_amount: usize,
max_amount: usize,
}

impl Violation for TooManyLocals {
Expand All @@ -31,3 +35,25 @@ impl Violation for TooManyLocals {
format!("Too many local variables: ({current_amount}/{max_amount})")
}
}

/// PLR0914
pub(crate) fn too_many_locals(checker: &Checker, scope: &Scope, diagnostics: &mut Vec<Diagnostic>) {
let num_locals = scope
.binding_ids()
.filter(|id| {
let binding = checker.semantic().binding(*id);
binding.kind.is_assignment()
})
.count();
if num_locals > checker.settings.pylint.max_locals {
if let ScopeKind::Function(func) = scope.kind {
diagnostics.push(Diagnostic::new(
TooManyLocals {
current_amount: num_locals,
max_amount: checker.settings.pylint.max_locals,
},
func.identifier(),
));
};
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
---
too_many_locals.py:19:5: PLR0914 Too many local variables: (16/15)
too_many_locals.py:20:5: PLR0914 Too many local variables: (16/15)
|
17 | fifteenth = 15
18 |
19 | def func() -> None: # PLR0914
20 | def func() -> None: # PLR0914
| ^^^^ PLR0914
20 | first = 1
21 | second = 2
21 | first = 1
22 | second = 2
|


2 changes: 1 addition & 1 deletion crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2747,7 +2747,7 @@ impl PylintOptions {
max_public_methods: self
.max_public_methods
.unwrap_or(defaults.max_public_methods),
max_locals: defaults.max_locals,
max_locals: self.max_locals.unwrap_or(defaults.max_locals),
}
}
}
Expand Down

0 comments on commit 5c62432

Please sign in to comment.