Skip to content

Combining @dataclass and NamedTuple leads to crash in Python 3.13+ #18982

Closed as duplicate of#18527
@smheidrich

Description

@smheidrich

Crash Report

When being told to typecheck a module that contains a class which is both derived from NamedTuple and decorated with @dataclass (probably by mistake, but no matter), Mypy installed in a Python 3.13+ environment will crash.

It seems to work fine for Python < 3.13.

Traceback

Traceback (most recent call last):
  File ".../bin/mypy", line 12, in <module>
    sys.exit(console_entry())
             ~~~~~~~~~~~~~^^
  File ".../mypy/__main__.py", line 15, in console_entry
    main()
    ~~~~^^
  File "mypy/main.py", line 119, in main
  File "mypy/main.py", line 203, in run_build
  File "mypy/build.py", line 191, in build
  File "mypy/build.py", line 267, in _build
  File "mypy/build.py", line 2937, in dispatch
  File "mypy/build.py", line 3335, in process_graph
  File "mypy/build.py", line 3430, in process_stale_scc
  File "mypy/semanal_main.py", line 99, in semantic_analysis_for_scc
  File "mypy/semanal_main.py", line 443, in apply_class_plugin_hooks
  File "mypy/semanal_main.py", line 479, in apply_hooks_to_class
  File "mypy/plugins/dataclasses.py", line 969, in dataclass_class_maker_callback
  File "mypy/plugins/dataclasses.py", line 392, in transform
  File "mypy/plugins/dataclasses.py", line 412, in _add_dunder_replace
  File "mypy/plugins/common.py", line 238, in add_method_to_class
  File "mypy/plugins/common.py", line 314, in _prepare_class_namespace
ValueError: list.remove(x): x not in list

To Reproduce

Create a file named bug.py with these contents:

from typing import NamedTuple
from dataclasses import dataclass

@dataclass
class A(NamedTuple):
  a: int
  b: str

Run Mypy installed in a Python 3.13 environment on this file, e.g. using uvx:

uvx --python 3.13 --reinstall mypy --show-traceback bug.py

Replacing 3.13 with 3.12 or earlier, you can check that it worked fine for earlier Python versions.

Your Environment

  • Mypy version used: 1.15.0 (compiled: yes)
  • Mypy command-line flags: None needed
  • Mypy configuration options from mypy.ini (and other config files): None needed
  • Python version used: 3.13+ required to reproduce this
  • Operating system and version: Debian Linux 12 (bookworm), but also happened on another machine running some version of Ubuntu

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions