-
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-few-public-methods
rule (PLR0903
)
- Loading branch information
1 parent
8379841
commit 366bd8e
Showing
12 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
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
15 changes: 15 additions & 0 deletions
15
crates/ruff_linter/resources/test/fixtures/pylint/too_few_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,15 @@ | ||
from __future__ import annotations | ||
|
||
|
||
class Point: # PLR0903 | ||
def __init__(self, x: float, y: float) -> None: | ||
self.x = x | ||
self.y = y | ||
|
||
|
||
class Rectangle: # OK | ||
def __init__(self, top_left: Point, bottom_right: Point) -> None: | ||
... | ||
|
||
def area(self) -> float: | ||
... |
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
82 changes: 82 additions & 0 deletions
82
crates/ruff_linter/src/rules/pylint/rules/too_few_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,82 @@ | ||
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 few public methods | ||
/// | ||
/// By default, this rule allows down to 2 public methods, as configured by | ||
/// the [`pylint.min-public-methods`] option. | ||
/// | ||
/// ## Why is this bad? | ||
/// Classes with too few public methods are possibly better off | ||
/// being a dataclass or a namedtuple, which are more lightweight. | ||
/// | ||
/// ## Example | ||
/// Assuming that `pylint.min-public-settings` is set to 2: | ||
/// ```python | ||
/// class Point: | ||
/// def __init__(self, x: float, y: float): | ||
/// self.x = x | ||
/// self.y = y | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from dataclasses import dataclass | ||
/// | ||
/// | ||
/// @dataclass | ||
/// class Point: | ||
/// x: float | ||
/// y: float | ||
/// ``` | ||
/// | ||
/// ## Options | ||
/// - `pylint.min-public-methods` | ||
#[violation] | ||
pub struct TooFewPublicMethods { | ||
methods: usize, | ||
min_methods: usize, | ||
} | ||
|
||
impl Violation for TooFewPublicMethods { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let TooFewPublicMethods { | ||
methods, | ||
min_methods, | ||
} = self; | ||
format!("Too few public methods ({methods} < {min_methods})") | ||
} | ||
} | ||
|
||
/// R0903 | ||
pub(crate) fn too_few_public_methods( | ||
checker: &mut Checker, | ||
class_def: &ast::StmtClassDef, | ||
min_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 < min_methods { | ||
checker.diagnostics.push(Diagnostic::new( | ||
TooFewPublicMethods { | ||
methods, | ||
min_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
13 changes: 13 additions & 0 deletions
13
...src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__too_few_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,13 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
too_few_public_methods.py:4:1: PLR0903 Too few public methods (1 < 2) | ||
| | ||
4 | / class Point: # PLR0903 | ||
5 | | def __init__(self, x: float, y: float) -> None: | ||
6 | | self.x = x | ||
7 | | self.y = y | ||
| |__________________^ PLR0903 | ||
| | ||
|
||
|
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.