Skip to content

gh-158418: Fix zero-argument super() in ctypes.util.struct methods - #158419

Open
fedonman wants to merge 1 commit into
python:mainfrom
fedonman:fix-ctypes-struct-super
Open

fedonman wants to merge 1 commit into
python:mainfrom
fedonman:fix-ctypes-struct-super

Conversation

@fedonman

@fedonman fedonman commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Methods of a @ctypes.util.struct class keep a __class__ cell that points at the original class, so zero-argument super() raises TypeError and __class__ names the wrong class. The decorator now rebinds that cell to the class it returns, the same way dataclasses does for slots=True.

No NEWS entry: ctypes.util.struct is new in 3.16 and has not been released.

…methods

@struct builds a new class and copies the decorated class's namespace
into it, so the __class__ cell of the copied methods still pointed at
the discarded class. Rebind that cell to the returned class, as
dataclasses does for slots=True.

@ZeroIntensity ZeroIntensity 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.

This fix looks really fragile and relies on a lot of internals. I don't have any alternative ideas yet, but it would be good to look at how other libraries handle this (for example, what do attrs and pydantic do?).

@johnslavik, I think you might find this one interesting. It reminds me of your NamedTuple work from a while back.

Comment thread Lib/ctypes/util.py
import sys

from dataclasses import dataclass
from dataclasses import dataclass, _update_func_cell_for__class__

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.

I think it's a really bad idea for us to rely on dataclass internals like this.

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.

It is a bad idea.

@bedevere-app

bedevere-app Bot commented Sep 30, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@johnslavik

johnslavik commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

(for example, what do attrs and pydantic do?).

I don't think they have to do anything as they don't replace the original class with a synthetic one. NamedTuple ran into this problem too because both of these utilities create a new class in place of the old one, instead of mutating the original one and returning it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants