
Hey everyone. Quick sanity check: The ctypes docs <https://docs.python.org/3.10/library/ctypes.html#ctypes._CData> refer to _CData as a non-public class which is in the module, but _ctypes.c doesn't actually export it <https://github.com/python/cpython/blob/main/Modules/_ctypes/_ctypes.c#L5680>. (I discovered this because it turns out that typeshed *is* referencing _CData, e.g. in its type annotations for RawIOBase <https://github.com/python/typeshed/blob/master/stdlib/_typeshed/__init__.pyi...> ) Is this intended behavior in CPython (in which case the docs are a bit off and typeshed has a bug), or is it unexpected to people on this list (in which case it's an issue in _ctypes.c)? Yonatan

On Wed, Jan 5, 2022 at 3:17 PM Yonatan Zunger <zunger@gmail.com> wrote:
typeshed is presumably referring to itself. It defines an interface for ctypes._CData in https://github.com/python/typeshed/blob/master/stdlib/ctypes/__init__.pyi#L8... The CPython ctypes docs *seem* reasonable to me. There is such a class. It is not public, so you cannot access ctypes._CData in any direct manner. That it gets called a class may be somewhat historical - its purpose is to provide a common interface. What code would ever actually care that it used class mechanisms as an internal implementation detail to do that? -gps

I think it makes good sense for the type-checking reason: _CData *does* declare a fairly useful base interface that other classes also expose, so saying that a function takes a _CData argument can make good sense. (As a bunch of the methods in the io library do, for example) typeshed hacks it for the case where one is using .pyi files for type annotations, but there's no great way to just do that in ordinary Python code. (Really, I think it would be lovely to stick proper type signatures directly into the base libraries like io, but that's a whole separate conversation. I like strong typing, does it show?) Yonatan On Wed, Jan 5, 2022 at 5:15 PM Gregory P. Smith <greg@krypto.org> wrote:

On Wed, Jan 5, 2022 at 3:17 PM Yonatan Zunger <zunger@gmail.com> wrote:
typeshed is presumably referring to itself. It defines an interface for ctypes._CData in https://github.com/python/typeshed/blob/master/stdlib/ctypes/__init__.pyi#L8... The CPython ctypes docs *seem* reasonable to me. There is such a class. It is not public, so you cannot access ctypes._CData in any direct manner. That it gets called a class may be somewhat historical - its purpose is to provide a common interface. What code would ever actually care that it used class mechanisms as an internal implementation detail to do that? -gps

I think it makes good sense for the type-checking reason: _CData *does* declare a fairly useful base interface that other classes also expose, so saying that a function takes a _CData argument can make good sense. (As a bunch of the methods in the io library do, for example) typeshed hacks it for the case where one is using .pyi files for type annotations, but there's no great way to just do that in ordinary Python code. (Really, I think it would be lovely to stick proper type signatures directly into the base libraries like io, but that's a whole separate conversation. I like strong typing, does it show?) Yonatan On Wed, Jan 5, 2022 at 5:15 PM Gregory P. Smith <greg@krypto.org> wrote:
participants (2)
-
Gregory P. Smith
-
Yonatan Zunger