Skip to content

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

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

Merged

Conversation

tyralla
Copy link
Collaborator

@tyralla tyralla commented Feb 7, 2025

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.

This comment has been minimized.

@@ -965,6 +965,9 @@ def dataclass_tag_callback(ctx: ClassDefContext) -> None:

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
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
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
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
Copy link
Collaborator Author

tyralla commented Feb 8, 2025

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)."?

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

🚀

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>
Copy link
Contributor

github-actions bot commented Feb 8, 2025

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

Copy link
Member

@sobolevn sobolevn left a comment

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
18 checks passed
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