Title: Using `ctypes.CField` for annotations of fields within `ctypes.Structure`-like · Issue #10567 · python/typeshed · GitHub
Open Graph Title: Using `ctypes.CField` for annotations of fields within `ctypes.Structure`-like · Issue #10567 · python/typeshed
X Title: Using `ctypes.CField` for annotations of fields within `ctypes.Structure`-like · Issue #10567 · python/typeshed
Description: Currently, attributes of ctypes.Structure, such as ctypes.wintypes.POINT, are annotated with names and types that specified in _fields_. typeshed/stdlib/ctypes/wintypes.pyi Lines 126 to 128 in df08fce class POINT(Structure): x: LONG y: L...
Open Graph Description: Currently, attributes of ctypes.Structure, such as ctypes.wintypes.POINT, are annotated with names and types that specified in _fields_. typeshed/stdlib/ctypes/wintypes.pyi Lines 126 to 128 in df08...
X Description: Currently, attributes of ctypes.Structure, such as ctypes.wintypes.POINT, are annotated with names and types that specified in _fields_. typeshed/stdlib/ctypes/wintypes.pyi Lines 126 to 128 in df08...
Opengraph URL: https://github.com/python/typeshed/issues/10567
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Using `ctypes.CField` for annotations of fields within `ctypes.Structure`-like","articleBody":"Currently, attributes of `ctypes.Structure`, such as `ctypes.wintypes.POINT`, are annotated with names and types that [specified in `_fields_`](https://github.com/python/cpython/blob/ee40b3e20d9b8d62a9b36b777dff42db1e9049d5/Lib/ctypes/wintypes.py#L115-L118).\r\n\r\nhttps://github.com/python/typeshed/blob/df08fcec5f281ba01266869ec55368819aed0a77/stdlib/ctypes/wintypes.pyi#L126-L128\r\n\r\nThe problem with this approach is that at runtime, the attributes return types like `int` or `bytes`, while type checkers interpret them as returning `c_int` or `c_char`.\r\n\r\nBelow is the simplest example of a structure created for explanation purposes.\r\nAt runtime, the types of these fields are `ctypes.CField`. In the example below, `x` is a data descriptor that returns `int` as the getter and accepts both `c_int` and `int` as the setter.\r\n\r\n```py\r\n>>> import ctypes\r\n>>> \r\n>>> class Foo(ctypes.Structure):\r\n... pass\r\n... \r\n>>> Foo._fields_ = [('x', ctypes.c_int)]\r\n>>> Foo.x\r\n\u003cField type=c_long, ofs=0, size=4>\r\n>>> type(Foo.x) \r\n\u003cclass '_ctypes.CField'>\r\n>>> foo = Foo()\r\n>>> foo\r\n\u003c__main__.Foo object at 0x0000023CDB80B8C0>\r\n>>> foo.x\r\n0\r\n>>> foo.x = 3\r\n>>> foo.x\r\n3\r\n>>> foo.x = ctypes.c_int(2)\r\n>>> foo.x\r\n2\r\n```\r\n\r\nHaving stubs with types that deviate from the runtime is not an ideal situation.\r\nTherefore, I propose modifying `CField` as shown below for use in annotating fields.\r\n\r\n```py\r\n_T = TypeVar(\"_T\")\r\n_CT = TypeVar(\"_CT\", bound=_CData)\r\n\r\n\r\nclass CField(Generic[_T, _CT]):\r\n offset: int\r\n size: int\r\n @overload\r\n def __get__(self, instance: None, owner: type[Any]) -> Self: ...\r\n @overload\r\n def __get__(self, instance: Any, owner: type[Any] | None) -> _T: ...\r\n def __set__(self, instance: Any, value: _T | _CT) -> None: ...\r\n\r\n\r\nclass Foo(Structure):\r\n x: ClassVar[CField[int, c_int]]\r\n\r\n\r\n# Foo._fields_ = [('x', c_int)] # required in runtime\r\n\r\n\r\na = Foo.x # CField[int, c_int]\r\nfoo = Foo()\r\nb = foo.x # int\r\nfoo.x = 3 # OK\r\nfoo.x = c_int(2) # OK\r\nfoo.x = 3.14 # NG\r\nfoo.x = c_double(3.14) # NG\r\n```\r\n\r\n## another idea\r\n\r\nI thought it elegant to specify only subclasses of `_SimpleCData[_T]` as type parameters, given the potential for inferring `_T`, as shown below.\r\n\r\n```py\r\nclass Bar(Structure):\r\n x: ClassVar[CField[c_int]] # returns `int`, can take `int` or `c_int`\r\n```\r\n\r\nHowever, current static type system cannot that.\r\n\r\nMoreover, considering that there exist not only subclasses of `_SimpleCData` defined within `ctypes`, but also third-party developers who define `_SimpleCData` subclasses within their own projects.\r\n\r\nThere is the redundancy, but I believe that utilizing the two type parameters would enhance flexibility.","author":{"url":"https://github.com/junkmd","@type":"Person","name":"junkmd"},"datePublished":"2023-08-13T03:40:54Z","url":"https://github.com/python/typeshed/issues/10567"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:bfbe87c2-d017-d50b-f38b-32c7a6bd464e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 83E6:2FD3CB:35D2CA:488822:6B002925 |
| html-safe-nonce | 73eb9777eb7b7429cb223100c1255189daf1720fc3f709da24d57bdb4088f8a1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4M0U2OjJGRDNDQjozNUQyQ0E6NDg4ODIyOjZCMDAyOTI1IiwidmlzaXRvcl9pZCI6IjcxNTk2OTY3MDE2NzcxMTk3ODEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 078ce47318dea44a09e499d66d5fea97b6651bd320db6731439f92c31e11c2f4 |
| hovercard-subject-tag | issue:1848385599 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/typeshed/10567/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f3483f18abb6d1be16c7e89d3e974cb26d84944ae3165aaea9a7e3e7e0db71dd/python/typeshed/issues/10567 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f3483f18abb6d1be16c7e89d3e974cb26d84944ae3165aaea9a7e3e7e0db71dd/python/typeshed/issues/10567 |
| og:image:alt | Currently, attributes of ctypes.Structure, such as ctypes.wintypes.POINT, are annotated with names and types that specified in _fields_. typeshed/stdlib/ctypes/wintypes.pyi Lines 126 to 128 in df08... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | junkmd |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width