python / python/mypy

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

Offen
#21,583 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

crash
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit mypy/plugins/dataclasses.py, insbesondere mit dataclass_class_maker_callback, transform und collect_attributes, und führe dann mypy für die benannte namedtuple/dataclass-Reproduktion aus. Verfolge, warum das namedtuple-Feld einen TypeInfo erzeugt, obwohl collect_attributes einen Var erwartet; abgeschlossen ist die Aufgabe, wenn der Reproducer nicht mehr abstürzt und die Regression durch die relevanten Dataclass-Plugin-Tests abgedeckt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
62/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.