GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add `__class_getitem__` support to `ctypes.py_object` · Issue #132168 · python/cpython

Add __class_getitem__ support to ctypes.py_object #132168

New issue
Copy link
New issue
Copy link
Closed
Closed
Add __class_getitem__ support to ctypes.py_object#132168
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-ctypestopic-typingtype-featureA feature request or enhancementA feature request or enhancement

Description

@brianschubert
brianschubert
opened on Apr 6, 2025
Issue body actions

Feature or enhancement

Proposal:

ctypes.py_object is generic in typeshed, but is not subscriptable at runtime. This can be troublesome for end users, since type checkers will prompt them to supply a generic type argument, but doing so will produce a TypeError at runtime:

>>> import ctypes
>>> ctypes.py_object[int]
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    ctypes.py_object[int]
    ~~~~~~~~~~~~~~~~^^^^^
TypeError: type 'py_object' is not subscriptable

(albeit this is partially mitigated by deferred annotations in 3.14, but TypeError can still pop up if the annotations are ever evaluated)

py_object's typeshed stubs have been stable for a while, and it seems unlikely to be made non-generic in the future. Therefore I propose that __class_getitem__ be added to ctypes.py_object so that it can be subscripted at runtime.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

python/typeshed#13760

Linked PRs

  • gh-132168: Add __class_getitem__ to ctypes.py_object #132169
Reacted by Joren Hammudoglu

Activity

  1. brianschubert
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Apr 6, 2025
  2. tomasr8
    added this to Ctypes issueson Apr 6, 2025
  3. tomasr8
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    topic-ctypes
    topic-typing
    on Apr 6, 2025
  4. picnixz commented on Apr 6, 2025

    @picnixz
    picnixz
    on Apr 6, 2025
    Member
    More actions

    What's the reason for having ctypes.py_object generic actually? what does ctypes.py_object[int] means? does it mean a PyLongObject?

  5. sobolevn commented on Apr 6, 2025

    @sobolevn
    sobolevn
    on Apr 6, 2025
    Member
    More actions

    @picnixz see https://github.com/python/typeshed/blob/2b7019b933ac8339e4ae8f4619a2a746446a8532/stdlib/ctypes/__init__.pyi#L253

    Reacted by Bénédikt Tran
  6. brianschubert commented on Apr 6, 2025

    @brianschubert
    brianschubert
    on Apr 6, 2025
    ContributorAuthor
    More actions

    It represents a pointer to an arbitrary Python object. Having it be generic lets you statically track the type of that object. I haven't written enough ctypes code to personally need it, but I imagine it being generic is useful if you need to access py_object.value, or if you need to type-hint a function whose behavior depends on the type of object that py_object is holding. It being generic also seems to be useful to numpy.

  7. AA-Turner
    added a commit that references this issue on Apr 6, 2025

    gh-132168: Add `__class_getitem__` to `ctypes.py_object` (#132169)

    e247639
  8. picnixz
    closed this as completedon Apr 6, 2025
  9. github-project-automation
    moved this to Done in Ctypes issueson Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-ctypestopic-typingtype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • Ctypes issues
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.