mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:37 +00:00
[pylint
] Don't recommend decorating staticmethods with @singledispatch
(PLE1519
, PLE1520
) (#10637)
Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
This commit is contained in:
parent
b90e6df5cc
commit
0de23760ff
6 changed files with 34 additions and 42 deletions
|
@ -20,7 +20,7 @@ class Board:
|
||||||
def place(self, position):
|
def place(self, position):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@singledispatch
|
@singledispatch # [singledispatch-method]
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def do(position):
|
def do(position):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -17,7 +17,7 @@ class Board:
|
||||||
def move(self, position):
|
def move(self, position):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@singledispatchmethod # [singledispatchmethod-function]
|
@singledispatchmethod # Ok
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def do(position):
|
def do(position):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -9,13 +9,13 @@ use crate::checkers::ast::Checker;
|
||||||
use crate::importer::ImportRequest;
|
use crate::importer::ImportRequest;
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
/// Checks for `@singledispatch` decorators on class and instance methods.
|
/// Checks for methods decorated with `@singledispatch`.
|
||||||
///
|
///
|
||||||
/// ## Why is this bad?
|
/// ## Why is this bad?
|
||||||
/// The `@singledispatch` decorator is intended for use with functions, not methods.
|
/// The `@singledispatch` decorator is intended for use with functions, not methods.
|
||||||
///
|
///
|
||||||
/// Instead, use the `@singledispatchmethod` decorator, or migrate the method to a
|
/// Instead, use the `@singledispatchmethod` decorator, or migrate the method to a
|
||||||
/// standalone function or `@staticmethod`.
|
/// standalone function.
|
||||||
///
|
///
|
||||||
/// ## Example
|
/// ## Example
|
||||||
/// ```python
|
/// ```python
|
||||||
|
@ -88,7 +88,9 @@ pub(crate) fn singledispatch_method(
|
||||||
);
|
);
|
||||||
if !matches!(
|
if !matches!(
|
||||||
type_,
|
type_,
|
||||||
function_type::FunctionType::Method | function_type::FunctionType::ClassMethod
|
function_type::FunctionType::Method
|
||||||
|
| function_type::FunctionType::ClassMethod
|
||||||
|
| function_type::FunctionType::StaticMethod
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,12 +9,11 @@ use crate::checkers::ast::Checker;
|
||||||
use crate::importer::ImportRequest;
|
use crate::importer::ImportRequest;
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
/// Checks for `@singledispatchmethod` decorators on functions or static
|
/// Checks for non-method functions decorated with `@singledispatchmethod`.
|
||||||
/// methods.
|
|
||||||
///
|
///
|
||||||
/// ## Why is this bad?
|
/// ## Why is this bad?
|
||||||
/// The `@singledispatchmethod` decorator is intended for use with class and
|
/// The `@singledispatchmethod` decorator is intended for use with methods, not
|
||||||
/// instance methods, not functions.
|
/// functions.
|
||||||
///
|
///
|
||||||
/// Instead, use the `@singledispatch` decorator.
|
/// Instead, use the `@singledispatch` decorator.
|
||||||
///
|
///
|
||||||
|
@ -85,10 +84,7 @@ pub(crate) fn singledispatchmethod_function(
|
||||||
&checker.settings.pep8_naming.classmethod_decorators,
|
&checker.settings.pep8_naming.classmethod_decorators,
|
||||||
&checker.settings.pep8_naming.staticmethod_decorators,
|
&checker.settings.pep8_naming.staticmethod_decorators,
|
||||||
);
|
);
|
||||||
if !matches!(
|
if !matches!(type_, function_type::FunctionType::Function) {
|
||||||
type_,
|
|
||||||
function_type::FunctionType::Function | function_type::FunctionType::StaticMethod
|
|
||||||
) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,3 +41,24 @@ singledispatch_method.py:15:5: PLE1519 [*] `@singledispatch` decorator should no
|
||||||
16 16 | def move(self, position):
|
16 16 | def move(self, position):
|
||||||
17 17 | pass
|
17 17 | pass
|
||||||
18 18 |
|
18 18 |
|
||||||
|
|
||||||
|
singledispatch_method.py:23:5: PLE1519 [*] `@singledispatch` decorator should not be used on methods
|
||||||
|
|
|
||||||
|
21 | pass
|
||||||
|
22 |
|
||||||
|
23 | @singledispatch # [singledispatch-method]
|
||||||
|
| ^^^^^^^^^^^^^^^ PLE1519
|
||||||
|
24 | @staticmethod
|
||||||
|
25 | def do(position):
|
||||||
|
|
|
||||||
|
= help: Replace with `@singledispatchmethod`
|
||||||
|
|
||||||
|
ℹ Unsafe fix
|
||||||
|
20 20 | def place(self, position):
|
||||||
|
21 21 | pass
|
||||||
|
22 22 |
|
||||||
|
23 |- @singledispatch # [singledispatch-method]
|
||||||
|
23 |+ @singledispatchmethod # [singledispatch-method]
|
||||||
|
24 24 | @staticmethod
|
||||||
|
25 25 | def do(position):
|
||||||
|
26 26 | pass
|
||||||
|
|
|
@ -20,30 +20,3 @@ singledispatchmethod_function.py:4:1: PLE1520 [*] `@singledispatchmethod` decora
|
||||||
5 5 | def convert_position(position):
|
5 5 | def convert_position(position):
|
||||||
6 6 | pass
|
6 6 | pass
|
||||||
7 7 |
|
7 7 |
|
||||||
|
|
||||||
singledispatchmethod_function.py:20:5: PLE1520 [*] `@singledispatchmethod` decorator should not be used on non-method functions
|
|
||||||
|
|
|
||||||
18 | pass
|
|
||||||
19 |
|
|
||||||
20 | @singledispatchmethod # [singledispatchmethod-function]
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^ PLE1520
|
|
||||||
21 | @staticmethod
|
|
||||||
22 | def do(position):
|
|
||||||
|
|
|
||||||
= help: Replace with `@singledispatch`
|
|
||||||
|
|
||||||
ℹ Unsafe fix
|
|
||||||
1 |-from functools import singledispatchmethod
|
|
||||||
1 |+from functools import singledispatchmethod, singledispatch
|
|
||||||
2 2 |
|
|
||||||
3 3 |
|
|
||||||
4 4 | @singledispatchmethod # [singledispatchmethod-function]
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
17 17 | def move(self, position):
|
|
||||||
18 18 | pass
|
|
||||||
19 19 |
|
|
||||||
20 |- @singledispatchmethod # [singledispatchmethod-function]
|
|
||||||
20 |+ @singledispatch # [singledispatchmethod-function]
|
|
||||||
21 21 | @staticmethod
|
|
||||||
22 22 | def do(position):
|
|
||||||
23 23 | pass
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue