Skip to content

Commit

Permalink
Fix a crash when looking up an __init__ method (#7744)
Browse files Browse the repository at this point in the history
* 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 <pierre.sassoulas@gmail.com>
Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 17, 2022
1 parent 98ceab7 commit 7b0bc19
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7742.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash when a child class with an ``__init__`` method inherits from a parent class with an ``__init__`` class attribute.

Closes #7742
6 changes: 4 additions & 2 deletions pylint/checkers/classes/class_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2224,15 +2224,17 @@ def _is_mandatory_method_param(self, node: nodes.NodeNG) -> bool:


def _ancestors_to_call(
klass_node: nodes.ClassDef, method: str = "__init__"
klass_node: nodes.ClassDef, method_name: str = "__init__"
) -> dict[nodes.ClassDef, bases.UnboundMethod]:
"""Return a dictionary where keys are the list of base classes providing
the queried method, and so that should/may be called from the method node.
"""
to_call: dict[nodes.ClassDef, bases.UnboundMethod] = {}
for base_node in klass_node.ancestors(recurs=False):
try:
init_node: bases.UnboundMethod = next(base_node.igetattr(method))
init_node = next(base_node.igetattr(method_name))
if not isinstance(init_node, astroid.UnboundMethod):
continue
if init_node.is_abstract():
continue
to_call[base_node] = init_node
Expand Down
11 changes: 11 additions & 0 deletions tests/functional/i/init_not_called.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,14 @@ def __init__(self, num: float):

def __init__(self, num):
super().__init__(round(num))


# https://github.com/PyCQA/pylint/issues/7742
# Crash when parent class has a class attribute named `__init__`
class NoInitMethod:
__init__ = 42


class ChildNoInitMethod(NoInitMethod):
def __init__(self):
...

0 comments on commit 7b0bc19

Please sign in to comment.