Skip to content

Commit 7b0bc19

Browse files
mbyrnepr2Pierre-SassoulasDanielNoord
committed
Fix a crash when looking up an __init__ method (#7744)
* Fix a crash when a child class with an ``__init__`` method inherits from a parent class with an ``__init__`` class attribute. * `continue` if not a method. * Update pylint/checkers/classes/class_checker.py * Rename fragment Closes #7742 Co-authored-by: Pierre Sassoulas <[email protected]> Co-authored-by: Daniël van Noord <[email protected]>
1 parent 98ceab7 commit 7b0bc19

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/whatsnew/fragments/7742.bugfix

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash when a child class with an ``__init__`` method inherits from a parent class with an ``__init__`` class attribute.
2+
3+
Closes #7742

pylint/checkers/classes/class_checker.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2224,15 +2224,17 @@ def _is_mandatory_method_param(self, node: nodes.NodeNG) -> bool:
22242224

22252225

22262226
def _ancestors_to_call(
2227-
klass_node: nodes.ClassDef, method: str = "__init__"
2227+
klass_node: nodes.ClassDef, method_name: str = "__init__"
22282228
) -> dict[nodes.ClassDef, bases.UnboundMethod]:
22292229
"""Return a dictionary where keys are the list of base classes providing
22302230
the queried method, and so that should/may be called from the method node.
22312231
"""
22322232
to_call: dict[nodes.ClassDef, bases.UnboundMethod] = {}
22332233
for base_node in klass_node.ancestors(recurs=False):
22342234
try:
2235-
init_node: bases.UnboundMethod = next(base_node.igetattr(method))
2235+
init_node = next(base_node.igetattr(method_name))
2236+
if not isinstance(init_node, astroid.UnboundMethod):
2237+
continue
22362238
if init_node.is_abstract():
22372239
continue
22382240
to_call[base_node] = init_node

tests/functional/i/init_not_called.py

+11
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,14 @@ def __init__(self, num: float):
8484

8585
def __init__(self, num):
8686
super().__init__(round(num))
87+
88+
89+
# https://github.com/PyCQA/pylint/issues/7742
90+
# Crash when parent class has a class attribute named `__init__`
91+
class NoInitMethod:
92+
__init__ = 42
93+
94+
95+
class ChildNoInitMethod(NoInitMethod):
96+
def __init__(self):
97+
...

0 commit comments

Comments
 (0)