-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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) (#6179)
Implement https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/too-many-public-methods.html Confusingly the rule page mentions a max of 7 while in practice it is 20. https://github.com/search?q=repo%3Apylint-dev%2Fpylint+max-public-methods&type=code ## Summary Implement pylint's R0904 ## Test Plan Unit tests.
- Loading branch information
Showing
12 changed files
with
283 additions
and
2 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
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 | ||
|
||
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 |
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_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast as ast; | ||
use ruff_python_semantic::analyze::visibility::{self, Visibility::Public}; | ||
use ruff_text_size::Ranged; | ||
|
||
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 | ||
/// Assuming that `pylint.max-public-settings` is 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 methods = class_def | ||
.body | ||
.iter() | ||
.filter(|stmt| { | ||
stmt.as_function_def_stmt() | ||
.is_some_and(|node| matches!(visibility::method_visibility(node), Public)) | ||
}) | ||
.count(); | ||
|
||
if methods > max_methods { | ||
checker.diagnostics.push(Diagnostic::new( | ||
TooManyPublicMethods { | ||
methods, | ||
max_methods, | ||
}, | ||
class_def.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
46 changes: 46 additions & 0 deletions
46
.../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,46 @@ | ||
--- | ||
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 | ||
| |____________^ PLR0904 | ||
36 | | ||
37 | 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.