python / python/mypy

Assertion failure crash: `assert isinstance(node, Var), node` when dataclass has namedtuple() field

Open
#21,583 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

crash
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Crash Report

mypy 2.1.0 and master (cbd8c82b74) crash on the fuzzed snippet below.

Traceback

Traceback (most recent call last):
  File ".venv-mypy/bin/mypy", line 6, in <module>
    sys.exit(console_entry())
  File "mypy/mypy/__main__.py", line 16, in console_entry
    main()
  File "mypy/mypy/main.py", line 154, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "mypy/mypy/main.py", line 244, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "mypy/mypy/build.py", line 422, in build
    result = build_inner(
  File "mypy/mypy/build.py", line 537, in build_inner
    graph = dispatch(sources, manager, stdout, connect_threads)
  File "mypy/mypy/build.py", line 4134, in dispatch
    process_graph(graph, manager)
  File "mypy/mypy/build.py", line 4602, in process_graph
    done, still_working, results = manager.wait_for_done(graph)
  File "mypy/mypy/build.py", line 1478, in wait_for_done
    process_stale_scc(graph, next_scc, self)
  File "mypy/mypy/build.py", line 4769, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "mypy/mypy/semanal_main.py", line 97, in semantic_analysis_for_scc
    apply_class_plugin_hooks(graph, scc, errors)
  File "mypy/mypy/semanal_main.py", line 466, in apply_class_plugin_hooks
    with state.wrap_context():
  File "/usr/lib/python3.14/contextlib.py", line 162, in __exit__
    self.gen.throw(value)
  File "mypy/mypy/build.py", line 3063, in wrap_context
    yield
  File "mypy/mypy/semanal_main.py", line 469, in apply_class_plugin_hooks
    if not apply_hooks_to_class(
           ~~~~~~~~~~~~~~~~~~~~^
        state.manager.semantic_analyzer,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<4 lines>...
        errors,
        ^^^^^^^
    ):
    ^
  File "mypy/mypy/semanal_main.py", line 505, in apply_hooks_to_class
    ok = ok and hook(ClassDefContext(defn, decorator, self))
                ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "mypy/mypy/plugins/dataclasses.py", line 970, in dataclass_class_maker_callback
    return transformer.transform()
           ~~~~~~~~~~~~~~~~~~~~~^^
  File "mypy/mypy/plugins/dataclasses.py", line 237, in transform
    attributes = self.collect_attributes()
  File "mypy/mypy/plugins/dataclasses.py", line 602, in collect_attributes
    assert isinstance(node, Var), node
           ~~~~~~~~~~^^^^^^^^^^^
AssertionError: TypeInfo(
  Name(crash.C.p)
  Bases(tuple[Any, ...])
  Mro(crash.C.p, builtins.tuple, typing.Sequence, typing.Reversible, typing.Collection, typing.Iterable, typing.Container, builtins.object)
  Names(
    _NT
    __annotations__ (dict[str, Any])
    __doc__ (str)
    __match_args__ (tuple[Literal['x'], Literal['y']])
    __new__
    __replace__
    _asdict
    _field_defaults (dict[str, Any])
    _field_types (dict[str, Any])
    _fields (tuple[str, str])
    _make
    _replace
    _source (str)
    x (Any)
    y (Any)))

To Reproduce

from collections import namedtuple
from dataclasses import dataclass

@dataclass
class C:
    p: 'Point' = namedtuple('Point', ['x', 'y'])

Your Environment

  • Mypy version used: 2.1.0 and cbd8c82b74
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.14.5
  • Operating system and version: Arch Linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with mypy/plugins/dataclasses.py, especially dataclass_class_maker_callback, transform, and collect_attributes, then run mypy on the namedtuple/dataclass reproduction. Trace why the namedtuple field produces a TypeInfo where collect_attributes expects a Var; done means the reproducer no longer crashes and the regression is covered by the relevant dataclass plugin tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.