Skip to content

Commit 3316637

Browse files
authored
Fix crash on a dataclass base that annotates __init__ (#3206)
A class body can bind `__init__` to a name instead of a function by annotating it as a dataclass field, as in `__init__: int`. astroid then skips generating an `__init__` for that class, so its locals entry stays an AssignName. _find_arguments_from_base_classes read `.args` off every dataclass base's `__init__`, so a subclass of such a dataclass crashed the transform with AttributeError: 'AssignName' object has no attribute 'args'. Skip a base whose `__init__` is not a FunctionDef; it contributes no arguments to the generated `__init__`. Closes #3200
1 parent 8577346 commit 3316637

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎ChangeLog‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ Release date: TBA
4141

4242
Closes #3191
4343

44+
* Fix a crash when a dataclass inherits from a dataclass that annotates
45+
``__init__`` as a field, as in ``__init__: int``. Such a base binds
46+
``__init__`` to a name instead of a function, so it no longer contributes
47+
arguments to the generated ``__init__``.
48+
49+
Closes #3200
50+
4451
* The documentation build now fails on any Sphinx warning, so a cross-reference
4552
that does not resolve is caught by CI instead of quietly rendering as plain
4653
text. ``Uninferable``, ``Position`` and the ``BadOperationMessage`` classes

‎astroid/brain/brain_dataclasses.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,16 @@ def _find_arguments_from_base_classes(
188188
if not base.is_dataclass:
189189
continue
190190
try:
191-
base_init: nodes.FunctionDef = base.locals["__init__"][0]
191+
base_init = base.locals["__init__"][0]
192192
except KeyError:
193193
continue
194194

195+
# A base can bind "__init__" to something that is not a function, for
196+
# example by annotating it as a field: "__init__: int". There are no
197+
# arguments to inherit from such a base.
198+
if not isinstance(base_init, nodes.FunctionDef):
199+
continue
200+
195201
pos_only, kw_only = base_init.args._get_arguments_data()
196202
for posarg, data in pos_only.items():
197203
# if data[1] is None:

‎tests/brain/test_dataclasses.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,3 +1359,29 @@ class ChildConfig(BaseConfig[T]):
13591359
# Should not raise DuplicateBasesError in _get_previous_field_default
13601360
inferred = next(node.infer())
13611361
assert inferred is not None
1362+
1363+
1364+
def test_dataclass_base_with_annotated_init_no_crash():
1365+
"""Regression test for https://github.com/pylint-dev/astroid/issues/3200.
1366+
1367+
A base dataclass can bind ``__init__`` to something that is not a function
1368+
by annotating it as a field. Collecting the arguments of such a base should
1369+
not crash with an AttributeError.
1370+
"""
1371+
node = astroid.extract_node("""
1372+
from dataclasses import dataclass
1373+
1374+
@dataclass
1375+
class A:
1376+
__init__: int
1377+
1378+
@dataclass
1379+
class B(A):
1380+
pass
1381+
1382+
B.__init__ #@
1383+
""")
1384+
1385+
inferred = next(node.infer())
1386+
assert isinstance(inferred, bases.UnboundMethod)
1387+
assert [a.name for a in inferred.args.args] == ["self"]

0 commit comments

Comments
 (0)