Skip to content

Report that NamedTuple and dataclass are incompatile instead of crashing.#18633

Merged
sobolevn merged 5 commits into
python:masterfrom
tyralla:fix/dataclass_incompatible_with_named_tuple
Feb 8, 2025
Merged

Report that NamedTuple and dataclass are incompatile instead of crashing.#18633
sobolevn merged 5 commits into
python:masterfrom
tyralla:fix/dataclass_incompatible_with_named_tuple

Conversation

@tyralla

@tyralla tyralla commented Feb 7, 2025

Copy link
Copy Markdown
Collaborator

Fixes #18527

The fix is pretty simple. I could not find a situation where combining NamedTuple and dataclass makes sense, so emitting an error and just not applying the dataclass transformations seems sensible.

tyralla and others added 2 commits February 7, 2025 20:52
…rashing.

The fix is pretty simple.  I could not find a situation where combining `NamedTuple` and `dataclass` makes sense, so emitting an error seems sensible.
@github-actions

This comment has been minimized.


def dataclass_class_maker_callback(ctx: ClassDefContext) -> bool:
"""Hooks into the class typechecking process to add support for dataclasses."""
if any(i.is_named_tuple for i in ctx.cls.info.mro):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we are at it: what about TypedDict?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dataclass is also not really compatible with TypedDict, but here, things work a little differently, and Mypy's understanding seems to be consistent with Python's runtime behaviour:

from dataclasses import dataclass
from typing import TypedDict

@dataclass
class A(TypedDict):
    i: int

a = A(i=1)
assert a["i"] == 1
a.i
# Mypy -> error: "A" has not attribute "i" [attr-defined]
# Python -> AttributeError: 'dict' object has no attribute 'i'

@sterliakov

Copy link
Copy Markdown
Collaborator

First I was excited when I saw that issue: "wow, we can make a dataclass with convenient unpacking, nice!". However, it really doesn't work at runtime:

from dataclasses import dataclass
from typing import NamedTuple

@dataclass
class Foo(NamedTuple):
    x: int
    y: str

Foo(0, '')

results in

Traceback (most recent call last):
  File "/tmp/a.py", line 9, in <module>
    Foo(0, '')
  File "<string>", line 3, in __init__
AttributeError: can't set attribute

So this is probably the most reasonable fix. Can we expand the message or add some note about runtime incompatibility? It really isn't immediately obvious why not use that combination.

@tyralla

tyralla commented Feb 8, 2025

Copy link
Copy Markdown
Collaborator Author

So this is probably the most reasonable fix. Can we expand the message or add some note about runtime incompatibility? It really isn't immediately obvious why not use that combination.

Maybe "A NamedTuple cannot be a dataclass (AttributeError during initialization)."?

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

Comment thread mypy/plugins/dataclasses.py Outdated
Comment thread test-data/unit/check-dataclasses.test Outdated
Comment thread test-data/unit/check-dataclasses.test Outdated
tyralla and others added 3 commits February 8, 2025 15:48
Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: sobolevn <mail@sobolevn.me>
@github-actions

github-actions Bot commented Feb 8, 2025

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@sobolevn
sobolevn merged commit a8c2345 into python:master Feb 8, 2025
x612skm pushed a commit to x612skm/mypy-dev that referenced this pull request Feb 24, 2025
…rashing. (python#18633)

Fixes python#18527

The fix is pretty simple. I could not find a situation where combining
`NamedTuple` and `dataclass` makes sense, so emitting an error and just
not applying the dataclass transformations seems sensible.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: sobolevn <mail@sobolevn.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash on namedtuple + @dataclass

3 participants