From ca1752227d5e9a73f8e7d044497f67c25de7417a Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Tue, 23 Nov 2021 12:52:05 +0300 Subject: [PATCH 1/4] Makes `Array` abstract Refs #6349 --- stdlib/ctypes/__init__.pyi | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index bbe083f5d4c4..271f632b2f05 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -1,5 +1,6 @@ import sys from _typeshed import ReadableBuffer, WriteableBuffer +from abc import abstractproperty from typing import ( Any, Callable, @@ -268,8 +269,14 @@ class BigEndianStructure(Structure): ... class LittleEndianStructure(Structure): ... class Array(Generic[_CT], _CData): - _length_: int - _type_: Type[_CT] + @abstractproperty + def _length_(self) -> int: ... + @_length_.setter + def _set_length_(self, value: int) -> None: ... + @abstractproperty + def _type_(self) -> Type[_CT]: ... + @_type_.setter + def _set_type_(self, value: Type[_CT]) -> None: ... raw: bytes # Note: only available if _CT == c_char value: Any # Note: bytes if _CT == c_char, str if _CT == c_wchar, unavailable otherwise # TODO These methods cannot be annotated correctly at the moment. From fb258b3b98c246cc78d5f20a55867a77b53d5648 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Tue, 23 Nov 2021 13:08:05 +0300 Subject: [PATCH 2/4] Update __init__.pyi --- stdlib/ctypes/__init__.pyi | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 271f632b2f05..6847aae2338d 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -1,6 +1,6 @@ import sys from _typeshed import ReadableBuffer, WriteableBuffer -from abc import abstractproperty +from abc import abstractmethod from typing import ( Any, Callable, @@ -269,14 +269,18 @@ class BigEndianStructure(Structure): ... class LittleEndianStructure(Structure): ... class Array(Generic[_CT], _CData): - @abstractproperty + @property + @abstractmethod def _length_(self) -> int: ... @_length_.setter def _set_length_(self, value: int) -> None: ... - @abstractproperty + + @property + @abstractmethod def _type_(self) -> Type[_CT]: ... @_type_.setter def _set_type_(self, value: Type[_CT]) -> None: ... + raw: bytes # Note: only available if _CT == c_char value: Any # Note: bytes if _CT == c_char, str if _CT == c_wchar, unavailable otherwise # TODO These methods cannot be annotated correctly at the moment. From 6ff463c0a94ef3afe46b9d07a76c756d520efdf2 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Tue, 23 Nov 2021 13:13:45 +0300 Subject: [PATCH 3/4] Update __init__.pyi --- stdlib/ctypes/__init__.pyi | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 6847aae2338d..40af2d348131 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -273,14 +273,12 @@ class Array(Generic[_CT], _CData): @abstractmethod def _length_(self) -> int: ... @_length_.setter - def _set_length_(self, value: int) -> None: ... - + def _length_(self, value: int) -> None: ... @property @abstractmethod def _type_(self) -> Type[_CT]: ... @_type_.setter - def _set_type_(self, value: Type[_CT]) -> None: ... - + def _type_(self, value: Type[_CT]) -> None: ... raw: bytes # Note: only available if _CT == c_char value: Any # Note: bytes if _CT == c_char, str if _CT == c_wchar, unavailable otherwise # TODO These methods cannot be annotated correctly at the moment. From b8a87e457c028d57bf5fcb65a866b0d044ebeb9b Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sun, 28 Nov 2021 23:28:32 +0300 Subject: [PATCH 4/4] Update py3_common.txt --- tests/stubtest_allowlists/py3_common.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index c449bf197cde..0e5cda267462 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -73,6 +73,8 @@ csv.Dialect.lineterminator csv.Dialect.quoting csv.Dialect.skipinitialspace ctypes.Array.__iter__ # mypy doesn't support using __getitem__ instead of __iter__ so this is here https://github.com/python/mypy/issues/2220 +ctypes.Array._type_ # _type_ and _length_ are abstract, https://github.com/python/typeshed/pull/6361 +ctypes.Array._length_ ctypes.CDLL._FuncPtr # None at class level but initialized in __init__ to this value ctypes.memmove # CFunctionType ctypes.memset # CFunctionType