Skip to content

make the type-parameter of ctypes.py_object optional#13760

Merged
srittau merged 1 commit intopython:mainfrom
jorenham:ctypes.py_object-generic-default
Apr 7, 2025
Merged

make the type-parameter of ctypes.py_object optional#13760
srittau merged 1 commit intopython:mainfrom
jorenham:ctypes.py_object-generic-default

Conversation

@jorenham
Copy link
Contributor

@jorenham jorenham commented Apr 1, 2025

Type-checkers currently require a generic type argument for ctypes.py_object. But at runtime this can raise a TypeError if not quoted.
We can avoid this contradictory paradox that's sending conflicting mixed signals by setting the type-parameter default of ctypes.py_object to Any.

@github-actions

This comment has been minimized.

@brianschubert
Copy link
Collaborator

But at runtime this can raise a TypeError if not quoted.

Which begs the question, should ctypes.py_object define __class_getitem__ at runtime, to mirror the stubs?

@jorenham
Copy link
Contributor Author

jorenham commented Apr 2, 2025

But at runtime this can raise a TypeError if not quoted.

Which begs the question, should ctypes.py_object define __class_getitem__ at runtime, to mirror the stubs?

Maybe? But even if that were to be changed in cpython, then that'd only fix it for 3.14+ or something. So I suppose that this would still be relevant

@brianschubert
Copy link
Collaborator

It seems that there's very few instances of generic py_object being used in the wild, most of which are from numpy/optypes. Is it being generic actually useful? I wonder if it would make sense to specialize it to Any or object

@jorenham
Copy link
Contributor Author

jorenham commented Apr 2, 2025

It seems that there's very few instances of generic py_object being used in the wild, most of which are from numpy/optypes.

Well, I admit that this is mostly self-interest, haha.

Oh and it's optype btw ;)

@jorenham
Copy link
Contributor Author

jorenham commented Apr 2, 2025

Is it being generic actually useful? I wonder if it would make sense to specialize it to Any or object

Not at the moment, no. But once mypy fully supports __new__, then I can make numpy.object_ generic. And then it would help to also have a generic ctypes.py_object.

@jorenham jorenham force-pushed the ctypes.py_object-generic-default branch from d5c4051 to f7e2270 Compare April 4, 2025 02:47
@github-actions
Copy link
Contributor

github-actions bot commented Apr 4, 2025

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

@srittau srittau merged commit 2ae5ed8 into python:main Apr 7, 2025
55 checks passed
@jorenham jorenham deleted the ctypes.py_object-generic-default branch April 7, 2025 14:02
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

3 participants