Skip to content

Conversation

@jorenham
Copy link
Contributor

@jorenham jorenham commented Apr 2, 2025

This adds the _type_ class attributes to the relevant types of ctypes. It makes the individual c_* types structurally distinguishable. For the integer and floating C types, the C99 standard is assumed: https://en.cppreference.com/w/c/language/arithmetic_types#Integer_types as reference

But why?

A Protocol can now be used to e.g. only accept C types could have _type_ == "l", i.e. signed integers of at least 32 bits on 32- and 64-bits systems. This will match c_int, c_long, c_longlong, c_int32, c_int64, c_time_t, c_ssize_t, and on windows also HRESULT. The alternative would be to use a platform-specific union of these 7 or 8 types, which would be rather messy and verbose.

Also, because stubs are often used as form of documentation (or maybe that's just me), this will also help in that sense.

@jorenham
Copy link
Contributor Author

jorenham commented Apr 2, 2025

Another advantage: stubtest is now able to recognize that BYTE = c_byte in ctypes.wintypes is incorrect :)

@github-actions

This comment has been minimized.

@jorenham jorenham force-pushed the ctypes._._type_ branch 2 times, most recently from aea1c91 to 2d8ddcb Compare April 2, 2025 21:13
@jorenham jorenham changed the title _type_ class attributes in ctypes _type_ class attributes in ctypes, and fix ctypes.wintypes.BYTE Apr 2, 2025
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

There's a merge conflict now, but apart from that LGTM – for my limited ctypes experience.

@jorenham
Copy link
Contributor Author

jorenham commented Apr 3, 2025

but apart from that LGTM – for my limited ctypes experience.

@seberg you're pretty C-savvy; does this look right to you?

@github-actions

This comment has been minimized.

@brianschubert
Copy link
Collaborator

Would it make sense for these to be Final?

@jorenham
Copy link
Contributor Author

jorenham commented Apr 3, 2025

Would it make sense for these to be Final?

Good question. I guess that partially depends on whether mixed ClassVar and Final generally supported by the major type-checkers yet.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 7, 2025

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau merged commit 6b8aebc into python:main Apr 7, 2025
55 checks passed
@jorenham jorenham deleted the ctypes._._type_ branch April 7, 2025 14:06
mmingyu pushed a commit to mmingyu/typeshed that referenced this pull request May 16, 2025
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.

3 participants