You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ctypes POINTER invalid incompatible type with None #12736
With type checkers (PyCharm's, MyPy) I get the error: mypy_test.py:6: error: Argument 1 to "POINTER" has incompatible type "None"; expected "type[Never]" [arg-type]
Using this simple example:
from ctypes import POINTER
void = None
POINTER(void)
POINTER(None) in ctypes is equivalent to c_void_p and is valid when ran. Minor issue but is technically a compatible type.
Therefore, I think it is reasonable that POINTER(None) returns type[c_void_p].
I recently dived into the ctypes implementation while maintaining a project that needed to be updated to accommodate recent changes made to ctypes. This is where I gained this insight.
Activity
junkmd commentedon Oct 7, 2024
Is this change reasonable?
typeshed/stdlib/_ctypes.pyi
Lines 101 to 102 in dbd0d35
srittau commentedon Oct 8, 2024
I'm no expert for ctypes, but I think
c_void_p(withouttype) is the correct return type:junkmd commentedon Oct 8, 2024
ctypes.POINTERreturns a pointer type for the giventype, based on the private_pointer_type_cachemapping.The private function
_reset_cacheis called when the module is loaded and clears that mapping, assigningNoneas the key andc_void_pas the value.Therefore, I think it is reasonable that
POINTER(None)returnstype[c_void_p].I recently dived into the
ctypesimplementation while maintaining a project that needed to be updated to accommodate recent changes made toctypes. This is where I gained this insight.I'm planning to submit a PR regarding this.
Fixes python#12736
Avasam commentedon Oct 9, 2024
Come to think of it with fresh eyes. Doesn't that confirm the return is indeed the class to be instanciated? (represented by
type[c_void_p])?Let's do the same exercise with a simple type (
int):I'm not sure where the confusion was, maybe I was also too sleepy ^^"
srittau commentedon Oct 10, 2024
You're completely right, of course. No idea why I was confused.