Skip to content

Commit

Permalink
Ignore @overrides and @overloads for too-many-positional (#9000)
Browse files Browse the repository at this point in the history
Same as `too-many-arguments`.
  • Loading branch information
charliermarsh authored Dec 4, 2023
1 parent 8d9912a commit b7ffd73
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
pylint::rules::too_many_arguments(checker, function_def);
}
if checker.enabled(Rule::TooManyPositional) {
pylint::rules::too_many_positional(checker, parameters, stmt);
pylint::rules::too_many_positional(checker, function_def);
}
if checker.enabled(Rule::TooManyReturnStatements) {
if let Some(diagnostic) = pylint::rules::too_many_return_statements(
Expand Down
21 changes: 16 additions & 5 deletions crates/ruff_linter/src/rules/pylint/rules/too_many_positional.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{identifier::Identifier, Parameters, Stmt};
use ruff_python_ast::{self as ast, identifier::Identifier};
use ruff_python_semantic::analyze::visibility;

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

Expand Down Expand Up @@ -55,25 +56,35 @@ impl Violation for TooManyPositional {
}

/// PLR0917
pub(crate) fn too_many_positional(checker: &mut Checker, parameters: &Parameters, stmt: &Stmt) {
let num_positional_args = parameters
pub(crate) fn too_many_positional(checker: &mut Checker, function_def: &ast::StmtFunctionDef) {
let num_positional_args = function_def
.parameters
.args
.iter()
.chain(&parameters.posonlyargs)
.chain(&function_def.parameters.posonlyargs)
.filter(|arg| {
!checker
.settings
.dummy_variable_rgx
.is_match(&arg.parameter.name)
})
.count();

if num_positional_args > checker.settings.pylint.max_positional_args {
// Allow excessive arguments in `@override` or `@overload` methods, since they're required
// to adhere to the parent signature.
if visibility::is_override(&function_def.decorator_list, checker.semantic())
|| visibility::is_overload(&function_def.decorator_list, checker.semantic())
{
return;
}

checker.diagnostics.push(Diagnostic::new(
TooManyPositional {
c_pos: num_positional_args,
max_pos: checker.settings.pylint.max_positional_args,
},
stmt.identifier(),
function_def.identifier(),
));
}
}

0 comments on commit b7ffd73

Please sign in to comment.