-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Adds a rule that bans too many positional (i.e. not keyword-only) parameters in function definitions. Fixes #8946 Rule ID code taken from pylint-dev/pylint#9278 ## Test Plan 1. fixtures file checking multiple OKs/fails 2. parametrized test file
- Loading branch information
1 parent
060a25d
commit b90027d
Showing
12 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
crates/ruff_linter/resources/test/fixtures/pylint/too_many_positional.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
def f(x, y, z, t, u, v, w, r): # Too many positional arguments (8/3) | ||
pass | ||
|
||
|
||
def f(x): # OK | ||
pass | ||
|
||
|
||
def f(x, y, z, _t, _u, _v, _w, r): # OK (underscore-prefixed names are ignored | ||
pass | ||
|
||
|
||
def f(x, y, z, *, u=1, v=1, r=1): # OK | ||
pass | ||
|
||
|
||
def f(x=1, y=1, z=1): # OK | ||
pass | ||
|
||
|
||
def f(x, y, z, /, u, v, w): # Too many positional arguments (6/3) | ||
pass | ||
|
||
|
||
def f(x, y, z, *, u, v, w): # OK | ||
pass | ||
|
||
|
||
def f(x, y, z, a, b, c, *, u, v, w): # Too many positional arguments (6/3) | ||
pass |
10 changes: 10 additions & 0 deletions
10
crates/ruff_linter/resources/test/fixtures/pylint/too_many_positional_params.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Too many positional arguments (7/4) for max_positional=4 | ||
# OK for dummy_variable_rgx ~ "skip_.*" | ||
def f(w, x, y, z, skip_t, skip_u, skip_v): | ||
pass | ||
|
||
|
||
# Too many positional arguments (7/4) for max_args=4 | ||
# Too many positional arguments (7/3) for dummy_variable_rgx ~ "skip_.*" | ||
def f(w, x, y, z, t, u, v): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
crates/ruff_linter/src/rules/pylint/rules/too_many_positional.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{identifier::Identifier, Parameters, Stmt}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for function definitions that include too many positional arguments. | ||
/// | ||
/// By default, this rule allows up to five arguments, as configured by the | ||
/// [`pylint.max-positional-args`] option. | ||
/// | ||
/// ## Why is this bad? | ||
/// Functions with many arguments are harder to understand, maintain, and call. | ||
/// This is especially true for functions with many positional arguments, as | ||
/// providing arguments positionally is more error-prone and less clear to | ||
/// readers than providing arguments by name. | ||
/// | ||
/// Consider refactoring functions with many arguments into smaller functions | ||
/// with fewer arguments, using objects to group related arguments, or | ||
/// migrating to keyword-only arguments. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// def plot(x, y, z, color, mark, add_trendline): | ||
/// ... | ||
/// | ||
/// | ||
/// plot(1, 2, 3, "r", "*", True) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// def plot(x, y, z, *, color, mark, add_trendline): | ||
/// ... | ||
/// | ||
/// | ||
/// plot(1, 2, 3, color="r", mark="*", add_trendline=True) | ||
/// ``` | ||
/// | ||
/// ## Options | ||
/// - `pylint.max-positional-args` | ||
#[violation] | ||
pub struct TooManyPositional { | ||
c_pos: usize, | ||
max_pos: usize, | ||
} | ||
|
||
impl Violation for TooManyPositional { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let TooManyPositional { c_pos, max_pos } = self; | ||
format!("Too many positional arguments: ({c_pos}/{max_pos})") | ||
} | ||
} | ||
|
||
/// PLR0917 | ||
pub(crate) fn too_many_positional(checker: &mut Checker, parameters: &Parameters, stmt: &Stmt) { | ||
let num_positional_args = parameters | ||
.args | ||
.iter() | ||
.chain(¶meters.posonlyargs) | ||
.filter(|arg| { | ||
!checker | ||
.settings | ||
.dummy_variable_rgx | ||
.is_match(&arg.parameter.name) | ||
}) | ||
.count(); | ||
if num_positional_args > checker.settings.pylint.max_positional_args { | ||
checker.diagnostics.push(Diagnostic::new( | ||
TooManyPositional { | ||
c_pos: num_positional_args, | ||
max_pos: checker.settings.pylint.max_positional_args, | ||
}, | ||
stmt.identifier(), | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...s/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR0917_too_many_positional.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
too_many_positional.py:1:5: PLR0917 Too many positional arguments: (8/5) | ||
| | ||
1 | def f(x, y, z, t, u, v, w, r): # Too many positional arguments (8/3) | ||
| ^ PLR0917 | ||
2 | pass | ||
| | ||
|
||
too_many_positional.py:21:5: PLR0917 Too many positional arguments: (6/5) | ||
| | ||
21 | def f(x, y, z, /, u, v, w): # Too many positional arguments (6/3) | ||
| ^ PLR0917 | ||
22 | pass | ||
| | ||
|
||
too_many_positional.py:29:5: PLR0917 Too many positional arguments: (6/5) | ||
| | ||
29 | def f(x, y, z, a, b, c, *, u, v, w): # Too many positional arguments (6/3) | ||
| ^ PLR0917 | ||
30 | pass | ||
| | ||
|
||
|
22 changes: 22 additions & 0 deletions
22
...er/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__max_positional_args.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
too_many_positional_params.py:3:5: PLR0917 Too many positional arguments: (7/4) | ||
| | ||
1 | # Too many positional arguments (7/4) for max_positional=4 | ||
2 | # OK for dummy_variable_rgx ~ "skip_.*" | ||
3 | def f(w, x, y, z, skip_t, skip_u, skip_v): | ||
| ^ PLR0917 | ||
4 | pass | ||
| | ||
|
||
too_many_positional_params.py:9:5: PLR0917 Too many positional arguments: (7/4) | ||
| | ||
7 | # Too many positional arguments (7/4) for max_args=4 | ||
8 | # Too many positional arguments (7/3) for dummy_variable_rgx ~ "skip_.*" | ||
9 | def f(w, x, y, z, t, u, v): | ||
| ^ PLR0917 | ||
10 | pass | ||
| | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.