Skip to content

Fix crash on invalid _fields in named-tuple-like classes - #1259

Merged
sobolevn merged 3 commits into
mainfrom
fix-_fields-crash
Oct 13, 2026
Merged

sobolevn merged 3 commits into
mainfrom
fix-_fields-crash

Conversation

@sobolevn

@sobolevn sobolevn commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

Before this change we used PyTuple_GET_SIZE(fields); without checking fields to be tuple.

I changed how NamedTuple is detected. Now, it must have all the required fields.

Comment thread tests/unit/test_json.py Resolved
Comment thread src/msgspec/_core.c
&& PyType_IsSubtype((PyTypeObject *)t, &PyTuple_Type)
&& PyObject_HasAttr(t, state->mod->str__fields)
);
// There's no `NamedTuple` class, it is not really easy to check.

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.

Really a shame that there's no common base / metaclass for this. Do you know if there's a reason for this, or is it just historical?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This happens to be an implementation detail: when collection.namedtuple was first introduced it was not possible to be used as a baseclass, only as a function call: https://github.com/python/cpython/blob/50082b8d89392b7655eb9a2790731b613cbc620f/Lib/collections/__init__.py#L361-L533

Later it started supporing subclassing and custom methods.
Basically, it is a class maker function.

typing.NamedTuple just allows doing the semantically identical thing: https://github.com/python/cpython/blob/50082b8d89392b7655eb9a2790731b613cbc620f/Lib/typing.py#L3015-L3019

So, nobody ever expected NamedTuple or namedtuple to be a serialization model :(

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.

Mhm. Would be nice to have this. You think it's worth an attempt to contribute this upstream? Checking if something's a NamedTuple would be rather helpful. Even something like datclasses, e.g. a canonical static attribute on the namespace, and a collections.isnamedtuple function

@sobolevn sobolevn Oct 13, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is worth trying, but I doubt it will make it into the source code :)

@sobolevn
sobolevn added this pull request to the merge queue Oct 13, 2026
Merged via the queue into main with commit 76d4402 Oct 13, 2026
24 checks passed
@sobolevn
sobolevn deleted the fix-_fields-crash branch October 13, 2026 12:34
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.

2 participants