Skip to content

Commit 3ed7a8e

Browse files
Fix #5371: Correctly count arguments to static methods missing @staticmethod decorator (#5412)
* Fix #5371: Correctly count arguments to static methods missing @staticmethod decorator * Implementations of MapReduceMixin.reduce_map_data were actually not classmethods
1 parent 82f0f88 commit 3ed7a8e

File tree

7 files changed

+39
-12
lines changed

7 files changed

+39
-12
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ Release date: TBA
2323

2424
Closes #5323
2525

26+
* Fixed detection of ``arguments-differ`` when superclass static
27+
methods lacked a ``@staticmethod`` decorator.
28+
29+
Closes #5371
30+
2631
..
2732
Insert your changelog randomly, it will reduce merge conflicts
2833
(Ie. not necessarily at the end)

pylint/checkers/classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def _definition_equivalent_to_call(definition, call):
177177

178178
def _positional_parameters(method):
179179
positional = method.args.args
180-
if method.type in {"classmethod", "method"}:
180+
if method.is_bound() and method.type in {"classmethod", "method"}:
181181
positional = positional[1:]
182182
return positional
183183

pylint/checkers/mapreduce_checker.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class MapReduceMixin(metaclass=abc.ABCMeta):
1414
def get_map_data(self):
1515
"""Returns mergable/reducible data that will be examined"""
1616

17-
@classmethod
1817
@abc.abstractmethod
19-
def reduce_map_data(cls, linter, data):
18+
def reduce_map_data(self, linter, data):
2019
"""For a given Checker, receives data for all mapped runs"""

tests/functional/a/arguments_differ.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def close(self, attr):
141141

142142
class StaticmethodChild2(Staticmethod):
143143

144-
def func(self, data):
144+
def func(self, data): # [arguments-differ]
145145
super().func(data)
146146

147147

@@ -151,15 +151,23 @@ class SuperClass(object):
151151
def impl(arg1, arg2, **kwargs):
152152
return arg1 + arg2
153153

154+
def should_have_been_decorated_as_static(arg1, arg2): # pylint: disable=no-self-argument
155+
return arg1 + arg2
156+
154157

155158
class MyClass(SuperClass):
156159

157-
def impl(self, *args, **kwargs):
160+
@staticmethod
161+
def impl(*args, **kwargs):
158162
"""
159163
Acceptable use of vararg in subclass because it does not violate LSP.
160164
"""
161165
super().impl(*args, **kwargs)
162166

167+
@staticmethod
168+
def should_have_been_decorated_as_static(arg1, arg2):
169+
return arg1 + arg2
170+
163171

164172
class FirstHasArgs(object):
165173

tests/functional/a/arguments_differ.txt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ arguments-differ:23:4:24:12:ChildDefaults.test:Number of parameters was 3 in 'Pa
33
arguments-differ:41:4:42:12:ClassmethodChild.func:Number of parameters was 2 in 'Classmethod.func' and is now 0 in overridden 'ClassmethodChild.func' method:UNDEFINED
44
arguments-differ:68:4:69:64:VarargsChild.has_kwargs:Variadics removed in overridden 'VarargsChild.has_kwargs' method:UNDEFINED
55
arguments-renamed:71:4:72:89:VarargsChild.no_kwargs:Parameter 'args' has been renamed to 'arg' in overridden 'VarargsChild.no_kwargs' method:UNDEFINED
6-
arguments-differ:172:4:173:12:SecondChangesArgs.test:Number of parameters was 2 in 'FirstHasArgs.test' and is now 4 in overridden 'SecondChangesArgs.test' method:UNDEFINED
7-
arguments-differ:298:4:299:60:Foo.kwonly_1:Number of parameters was 4 in 'AbstractFoo.kwonly_1' and is now 3 in overridden 'Foo.kwonly_1' method:UNDEFINED
8-
arguments-differ:301:4:302:82:Foo.kwonly_2:Number of parameters was 3 in 'AbstractFoo.kwonly_2' and is now 2 in overridden 'Foo.kwonly_2' method:UNDEFINED
9-
arguments-differ:304:4:305:32:Foo.kwonly_3:Number of parameters was 3 in 'AbstractFoo.kwonly_3' and is now 3 in overridden 'Foo.kwonly_3' method:UNDEFINED
10-
arguments-differ:307:4:308:32:Foo.kwonly_4:Number of parameters was 3 in 'AbstractFoo.kwonly_4' and is now 3 in overridden 'Foo.kwonly_4' method:UNDEFINED
11-
arguments-differ:310:4:311:41:Foo.kwonly_5:Variadics removed in overridden 'Foo.kwonly_5' method:UNDEFINED
6+
arguments-differ:144:4:145:26:StaticmethodChild2.func:Number of parameters was 1 in 'Staticmethod.func' and is now 2 in overridden 'StaticmethodChild2.func' method:UNDEFINED
7+
arguments-differ:180:4:181:12:SecondChangesArgs.test:Number of parameters was 2 in 'FirstHasArgs.test' and is now 4 in overridden 'SecondChangesArgs.test' method:UNDEFINED
8+
arguments-differ:306:4:307:60:Foo.kwonly_1:Number of parameters was 4 in 'AbstractFoo.kwonly_1' and is now 3 in overridden 'Foo.kwonly_1' method:UNDEFINED
9+
arguments-differ:309:4:310:82:Foo.kwonly_2:Number of parameters was 3 in 'AbstractFoo.kwonly_2' and is now 2 in overridden 'Foo.kwonly_2' method:UNDEFINED
10+
arguments-differ:312:4:313:32:Foo.kwonly_3:Number of parameters was 3 in 'AbstractFoo.kwonly_3' and is now 3 in overridden 'Foo.kwonly_3' method:UNDEFINED
11+
arguments-differ:315:4:316:32:Foo.kwonly_4:Number of parameters was 3 in 'AbstractFoo.kwonly_4' and is now 3 in overridden 'Foo.kwonly_4' method:UNDEFINED
12+
arguments-differ:318:4:319:41:Foo.kwonly_5:Variadics removed in overridden 'Foo.kwonly_5' method:UNDEFINED
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""https://github.com/PyCQA/pylint/issues/5371"""
2+
from enum import Enum
3+
4+
5+
class MyEnum(Enum):
6+
"""
7+
Enum._generate_next_value_() in the stdlib currently lacks a
8+
@staticmethod decorator.
9+
"""
10+
11+
@staticmethod
12+
def _generate_next_value_(name: str, start: int, count: int, last_values: list):
13+
return 42

tests/test_check_parallel.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import pylint.interfaces
2121
import pylint.lint.parallel
2222
from pylint.checkers.base_checker import BaseChecker
23+
from pylint.checkers.mapreduce_checker import MapReduceMixin
2324
from pylint.lint import PyLinter
2425
from pylint.lint.parallel import _worker_check_single_file as worker_check_single_file
2526
from pylint.lint.parallel import _worker_initialize as worker_initialize
@@ -73,7 +74,7 @@ def process_module(self, _node: nodes.Module) -> None:
7374
self.data.append(record)
7475

7576

76-
class ParallelTestChecker(BaseChecker):
77+
class ParallelTestChecker(BaseChecker, MapReduceMixin):
7778
"""A checker that does need to consolidate data.
7879
7980
To simulate the need to consolidate data, this checker only

0 commit comments

Comments
 (0)