Skip to content

Commit

Permalink
Fix crash on datafields
Browse files Browse the repository at this point in the history
Fixes the crash reported at pylint-dev/pylint#4963
Tests added there.
  • Loading branch information
DanielNoord committed Sep 5, 2021
1 parent 46628ba commit c276e18
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
24 changes: 13 additions & 11 deletions astroid/brain/brain_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from astroid.manager import AstroidManager
from astroid.nodes.node_classes import (
AnnAssign,
Assign,
AssignName,
Attribute,
Call,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit c276e18

Please sign in to comment.