{"id":15574574,"url":"https://github.com/runfalk/typed-ctypes","last_synced_at":"2026-04-16T13:39:22.974Z","repository":{"id":68823739,"uuid":"540495503","full_name":"runfalk/typed-ctypes","owner":"runfalk","description":"Stricter typing support for ctypes.Structure.","archived":false,"fork":false,"pushed_at":"2024-07-04T11:55:34.000Z","size":25,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-28T00:59:28.153Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/runfalk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-23T15:07:20.000Z","updated_at":"2024-07-04T11:55:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"3d12aa96-7208-4553-ba54-83e11f88da19","html_url":"https://github.com/runfalk/typed-ctypes","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/runfalk/typed-ctypes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/runfalk%2Ftyped-ctypes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/runfalk%2Ftyped-ctypes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/runfalk%2Ftyped-ctypes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/runfalk%2Ftyped-ctypes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/runfalk","download_url":"https://codeload.github.com/runfalk/typed-ctypes/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/runfalk%2Ftyped-ctypes/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31888935,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T11:36:10.202Z","status":"ssl_error","status_checked_at":"2026-04-16T11:36:09.652Z","response_time":69,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T18:19:26.652Z","updated_at":"2026-04-16T13:39:22.968Z","avatar_url":"https://github.com/runfalk.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Typed ctypes\n============\nThis project improves type support for Python's\n[ctypes.Structure](https://docs.python.org/3/library/ctypes.html#ctypes.Structure).\nThe standard type definition for this doesn't flag when the user tries to access\na missing property.\n\n```python\nimport ctypes\n\nclass Struct(ctypes.Structure):\n    _fields_ = [(\"field\", ctypes.c_uint32)]\n\ns = Struct()\nprint(s.missing)  # Not caught by type checkers\n```\n\nThis is because the default type definitions rely on\n`__getattr__` and the type checker doesn't know how to interpret the `_fields_`\nproperty. The `ctypes.Structure` type definition[^1] in Python looks something\nlike this:\n\n```python\n# ctypes module (simplified for easier reading)\nclass Structure:\n    def __init__(self, *args: Any, **kw: Any) -\u003e None:\n        ...\n\n    # To a type checker this means that any field can be accessed\n    def __getattr__(self, name: str) -\u003e Any:\n        ...\n\n    def __setattr__(self, name: str, value: Any) -\u003e None:\n        ...\n```\n\nExample\n-------\n```python\nfrom typed_ctypes import CStruct, Uint32, Uint8\n\nclass Struct(CStruct):\n    uint: Uint32\n    char: Uint8\n\ns = Struct()\ns.uint = 1\nprint(s.uint)  # Prints 1\ns.missing  # Throws attribute error, and gets caught by any type checker\n```\n\nctypes support\n--------------\nSupported:\n\n* `ctypes.Structure` through `CStruct`\n* `ctypes.CDLL` through `cdll_from_spec`\n\nUnsupported\n\n* `ctypes.Union`\n* Array\n* Pointers, though structs can be passed to functions.\n\nKnown issues\n------------\nIt's not possible to statically ensure that the attribute types can be\nunderstood by this library. This happens at runtime:\n\n```python\nfrom typed_ctypes import CStruct\n\nclass Struct(CStruct):\n    invalid: int  # Raises TypeError\n```\n\nThis is fine most of the time since you don't generate struct inside of\nfunctions. Therefore you would catch it when you start the application or the\ntest suite.\n\nEnums are a bit awkward to work with. The recommended implementation for now is:\n\n```python\nfrom enum import IntEnum\nfrom typed_ctypes import CStruct, Int32\n\nclass State:\n    Unknown = 0\n    Open = 1\n    Closed = 2\n\n\nclass Struct(CStruct):\n    _state: Int32\n\n    @property\n    def state(self) -\u003e State:\n        return State(self._state)\n\n    @state.setter\n    def state(self, state: State) -\u003e None:\n        self._state = state.value\n```\n\n[^1]: https://github.com/python/typeshed/blob/a702daa6311db14db603827cd649fcfab15f8f53/stdlib/ctypes/__init__.pyi#L248\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frunfalk%2Ftyped-ctypes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frunfalk%2Ftyped-ctypes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frunfalk%2Ftyped-ctypes/lists"}