Skip to content

Commit 9c3a2fc

Browse files
authored
Fix unexpected-special-method-signature false positive for __init_subclass__ methods with one or more arguments. (#6654)
Closes #6644
1 parent e830eed commit 9c3a2fc

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Release date: TBA
2525

2626
Closes #6076
2727

28+
* Fix ``unexpected-special-method-signature`` false positive for ``__init_subclass__`` methods with one or more arguments.
29+
30+
Closes #6644
31+
2832
* Started ignoring underscore as a local variable for ``too-many-locals``.
2933

3034
Closes #6488

doc/whatsnew/2.14.rst

+5
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ Other Changes
273273

274274
Closes #6594
275275

276+
* Fix ``unexpected-special-method-signature`` false positive for ``__init_subclass__`` methods with one or more arguments.
277+
278+
Closes #6644
279+
280+
276281
Deprecations
277282
============
278283

pylint/checkers/utils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
# although it's best to implement it in order to accept
7070
# all of them.
7171
_SPECIAL_METHODS_PARAMS = {
72-
None: ("__new__", "__init__", "__call__"),
72+
None: ("__new__", "__init__", "__call__", "__init_subclass__"),
7373
0: (
7474
"__del__",
7575
"__repr__",
@@ -107,7 +107,6 @@
107107
"__anext__",
108108
"__fspath__",
109109
"__subclasses__",
110-
"__init_subclass__",
111110
),
112111
1: (
113112
"__format__",

tests/functional/u/unexpected_special_method_signature.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ def __getattr__(self, nanana): # [unexpected-special-method-signature]
3333
def __subclasses__(self, blabla): # [unexpected-special-method-signature]
3434
pass
3535

36-
@classmethod
37-
def __init_subclass__(cls, blabla): # [unexpected-special-method-signature]
38-
pass
3936

4037
class FirstBadContextManager(object):
4138
def __enter__(self):
@@ -106,6 +103,10 @@ def __enter__():
106103
def __getitem__(index):
107104
pass
108105

106+
@classmethod
107+
def __init_subclass__(cls, blabla):
108+
pass
109+
109110

110111
class FirstGoodContextManager(object):
111112
def __enter__(self):
@@ -124,3 +125,14 @@ def __enter__(self):
124125
return self
125126
def __exit__(self, exc_type, *args):
126127
pass
128+
129+
130+
# unexpected-special-method-signature
131+
# https://github.com/PyCQA/pylint/issues/6644
132+
class Philosopher:
133+
def __init_subclass__(cls, default_name, **kwargs):
134+
super().__init_subclass__(**kwargs)
135+
cls.default_name = default_name
136+
137+
class AustralianPhilosopher(Philosopher, default_name="Bruce"):
138+
pass

tests/functional/u/unexpected_special_method_signature.txt

+8-9
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ unexpected-special-method-signature:23:4:23:20:Invalid.__deepcopy__:The special
77
no-method-argument:26:4:26:16:Invalid.__iter__:Method has no argument:UNDEFINED
88
unexpected-special-method-signature:30:4:30:19:Invalid.__getattr__:The special method '__getattr__' expects 1 param(s), 2 were given:UNDEFINED
99
unexpected-special-method-signature:33:4:33:22:Invalid.__subclasses__:The special method '__subclasses__' expects 0 param(s), 1 was given:UNDEFINED
10-
unexpected-special-method-signature:37:4:37:25:Invalid.__init_subclass__:The special method '__init_subclass__' expects 0 param(s), 1 was given:UNDEFINED
11-
unexpected-special-method-signature:43:4:43:16:FirstBadContextManager.__exit__:The special method '__exit__' expects 3 param(s), 1 was given:UNDEFINED
12-
unexpected-special-method-signature:49:4:49:16:SecondBadContextManager.__exit__:The special method '__exit__' expects 3 param(s), 4 were given:UNDEFINED
13-
unexpected-special-method-signature:57:4:57:16:ThirdBadContextManager.__exit__:The special method '__exit__' expects 3 param(s), 4 were given:UNDEFINED
14-
unexpected-special-method-signature:63:4:63:17:Async.__aiter__:The special method '__aiter__' expects 0 param(s), 1 was given:UNDEFINED
15-
unexpected-special-method-signature:65:4:65:17:Async.__anext__:The special method '__anext__' expects 0 param(s), 2 were given:UNDEFINED
16-
unexpected-special-method-signature:67:4:67:17:Async.__await__:The special method '__await__' expects 0 param(s), 1 was given:UNDEFINED
17-
unexpected-special-method-signature:69:4:69:18:Async.__aenter__:The special method '__aenter__' expects 0 param(s), 1 was given:UNDEFINED
18-
unexpected-special-method-signature:71:4:71:17:Async.__aexit__:The special method '__aexit__' expects 3 param(s), 0 was given:UNDEFINED
10+
unexpected-special-method-signature:40:4:40:16:FirstBadContextManager.__exit__:The special method '__exit__' expects 3 param(s), 1 was given:UNDEFINED
11+
unexpected-special-method-signature:46:4:46:16:SecondBadContextManager.__exit__:The special method '__exit__' expects 3 param(s), 4 were given:UNDEFINED
12+
unexpected-special-method-signature:54:4:54:16:ThirdBadContextManager.__exit__:The special method '__exit__' expects 3 param(s), 4 were given:UNDEFINED
13+
unexpected-special-method-signature:60:4:60:17:Async.__aiter__:The special method '__aiter__' expects 0 param(s), 1 was given:UNDEFINED
14+
unexpected-special-method-signature:62:4:62:17:Async.__anext__:The special method '__anext__' expects 0 param(s), 2 were given:UNDEFINED
15+
unexpected-special-method-signature:64:4:64:17:Async.__await__:The special method '__await__' expects 0 param(s), 1 was given:UNDEFINED
16+
unexpected-special-method-signature:66:4:66:18:Async.__aenter__:The special method '__aenter__' expects 0 param(s), 1 was given:UNDEFINED
17+
unexpected-special-method-signature:68:4:68:17:Async.__aexit__:The special method '__aexit__' expects 3 param(s), 0 was given:UNDEFINED

0 commit comments

Comments
 (0)