-
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.
[pylint] Implement
too-many-public-methods
rule (PLR0904)
Implement https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/too-many-public-methods.html
- Loading branch information
Showing
9 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
crates/ruff/resources/test/fixtures/pylint/too_many_public_methods.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,66 @@ | ||
class Everything: | ||
foo = 1 | ||
|
||
def __init__(self): | ||
pass | ||
|
||
def _private(self): | ||
pass | ||
|
||
def method1(self): | ||
pass | ||
|
||
def method2(self): | ||
pass | ||
|
||
def method3(self): | ||
pass | ||
|
||
def method4(self): | ||
pass | ||
|
||
def method5(self): | ||
pass | ||
|
||
def method6(self): | ||
pass | ||
|
||
def method7(self): | ||
pass | ||
|
||
def method8(self): | ||
pass | ||
|
||
def method9(self): | ||
pass | ||
|
||
def method10(self): | ||
pass | ||
|
||
class Small: | ||
def __init__(self): | ||
pass | ||
|
||
def _private(self): | ||
pass | ||
|
||
def method1(self): | ||
pass | ||
|
||
def method2(self): | ||
pass | ||
|
||
def method3(self): | ||
pass | ||
|
||
def method4(self): | ||
pass | ||
|
||
def method5(self): | ||
pass | ||
|
||
def method6(self): | ||
pass | ||
|
||
def method7(self): | ||
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
126 changes: 126 additions & 0 deletions
126
crates/ruff/src/rules/pylint/rules/too_many_public_methods.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,126 @@ | ||
use ruff_python_ast::{self as ast, Stmt}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for classes with too many public methods | ||
/// | ||
/// By default, this rule allows up to 20 statements, as configured by the | ||
/// `pylint.max-public-methods` option. | ||
/// | ||
/// ## Why is this bad? | ||
/// Classes with many public methods are harder to understand | ||
/// and maintain. | ||
/// | ||
/// Instead, consider refactoring the class into separate classes. | ||
/// | ||
/// ## Example | ||
/// With `pylint.max-public-settings` set to 5 | ||
/// | ||
/// ```python | ||
/// class Linter: | ||
/// def __init__(self): | ||
/// pass | ||
/// | ||
/// def pylint(self): | ||
/// pass | ||
/// | ||
/// def pylint_settings(self): | ||
/// pass | ||
/// | ||
/// def flake8(self): | ||
/// pass | ||
/// | ||
/// def flake8_settings(self): | ||
/// pass | ||
/// | ||
/// def pydocstyle(self): | ||
/// pass | ||
/// | ||
/// def pydocstyle_settings(self): | ||
/// pass | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// class Linter: | ||
/// def __init__(self): | ||
/// self.pylint = Pylint() | ||
/// self.flake8 = Flake8() | ||
/// self.pydocstyle = Pydocstyle() | ||
/// | ||
/// def lint(self): | ||
/// pass | ||
/// | ||
/// | ||
/// class Pylint: | ||
/// def lint(self): | ||
/// pass | ||
/// | ||
/// def settings(self): | ||
/// pass | ||
/// | ||
/// | ||
/// class Flake8: | ||
/// def lint(self): | ||
/// pass | ||
/// | ||
/// def settings(self): | ||
/// pass | ||
/// | ||
/// | ||
/// class Pydocstyle: | ||
/// def lint(self): | ||
/// pass | ||
/// | ||
/// def settings(self): | ||
/// pass | ||
/// ``` | ||
/// | ||
/// ## Options | ||
/// - `pylint.max-public-methods` | ||
#[violation] | ||
pub struct TooManyPublicMethods { | ||
methods: usize, | ||
max_methods: usize, | ||
} | ||
|
||
impl Violation for TooManyPublicMethods { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let TooManyPublicMethods { | ||
methods, | ||
max_methods, | ||
} = self; | ||
format!("Too many public methods ({methods} > {max_methods})") | ||
} | ||
} | ||
|
||
/// R0904 | ||
pub(crate) fn too_many_public_methods( | ||
checker: &mut Checker, | ||
class_def: &ast::StmtClassDef, | ||
max_methods: usize, | ||
) { | ||
let ast::StmtClassDef { body, range, .. } = class_def; | ||
let methods = body | ||
.iter() | ||
.filter(|stmt| match stmt { | ||
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. }) => !name.starts_with('_'), | ||
_ => false, | ||
}) | ||
.count(); | ||
|
||
if methods > max_methods { | ||
checker.diagnostics.push(Diagnostic::new( | ||
TooManyPublicMethods { | ||
methods, | ||
max_methods, | ||
}, | ||
*range, | ||
)); | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
.../ruff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__too_many_public_methods.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,49 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
--- | ||
too_many_public_methods.py:1:1: PLR0904 Too many public methods (10 > 7) | ||
| | ||
1 | / class Everything: | ||
2 | | foo = 1 | ||
3 | | | ||
4 | | def __init__(self): | ||
5 | | pass | ||
6 | | | ||
7 | | def _private(self): | ||
8 | | pass | ||
9 | | | ||
10 | | def method1(self): | ||
11 | | pass | ||
12 | | | ||
13 | | def method2(self): | ||
14 | | pass | ||
15 | | | ||
16 | | def method3(self): | ||
17 | | pass | ||
18 | | | ||
19 | | def method4(self): | ||
20 | | pass | ||
21 | | | ||
22 | | def method5(self): | ||
23 | | pass | ||
24 | | | ||
25 | | def method6(self): | ||
26 | | pass | ||
27 | | | ||
28 | | def method7(self): | ||
29 | | pass | ||
30 | | | ||
31 | | def method8(self): | ||
32 | | pass | ||
33 | | | ||
34 | | def method9(self): | ||
35 | | pass | ||
36 | | | ||
37 | | def method10(self): | ||
38 | | pass | ||
| |____________^ PLR0904 | ||
39 | | ||
40 | class Small: | ||
| | ||
|
||
|
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