From c276e185f86c4c8c27267771fd75dd16c96b037c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Sun, 5 Sep 2021 11:40:38 +0200 Subject: [PATCH] Fix crash on datafields Fixes the crash reported at PyCQA/pylint#4963 Tests added there. --- ChangeLog | 3 +++ astroid/brain/brain_dataclasses.py | 24 +++++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4fe21c5f13..4a0f5fb470 100644 --- a/ChangeLog +++ b/ChangeLog @@ -20,6 +20,9 @@ What's New in astroid 2.7.4? ============================ Release date: TBA +* Fixed bug in inference of dataclass field calls. + + Closes PyCQA/pylint#4963 What's New in astroid 2.7.3? diff --git a/astroid/brain/brain_dataclasses.py b/astroid/brain/brain_dataclasses.py index e010a51494..e76cdc44b8 100644 --- a/astroid/brain/brain_dataclasses.py +++ b/astroid/brain/brain_dataclasses.py @@ -16,6 +16,7 @@ from astroid.manager import AstroidManager from astroid.nodes.node_classes import ( AnnAssign, + Assign, AssignName, Attribute, Call, @@ -231,19 +232,20 @@ def infer_dataclass_attribute( def infer_dataclass_field_call( - node: AssignName, ctx: context.InferenceContext = None + node: Call, ctx: context.InferenceContext = None ) -> Generator: """Inference tip for dataclass field calls.""" - field_call = node.parent.value - default_type, default = _get_field_default(field_call) - if not default_type: - yield Uninferable - elif default_type == "default": - yield from default.infer(context=ctx) - else: - new_call = parse(default.as_string()).body[0].value - new_call.parent = field_call.parent - yield from new_call.infer(context=ctx) + if isinstance(node.parent, (AnnAssign, Assign)): + field_call = node.parent.value + default_type, default = _get_field_default(field_call) + if not default_type: + yield Uninferable + elif default_type == "default": + yield from default.infer(context=ctx) + else: + new_call = parse(default.as_string()).body[0].value + new_call.parent = field_call.parent + yield from new_call.infer(context=ctx) def _looks_like_dataclass_decorator(