Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
smheidrich opened this issue Apr 27, 2025 · 1 comment
Closed

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

smheidrich opened this issue Apr 27, 2025 · 1 comment
Labels

Comments

@smheidrich
Copy link

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
@smheidrich smheidrich changed the title Combining @dataclass and NamedTuple leads to Mypy crash in Python 3.13+ Combining @dataclass and NamedTuple leads to crash in Python 3.13+ Apr 27, 2025
@smheidrich
Copy link
Author

Never mind, this was already reported in #18527 and has gotten fixed between 1.15.0 and the current master, so I guess it's just a matter of waiting for the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant