You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Annotation-based syntax for ctypes structs #104533
The overwhelming support present on this issue, along with the repeated requests from users, has convinced me that this is worth the effort. I've put up an implementation at #153781, which I'll polish and/or merge during the EuroPython sprints this weekend.
Yes, but let's do that in a new issue. I'm uncomfortable with making CDLL/PyDLL callable like in your example, but a decorator that simply accepts a DLL should be fine:
Did you mean the ellipsis literally? It’s actually a good idea to require it and have the decorator wrapper verify that the function body consists of an ellipsis as a signal to the reader that the function body is not there.
I did. Maybe pass should be the documented option? I'm not a big fan of actually trying to validate that the function has no body, because that would involve weird bytecode or AST introspection, which would significantly complicate the implementation.
@ZeroIntensity, a question about ctypes.util.struct: what should it do with a class that subclasses ctypes.Union? Right now it drops the base and builds a Structure, so the fields no longer overlap:
The docs only promise structure types, so this might be intended. If it isn't, it could either raise TypeError or build a Union. I'm happy to open an issue and a PR for whichever you'd prefer.
Activity
ZeroIntensity commentedon Jul 15, 2026
The overwhelming support present on this issue, along with the repeated requests from users, has convinced me that this is worth the effort. I've put up an implementation at #153781, which I'll polish and/or merge during the EuroPython sprints this weekend.
gh-104533: Add a dataclass-like decorator for `ctypes` structures (GH…
orent commentedon Jul 18, 2026
The original discussion at https://discuss.python.org/t/annotation-based-sugar-for-ctypes/26579
Also had a proposal for sugaring the syntax of functions, too:
This:
into this:
Any interest in pursuing this, too?
pythongh-104533: Add a dataclass-like decorator for `ctypes` structur…
ZeroIntensity commentedon Jul 18, 2026
Yes, but let's do that in a new issue. I'm uncomfortable with making
CDLL/PyDLLcallable like in your example, but a decorator that simply accepts a DLL should be fine:orent commentedon Jul 18, 2026
Did you mean the ellipsis literally? It’s actually a good idea to require it and have the decorator wrapper verify that the function body consists of an ellipsis as a signal to the reader that the function body is not there.
ZeroIntensity commentedon Jul 18, 2026
I did. Maybe
passshould be the documented option? I'm not a big fan of actually trying to validate that the function has no body, because that would involve weird bytecode or AST introspection, which would significantly complicate the implementation.pythongh-104533: Fix @ctypes.util.struct with endian
pythongh-104533: Fix @ctypes.util.struct with string annotations
gh-104533: Fix `@ctypes.util.struct` with string annotations (#154040)
gh-104533: Fix `@ctypes.util.struct` with endian (#154038)
gh-104533: Update test_ctypes.test_structures (#154091)
gh-104533: Use `@ctypes.util.struct` decorator (#154031)
gh-104533: Fix ctypes test_pointer_proto_missing_argtypes_error() (#1…
fedonman commentedon Sep 23, 2026
@ZeroIntensity, a question about
ctypes.util.struct: what should it do with a class that subclassesctypes.Union? Right now it drops the base and builds aStructure, so the fields no longer overlap:The docs only promise structure types, so this might be intended. If it isn't, it could either raise
TypeErroror build aUnion. I'm happy to open an issue and a PR for whichever you'd prefer.ZeroIntensity commentedon Sep 23, 2026
I think it should raise a
TypeError. We may want to add a@ctypes.util.uniondecorator, but I'll handle that separately.