René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:bfbe87c2-d017-d50b-f38b-32c7a6bd464e
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id83E6:2FD3CB:35D2CA:488822:6B002925
html-safe-nonce73eb9777eb7b7429cb223100c1255189daf1720fc3f709da24d57bdb4088f8a1
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4M0U2OjJGRDNDQjozNUQyQ0E6NDg4ODIyOjZCMDAyOTI1IiwidmlzaXRvcl9pZCI6IjcxNTk2OTY3MDE2NzcxMTk3ODEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac078ce47318dea44a09e499d66d5fea97b6651bd320db6731439f92c31e11c2f4
hovercard-subject-tagissue:1848385599
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/typeshed/10567/issue_layout
twitter:imagehttps://opengraph.githubassets.com/f3483f18abb6d1be16c7e89d3e974cb26d84944ae3165aaea9a7e3e7e0db71dd/python/typeshed/issues/10567
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/f3483f18abb6d1be16c7e89d3e974cb26d84944ae3165aaea9a7e3e7e0db71dd/python/typeshed/issues/10567
og:image:altCurrently, 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamejunkmd
hostnamegithub.com
expected-hostnamegithub.com
None4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0
turbo-cache-controlno-preview
go-importgithub.com/python/typeshed git https://github.com/python/typeshed.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id31696383
octolytics-dimension-repository_nwopython/typeshed
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id31696383
octolytics-dimension-repository_network_root_nwopython/typeshed
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release44c658b24abf00491fb7ae5f56d42c9f38cbeab2
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/typeshed/issues/10567#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftypeshed%2Fissues%2F10567
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftypeshed%2Fissues%2F10567
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Ftypeshed
Reloadhttps://github.com/python/typeshed/issues/10567
Reloadhttps://github.com/python/typeshed/issues/10567
Reloadhttps://github.com/python/typeshed/issues/10567
pythonhttps://github.com/python
typeshedhttps://github.com/python/typeshed
Codehttps://github.com/python/typeshed
Issues196 (196)https://github.com/python/typeshed/issues
Pull requests251 (251)https://github.com/python/typeshed/pulls
Actionshttps://github.com/python/typeshed/actions
Security and qualityhttps://github.com/python/typeshed/security
Insightshttps://github.com/python/typeshed/insights
https://github.com/python/typeshed
New issuehttps://github.com/login?return_to=https://github.com/python/typeshed/issues/10567
New issuehttps://github.com/login?return_to=https://github.com/python/typeshed/issues/10567
Using ctypes.CField for annotations of fields within ctypes.Structure-likehttps://github.com/python/typeshed/issues/10567#top
https://github.com/junkmd
junkmdhttps://github.com/junkmd
on Aug 13, 2023https://github.com/python/typeshed/issues/10567#issue-1848385599
specified in _fields_https://github.com/python/cpython/blob/ee40b3e20d9b8d62a9b36b777dff42db1e9049d5/Lib/ctypes/wintypes.py#L115-L118
typeshed/stdlib/ctypes/wintypes.pyihttps://github.com/python/typeshed/blob/df08fcec5f281ba01266869ec55368819aed0a77/stdlib/ctypes/wintypes.pyi#L126-L128
df08fcehttps://github.com/python/typeshed/commit/df08fcec5f281ba01266869ec55368819aed0a77
https://github.com/junkmd
junkmdhttps://github.com/junkmd
on Aug 13, 2023https://github.com/python/typeshed/issues/10567#issuecomment-1676211683
python/cpython#104533https://github.com/python/cpython/issues/104533
https://discuss.python.org/t/annotation-based-sugar-for-ctypes/26579/https://discuss.python.org/t/annotation-based-sugar-for-ctypes/26579/
https://github.com/srittau
srittauhttps://github.com/srittau
on Aug 14, 2023https://github.com/python/typeshed/issues/10567#issuecomment-1677361944
https://github.com/junkmd
junkmdhttps://github.com/junkmd
on Aug 16, 2023https://github.com/python/typeshed/issues/10567#issuecomment-1680549523
https://github.com/junkmd
junkmdhttps://github.com/junkmd
on Aug 16, 2023https://github.com/python/typeshed/issues/10567#issuecomment-1680551830
https://github.com/junkmd
junkmdhttps://github.com/junkmd
on Aug 17, 2023https://github.com/python/typeshed/issues/10567#issuecomment-1682281845
#10595https://github.com/python/typeshed/pull/10595
https://github.com/junkmd
junkmdhttps://github.com/junkmd
on Sep 1, 2023https://github.com/python/typeshed/issues/10567#issuecomment-1703342826
#10595https://github.com/python/typeshed/pull/10595
junkmdhttps://github.com/junkmd
completedhttps://github.com/python/typeshed/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Sep 1, 2023https://github.com/python/typeshed/issues/10567#event-10261116054
https://github.com/einarwar
einarwarhttps://github.com/einarwar
on Sep 13, 2023https://github.com/python/typeshed/issues/10567#issuecomment-1717109273
microsoft/pyright#5932 (comment)https://github.com/microsoft/pyright/issues/5932#event-10349431970
https://github.com/junkmd
junkmdhttps://github.com/junkmd
on Sep 13, 2023https://github.com/python/typeshed/issues/10567#issuecomment-1717435105
https://github.com/junkmd
junkmdhttps://github.com/junkmd
on Oct 7, 2023https://github.com/python/typeshed/issues/10567#issuecomment-1751733109
#10777https://github.com/python/typeshed/issues/10777
#10795https://github.com/python/typeshed/pull/10795
microsoft/pyright#5932 (comment)https://github.com/microsoft/pyright/issues/5932#issuecomment-1740053569
microsoft/pyright#5932 (comment)https://github.com/microsoft/pyright/issues/5932#issuecomment-1746038353
https://github.com/microsoft/pyright/releases/tag/1.1.330https://github.com/microsoft/pyright/releases/tag/1.1.330
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/typeshed/issues/10567
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/typeshed/issues/10567
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.