Skip to content

Fatal error with dataclass after 2.11 #5030

Closed
pylint-dev/astroid
#1181
@friggog

Description

@friggog

Bug description

After 2.11 dataclass with dict causes fatal error.

Example code:

"""Example of fatal pylint error with dataclass."""
from typing import Dict, List, Any
from dataclasses import dataclass, field


@dataclass
class SomeData:
    """A dataclass."""
    a_dict: Dict[str, List[str]] = field(default_factory=dict)


@dataclass
class SubSomeData(SomeData):
    """A subclass of a dataclass."""

    def __init__(self, **kwargs: Dict[str, Any]) -> None:
        """Subclass init func."""
        super().__init__(**kwargs)
        if "test" in self.a_dict:
            print(self.a_dict["test"])

Configuration

No response

Command used

pylint pylint_test.py

Pylint output

Exception on node <If l.19 at 0x206550d06c8> in file '..\pylint_test.py'
Traceback (most recent call last):
  File "...\lib\site-packages\pylint\utils\ast_walker.py", line 75, in walk
    callback(astroid)
  File "...\lib\site-packages\pylint\checkers\base.py", line 1104, in visit_if
    self._check_using_constant_test(node, node.test)
  File "...\lib\site-packages\pylint\checkers\base.py", line 1142, in _check_using_constant_test
    inferred = utils.safe_infer(test)
  File "...\lib\site-packages\pylint\checkers\utils.py", line 1181, in safe_infer
    value = next(infer_gen)
  File "...\lib\site-packages\astroid\nodes\node_ng.py", line 105, in infer
    yield from self._infer(context, **kwargs)
  File "...\lib\site-packages\astroid\inference.py", line 872, in _infer_compare
    retval = _do_compare(lhs, op, rhs)
  File "...\lib\site-packages\astroid\inference.py", line 842, in _do_compare
    left, right = _to_literal(left), _to_literal(right)
  File "...\lib\site-packages\astroid\inference.py", line 814, in _to_literal
    return ast.literal_eval(node.as_string())
  File "...\lib\site-packages\astroid\nodes\node_ng.py", line 547, in as_string
    return AsStringVisitor()(self)
  File "...\lib\site-packages\astroid\nodes\as_string.py", line 55, in __call__
    return node.accept(self).replace(DOC_NEWLINE, "\n")
  File "...\lib\site-packages\astroid\nodes\node_ng.py", line 186, in accept
    return func(self)
  File "...\lib\site-packages\astroid\nodes\as_string.py", line 185, in visit_classdef
    args += [n.accept(self) for n in node.keywords]
TypeError: 'NoneType' object is not iterable
************* Module pylint_test
pylint_test.py:1:0: F0001: Fatal error while checking 'pylint_test.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in 'C:\Users\chewitt\AppData\Local\pylint\pylint\Cache\pylint-crash-2021-09-17-09.txt'. (fatal)

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

Expected behavior

As on 2.10:

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

Pylint version

2.11.*

astroid==2.8.0

OS / Environment

Windows 10

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    AstroidRelated to astroidCrash 💥A bug that makes pylint crashNeeds astroid updateNeeds an astroid update (probably a release too) before being mergableRegression

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions