Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash in dataclass brain #1768

Merged
merged 1 commit into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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())