-
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 Implement `bad-staticmethod-argument` from pylint, part of #970. ## Test Plan Text fixture added.
- Loading branch information
1 parent
2e7a1a4
commit c2790f9
Showing
8 changed files
with
200 additions
and
11 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
crates/ruff_linter/resources/test/fixtures/pylint/bad_staticmethod_argument.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,44 @@ | ||
class Wolf: | ||
@staticmethod | ||
def eat(self): # [bad-staticmethod-argument] | ||
pass | ||
|
||
|
||
class Wolf: | ||
@staticmethod | ||
def eat(sheep): | ||
pass | ||
|
||
|
||
class Sheep: | ||
@staticmethod | ||
def eat(cls, x, y, z): # [bad-staticmethod-argument] | ||
pass | ||
|
||
@staticmethod | ||
def sleep(self, x, y, z): # [bad-staticmethod-argument] | ||
pass | ||
|
||
def grow(self, x, y, z): | ||
pass | ||
|
||
@classmethod | ||
def graze(cls, x, y, z): | ||
pass | ||
|
||
|
||
class Foo: | ||
@staticmethod | ||
def eat(x, self, z): | ||
pass | ||
|
||
@staticmethod | ||
def sleep(x, cls, z): | ||
pass | ||
|
||
def grow(self, x, y, z): | ||
pass | ||
|
||
@classmethod | ||
def graze(cls, x, y, z): | ||
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
103 changes: 103 additions & 0 deletions
103
crates/ruff_linter/src/rules/pylint/rules/bad_staticmethod_argument.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,103 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast as ast; | ||
use ruff_python_ast::ParameterWithDefault; | ||
use ruff_python_semantic::analyze::function_type; | ||
use ruff_python_semantic::Scope; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for static methods that use `self` or `cls` as their first argument. | ||
/// | ||
/// ## Why is this bad? | ||
/// [PEP 8] recommends the use of `self` and `cls` as the first arguments for | ||
/// instance methods and class methods, respectively. Naming the first argument | ||
/// of a static method as `self` or `cls` can be misleading, as static methods | ||
/// do not receive an instance or class reference as their first argument. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// class Wolf: | ||
/// @staticmethod | ||
/// def eat(self): | ||
/// pass | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// class Wolf: | ||
/// @staticmethod | ||
/// def eat(sheep): | ||
/// pass | ||
/// ``` | ||
/// | ||
/// [PEP 8]: https://peps.python.org/pep-0008/#function-and-method-arguments | ||
#[violation] | ||
pub struct BadStaticmethodArgument { | ||
argument_name: String, | ||
} | ||
|
||
impl Violation for BadStaticmethodArgument { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let Self { argument_name } = self; | ||
format!("First argument of a static method should not be named `{argument_name}`") | ||
} | ||
} | ||
|
||
/// PLW0211 | ||
pub(crate) fn bad_staticmethod_argument( | ||
checker: &Checker, | ||
scope: &Scope, | ||
diagnostics: &mut Vec<Diagnostic>, | ||
) { | ||
let Some(func) = scope.kind.as_function() else { | ||
return; | ||
}; | ||
|
||
let ast::StmtFunctionDef { | ||
name, | ||
decorator_list, | ||
parameters, | ||
.. | ||
} = func; | ||
|
||
let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else { | ||
return; | ||
}; | ||
|
||
let type_ = function_type::classify( | ||
name, | ||
decorator_list, | ||
parent, | ||
checker.semantic(), | ||
&checker.settings.pep8_naming.classmethod_decorators, | ||
&checker.settings.pep8_naming.staticmethod_decorators, | ||
); | ||
if !matches!(type_, function_type::FunctionType::StaticMethod) { | ||
return; | ||
} | ||
|
||
let Some(ParameterWithDefault { | ||
parameter: self_or_cls, | ||
.. | ||
}) = parameters | ||
.posonlyargs | ||
.first() | ||
.or_else(|| parameters.args.first()) | ||
else { | ||
return; | ||
}; | ||
|
||
if matches!(self_or_cls.name.as_str(), "self" | "cls") { | ||
let diagnostic = Diagnostic::new( | ||
BadStaticmethodArgument { | ||
argument_name: self_or_cls.name.to_string(), | ||
}, | ||
self_or_cls.range(), | ||
); | ||
diagnostics.push(diagnostic); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
...nt/snapshots/ruff_linter__rules__pylint__tests__PLW0211_bad_staticmethod_argument.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,28 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
bad_staticmethod_argument.py:3:13: PLW0211 First argument of a static method should not be named `self` | ||
| | ||
1 | class Wolf: | ||
2 | @staticmethod | ||
3 | def eat(self): # [bad-staticmethod-argument] | ||
| ^^^^ PLW0211 | ||
4 | pass | ||
| | ||
|
||
bad_staticmethod_argument.py:15:13: PLW0211 First argument of a static method should not be named `cls` | ||
| | ||
13 | class Sheep: | ||
14 | @staticmethod | ||
15 | def eat(cls, x, y, z): # [bad-staticmethod-argument] | ||
| ^^^ PLW0211 | ||
16 | pass | ||
| | ||
|
||
bad_staticmethod_argument.py:19:15: PLW0211 First argument of a static method should not be named `self` | ||
| | ||
18 | @staticmethod | ||
19 | def sleep(self, x, y, z): # [bad-staticmethod-argument] | ||
| ^^^^ PLW0211 | ||
20 | pass | ||
| |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.