pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.32k stars 1.14k forks source link

Pylint Crash when building the AST of a dataclass with a `Any` member field #9888

Closed ancantus closed 2 months ago

ancantus commented 2 months ago

Crash Template

When parsing the following file:

from dataclasses import dataclass
from typing import Any

@dataclass
class TestDataClass:
    element: Any = None

pylint crashed with a AstroidBuildingError and with the following stacktrace:

Traceback (most recent call last):
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/pylint/lint/pylinter.py", line 1044, in get_ast
    return MANAGER.ast_from_file(filepath, modname, source=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/manager.py", line 138, in ast_from_file
    return AstroidBuilder(self).file_build(filepath, modname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/builder.py", line 145, in file_build
    return self._post_build(module, builder, encoding)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/builder.py", line 173, in _post_build
    module = self._manager.visit_transforms(module)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/manager.py", line 109, in visit_transforms
    return self._transform.visit(node)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/transforms.py", line 89, in visit
    return self._visit(module)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/transforms.py", line 54, in _visit
    visited = self._visit_generic(value)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/transforms.py", line 61, in _visit_generic
    return [self._visit_generic(child) for child in node]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/transforms.py", line 67, in _visit_generic
    return self._visit(node)
           ^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/transforms.py", line 57, in _visit
    return self._transform(node)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/transforms.py", line 39, in _transform
    ret = transform_func(node)
          ^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/brain/brain_dataclasses.py", line 66, in dataclass_transform
    for assign_node in _get_dataclass_attributes(node):
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/brain/brain_dataclasses.py", line 126, in _get_dataclass_attributes
    if _is_class_var(assign_node.annotation):  # type: ignore[arg-type]
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/brain/brain_dataclasses.py", line 546, in _is_class_var
    inferred = next(node.infer())
               ^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/nodes/node_ng.py", line 171, in infer
    yield from self._infer(context=context, **kwargs)
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/decorators.py", line 143, in raise_if_nothing_inferred
    yield next(generator)
          ^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/decorators.py", line 112, in wrapped
    for res in _func(node, context, **kwargs):
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/bases.py", line 177, in _infer_stmts
    for inf in stmt.infer(context=context):
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/nodes/node_ng.py", line 184, in infer
    for i, result in enumerate(self._infer(context=context, **kwargs)):
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/decorators.py", line 143, in raise_if_nothing_inferred
    yield next(generator)
          ^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/decorators.py", line 112, in wrapped
    for res in _func(node, context, **kwargs):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/inference.py", line 334, in infer_import_from
    module = self.do_import_module()
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/nodes/_base_nodes.py", line 146, in do_import_module
    return mymodule.import_module(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/nodes/scoped_nodes/scoped_nodes.py", line 530, in import_module
    return AstroidManager().ast_from_module_name(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/manager.py", line 246, in ast_from_module_name
    return self.ast_from_file(found_spec.location, modname, fallback=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/manager.py", line 138, in ast_from_file
    return AstroidBuilder(self).file_build(filepath, modname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/builder.py", line 144, in file_build
    module, builder = self._data_build(data, modname, path)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/builder.py", line 204, in _data_build
    module = builder.visit_module(node, modname, node_file, package)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/rebuilder.py", line 254, in visit_module
    [self.visit(child, newnode) for child in node.body],
     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/astroid/rebuilder.py", line 609, in visit
    visit_method = getattr(self, visit_name)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'TreeRebuilder' object has no attribute 'visit_typealias'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/pylint/lint/pylinter.py", line 730, in _get_asts
    ast_per_fileitem[fileitem] = self.get_ast(
                                 ^^^^^^^^^^^^^
  File "/home/restep/projects/ebl-data-collator/.venv/lib/python3.12/site-packages/pylint/lint/pylinter.py", line 1066, in get_ast
    raise astroid.AstroidBuildingError(
astroid.exceptions.AstroidBuildingError: Building error when trying to create ast representation of module 'test'

Pylint version

OS version

Ubuntu 24.04 LTS

Additional Notes

Issue happens even if the dataclass has non Any fields. Order does not seem to impact the issue.

DanielNoord commented 2 months ago

You're using an older version of pylint. Can you also reproduce with a newer version?

jacobtylerwalls commented 2 months ago

Python 3.12 is only supported on pylint 3+