-
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.
Implement pylint E1519 singledispatch-method #970
- Loading branch information
Showing
8 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
crates/ruff_linter/resources/test/fixtures/pylint/E1519.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,20 @@ | ||
from functools import singledispatch, singledispatchmethod | ||
|
||
|
||
@singledispatch | ||
def convert_position(position): | ||
pass | ||
|
||
class Board: | ||
@singledispatch # [singledispatch-method] | ||
@classmethod | ||
def convert_position(cls, position): | ||
pass | ||
|
||
@singledispatch # [singledispatch-method] | ||
def move(self, position): | ||
pass | ||
|
||
@singledispatchmethod | ||
def place(self, position): | ||
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
71 changes: 71 additions & 0 deletions
71
crates/ruff_linter/src/rules/pylint/rules/single_dispatch_method.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,71 @@ | ||
use ruff_diagnostics::{Diagnostic, FixAvailability, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{self as ast}; | ||
use ruff_python_semantic::{analyze::function_type, Scope}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for singledispatch decorators on class methods. | ||
/// | ||
/// ## Why is this bad? | ||
/// Single dispatch must happen on the type of first non self argument | ||
#[violation] | ||
pub struct SingleDispatchMethod; | ||
|
||
impl Violation for SingleDispatchMethod { | ||
const FIX_AVAILABILITY: FixAvailability = FixAvailability::None; | ||
|
||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("singledispatch decorator should not be used with methods, use singledispatchmethod instead.") | ||
} | ||
} | ||
|
||
/// E1519 | ||
pub(crate) fn single_dispatch_method( | ||
checker: &Checker, | ||
scope: &Scope, | ||
diagnostics: &mut Vec<Diagnostic>, | ||
) { | ||
let Some(func) = scope.kind.as_function() else { | ||
return; | ||
}; | ||
|
||
let ast::StmtFunctionDef { | ||
name, | ||
decorator_list, | ||
.. | ||
} = func; | ||
|
||
let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else { | ||
return; | ||
}; | ||
|
||
if !matches!( | ||
function_type::classify( | ||
name, | ||
decorator_list, | ||
parent, | ||
checker.semantic(), | ||
&checker.settings.pep8_naming.classmethod_decorators, | ||
&checker.settings.pep8_naming.staticmethod_decorators, | ||
), | ||
function_type::FunctionType::Method | function_type::FunctionType::ClassMethod | ||
) { | ||
return; | ||
} | ||
|
||
for decorator in decorator_list { | ||
if checker | ||
.semantic() | ||
.resolve_call_path(&decorator.expression) | ||
.is_some_and(|call_path| { | ||
matches!(call_path.as_slice(), ["functools", "singledispatch"]) | ||
}) | ||
{ | ||
diagnostics.push(Diagnostic::new(SingleDispatchMethod {}, decorator.range())); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...inter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE1519_E1519.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,21 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
E1519.py:9:5: PLE1519 singledispatch decorator should not be used with methods, use singledispatchmethod instead. | ||
| | ||
8 | class Board: | ||
9 | @singledispatch # [singledispatch-method] | ||
| ^^^^^^^^^^^^^^^ PLE1519 | ||
10 | @classmethod | ||
11 | def convert_position(cls, position): | ||
| | ||
|
||
E1519.py:14:5: PLE1519 singledispatch decorator should not be used with methods, use singledispatchmethod instead. | ||
| | ||
12 | pass | ||
13 | | ||
14 | @singledispatch # [singledispatch-method] | ||
| ^^^^^^^^^^^^^^^ PLE1519 | ||
15 | def move(self, position): | ||
16 | pass | ||
| |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.