From 2ea70cb2165d35888b61abbb07d1e1b5a2184846 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 4 Dec 2023 18:32:46 -0500 Subject: [PATCH] Ignore @overrides and @overloads for too-many-positional --- .../src/checkers/ast/analyze/statement.rs | 2 +- .../rules/pylint/rules/too_many_positional.rs | 21 ++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs index 0f05d6165b22e..afb80e227a539 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs @@ -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( diff --git a/crates/ruff_linter/src/rules/pylint/rules/too_many_positional.rs b/crates/ruff_linter/src/rules/pylint/rules/too_many_positional.rs index 369b6c63bc6e9..bc424441c0b7d 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/too_many_positional.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/too_many_positional.rs @@ -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; @@ -55,11 +56,12 @@ 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(¶meters.posonlyargs) + .chain(&function_def.parameters.posonlyargs) .filter(|arg| { !checker .settings @@ -67,13 +69,22 @@ pub(crate) fn too_many_positional(checker: &mut Checker, parameters: &Parameters .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(), )); } }