Skip to content

Commit

Permalink
Fix crash in dataclass brain
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Sep 6, 2022
1 parent 58af36b commit 2ceb3f9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ What's New in astroid 2.12.7?
=============================
Release date: TBA

* Fixed a crash in the ``dataclass`` brain for uninferable bases.

Closes PyCQA/pylint#7418


What's New in astroid 2.12.6?
Expand Down
4 changes: 3 additions & 1 deletion astroid/brain/brain_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ def _generate_dataclass_init(
assignments.append(assignment_str)

try:
base: ClassDef = next(next(iter(node.bases)).infer())
base = next(next(iter(node.bases)).infer())
if not isinstance(base, ClassDef):
raise InferenceError
base_init: FunctionDef | None = base.locals["__init__"][0]
except (StopIteration, InferenceError, KeyError):
base_init = None
Expand Down
23 changes: 23 additions & 0 deletions tests/unittest_brain_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,3 +824,26 @@ class Dee(Cee):
"ee",
]
assert [a.name for a in dee_init.args.kwonlyargs] == []


def test_dataclass_with_unknown_base() -> None:
"""Regression test for dataclasses with unknown base classes.
Reported in https://github.com/PyCQA/pylint/issues/7418
"""
node = astroid.extract_node(
"""
import dataclasses
from unknown import Unknown
@dataclasses.dataclass
class MyDataclass(Unknown):
pass
MyDataclass()
"""
)

assert next(node.infer())

0 comments on commit 2ceb3f9

Please sign in to comment.