Skip to content

Commit

Permalink
[pylint] Don't recommend decorating staticmethods with `@singledisp…
Browse files Browse the repository at this point in the history
…atch` (`PLE1519`, `PLE1520`) (#10637)

Co-authored-by: Alex Waygood <[email protected]>
  • Loading branch information
alex-700 and AlexWaygood authored Apr 2, 2024
1 parent b90e6df commit 0de2376
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def move(self, position):
def place(self, position):
pass

@singledispatch
@singledispatch # [singledispatch-method]
@staticmethod
def do(position):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def convert_position(cls, position):
def move(self, position):
pass

@singledispatchmethod # [singledispatchmethod-function]
@singledispatchmethod # Ok
@staticmethod
def do(position):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use crate::checkers::ast::Checker;
use crate::importer::ImportRequest;

/// ## What it does
/// Checks for `@singledispatch` decorators on class and instance methods.
/// Checks for methods decorated with `@singledispatch`.
///
/// ## Why is this bad?
/// The `@singledispatch` decorator is intended for use with functions, not methods.
///
/// Instead, use the `@singledispatchmethod` decorator, or migrate the method to a
/// standalone function or `@staticmethod`.
/// standalone function.
///
/// ## Example
/// ```python
Expand Down Expand Up @@ -88,7 +88,9 @@ pub(crate) fn singledispatch_method(
);
if !matches!(
type_,
function_type::FunctionType::Method | function_type::FunctionType::ClassMethod
function_type::FunctionType::Method
| function_type::FunctionType::ClassMethod
| function_type::FunctionType::StaticMethod
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ use crate::checkers::ast::Checker;
use crate::importer::ImportRequest;

/// ## What it does
/// Checks for `@singledispatchmethod` decorators on functions or static
/// methods.
/// Checks for non-method functions decorated with `@singledispatchmethod`.
///
/// ## Why is this bad?
/// The `@singledispatchmethod` decorator is intended for use with class and
/// instance methods, not functions.
/// The `@singledispatchmethod` decorator is intended for use with methods, not
/// functions.
///
/// Instead, use the `@singledispatch` decorator.
///
Expand Down Expand Up @@ -85,10 +84,7 @@ pub(crate) fn singledispatchmethod_function(
&checker.settings.pep8_naming.classmethod_decorators,
&checker.settings.pep8_naming.staticmethod_decorators,
);
if !matches!(
type_,
function_type::FunctionType::Function | function_type::FunctionType::StaticMethod
) {
if !matches!(type_, function_type::FunctionType::Function) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,25 @@ singledispatch_method.py:15:5: PLE1519 [*] `@singledispatch` decorator should no
15 |+ @singledispatchmethod # [singledispatch-method]
16 16 | def move(self, position):
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,4 @@ singledispatchmethod_function.py:4:1: PLE1520 [*] `@singledispatchmethod` decora
4 |+@singledispatch # [singledispatchmethod-function]
5 5 | def convert_position(position):
6 6 | pass
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
7 7 |

0 comments on commit 0de2376

Please sign in to comment.