[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:
Aleksei Latyshev 2024-04-02 18:47:31 +02:00 committed by GitHub
parent b90e6df5cc
commit 0de23760ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 34 additions and 42 deletions

View file

@ -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
@ -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;
}

View file

@ -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.
///
@ -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;
}

View file

@ -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

View file

@ -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 |