diff --git a/stdlib/_ctypes.pyi b/stdlib/_ctypes.pyi index 41252bcbf89b..dcb870bc39d4 100644 --- a/stdlib/_ctypes.pyi +++ b/stdlib/_ctypes.pyi @@ -1,7 +1,7 @@ import sys from _typeshed import ReadableBuffer, WriteableBuffer from abc import abstractmethod -from collections.abc import Iterable, Iterator, Mapping +from collections.abc import Iterable, Iterator, Mapping, Sequence from ctypes import CDLL, _CArgObject, _PointerLike from typing import Any, Generic, TypeVar, overload from typing_extensions import Self, TypeAlias @@ -66,6 +66,24 @@ class _SimpleCData(Generic[_T], _CData): # but we can't use overloads without creating many, many mypy false-positive errors def __init__(self, value: _T = ...) -> None: ... # pyright: ignore[reportInvalidTypeVarUse] +class _CField: + offset: int + size: int + +class _StructUnionMeta(_CDataMeta): + _fields_: Sequence[tuple[str, type[_CData]] | tuple[str, type[_CData], int]] + _pack_: int + _anonymous_: Sequence[str] + def __getattr__(self, name: str) -> _CField: ... + +class _StructUnionBase(_CData, metaclass=_StructUnionMeta): + def __init__(self, *args: Any, **kw: Any) -> None: ... + def __getattr__(self, name: str) -> Any: ... + def __setattr__(self, name: str, value: Any) -> None: ... + +class Union(_StructUnionBase): ... +class Structure(_StructUnionBase): ... + class Array(Generic[_CT], _CData): @property @abstractmethod diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 117561826d40..c85d65c9f474 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -3,9 +3,14 @@ from _ctypes import ( RTLD_GLOBAL as RTLD_GLOBAL, RTLD_LOCAL as RTLD_LOCAL, Array as Array, + Structure as Structure, + Union as Union, _CData as _CData, _CDataMeta as _CDataMeta, + _CField as _CField, _SimpleCData as _SimpleCData, + _StructUnionBase as _StructUnionBase, + _StructUnionMeta as _StructUnionMeta, ) from collections.abc import Callable, Sequence from typing import Any, ClassVar, Generic, TypeVar, overload @@ -216,23 +221,5 @@ if sys.platform == "win32": class HRESULT(_SimpleCData[int]): ... # TODO undocumented class py_object(_CanCastTo, _SimpleCData[_T]): ... - -class _CField: - offset: int - size: int - -class _StructUnionMeta(_CDataMeta): - _fields_: Sequence[tuple[str, type[_CData]] | tuple[str, type[_CData], int]] - _pack_: int - _anonymous_: Sequence[str] - def __getattr__(self, name: str) -> _CField: ... - -class _StructUnionBase(_CData, metaclass=_StructUnionMeta): - def __init__(self, *args: Any, **kw: Any) -> None: ... - def __getattr__(self, name: str) -> Any: ... - def __setattr__(self, name: str, value: Any) -> None: ... - -class Union(_StructUnionBase): ... -class Structure(_StructUnionBase): ... class BigEndianStructure(Structure): ... class LittleEndianStructure(Structure): ... diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index fc50e2aec757..ea0fbe1c6f83 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -339,8 +339,6 @@ _ctypes.POINTER _ctypes.PyObj_FromPtr _ctypes.Py_DECREF _ctypes.Py_INCREF -_ctypes.Structure -_ctypes.Union _ctypes.addressof _ctypes.alignment _ctypes.buffer_info