Skip to content

super() raises TypeError in a ctypes.util.struct method #158418

Closed

Description

@fedonman

Bug description:

Zero-argument super() raises TypeError in a method of a class decorated with ctypes.util.struct:

from ctypes import c_int
from ctypes.util import struct

@struct
class Point:
    x: c_int
    y: c_int
    def __init__(self, x=0, y=0):
        super().__init__(x, y)

print(Point(1, 2).x)
TypeError: super(type, obj): obj (instance of Point) is not an instance or subtype of type (Point).

The same method on a plain Structure subclass works. __class__ in such a method also refers to the original class instead of the one the decorator returns.

Expected: super() and __class__ refer to the class returned by @struct, and the example prints 1.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Sep 29, 2026
  2. ZeroIntensity commented on Sep 30, 2026

    @ZeroIntensity
    Member

    Agreed that we should probably deal with this in one way or another, but the attached PR adds a lot of extra complexity. Do we have any data on how often people use super() with ctypes.Structure subclasses? It might be okay to just document that this doesn't work.

  3. johnslavik commented on Sep 30, 2026

    @johnslavik
    Member

    Do we have any data on how often people use super() with ctypes.Structure subclasses?

    +1 on the question. With NamedTuple it simply didn't make sense to fix it.

    The tests provided in the PR do not exercise inheritance at all. Do we really need super() to work in those classes? (And if we decide we don't, I'm all for improving the error message)

  4. dqqFA8995 commented on Sep 30, 2026

    @dqqFA8995
  5. sergey-miryanov commented on Sep 30, 2026

    @sergey-miryanov
  6. fedonman commented on Sep 30, 2026

    @fedonman
    ContributorAuthor

    I agree that importing a private helper from dataclasses isn't ideal. We don't have usage data for @struct yet, since it hasn't been released. I was thinking of a custom __init__ that calls Structure.__init__, which already works with either super(Point, self).__init__(...) or Structure.__init__(self, ...). Subclasses of the decorated class aren't affected either.

    Unlike NamedTuple, the decorator runs after __classcell__ has been consumed. To raise a useful error, we'd have to check each method's __code__.co_freevars, including methods wrapped in classmethods and properties. That's much of the same traversal the PR already does.

    I'd suggest documenting the limitation in the struct entry and showing the two-argument super() workaround. I can update the PR to do that if you're happy with this approach.

  7. ZeroIntensity commented on Sep 30, 2026

    @ZeroIntensity
    Member

    I did find a number of examples of people using super() in ctypes.Structure subclasses:

    1. hite4044/WinEnchantKit
    2. qilingframework/qiling
    3. ionite34/einspect
    4. shawwn/libtpu-python
    5. Spexophis/minimiao
    6. linfeiliu/PyDA30L
    7. NUAACTF2022-Challenges
    8. Spexophis/Microscope_GUI

    They all seem to be from people instinctively calling super().__init__() with subclasses. I'm not worried about that with the @struct decorator because the inheritance is hidden, so people won't be inclined to call super().__init__(), so I'm okay with just documenting that no-arg super() doesn't work with @struct.

  8. johnslavik commented on Oct 1, 2026

    @johnslavik
    Member

    They all seem to be from people instinctively calling super().__init__() with subclasses. I'm not worried about that with the @struct decorator because the inheritance is hidden, so people won't be inclined to call super().__init__(), so I'm okay with just documenting that no-arg super() doesn't work with @struct.

    +1 (despite the docs warranting a base)

  9. added a commit that references this issue on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.16new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-ctypestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions