From a60272828420b17e5227701d6aa632066e8d4184 Mon Sep 17 00:00:00 2001 From: GastonAQS Date: Mon, 6 Oct 2025 18:04:03 -0300 Subject: [PATCH 1/3] Add broad definition for c_bool constructor --- stdlib/@tests/test_cases/ctypes/check_bool.py | 25 +++++++++++++++++++ stdlib/_typeshed/__init__.pyi | 3 +++ stdlib/ctypes/__init__.pyi | 4 +-- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 stdlib/@tests/test_cases/ctypes/check_bool.py diff --git a/stdlib/@tests/test_cases/ctypes/check_bool.py b/stdlib/@tests/test_cases/ctypes/check_bool.py new file mode 100644 index 000000000000..4cbc3277cd62 --- /dev/null +++ b/stdlib/@tests/test_cases/ctypes/check_bool.py @@ -0,0 +1,25 @@ +import ctypes +from typing_extensions import assert_type + + +class Foo: + def __bool__(self) -> bool: + return True + + +class Bar: + def __len__(self) -> int: + return 1 + + +assert_type(ctypes.c_bool(True), ctypes.c_bool) +assert_type(ctypes.c_bool(0), ctypes.c_bool) +assert_type(ctypes.c_bool([]), ctypes.c_bool) +assert_type(ctypes.c_bool({}), ctypes.c_bool) +assert_type(ctypes.c_bool(()), ctypes.c_bool) +assert_type(ctypes.c_bool(set[str]()), ctypes.c_bool) +assert_type(ctypes.c_bool(1.5), ctypes.c_bool) +assert_type(ctypes.c_bool("non-empty"), ctypes.c_bool) +assert_type(ctypes.c_bool(None), ctypes.c_bool) +assert_type(ctypes.c_bool(Foo()), ctypes.c_bool) +assert_type(ctypes.c_bool(Bar()), ctypes.c_bool) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 25054b601a4f..b786923880e1 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -142,6 +142,9 @@ class SupportsIter(Protocol[_T_co]): class SupportsAiter(Protocol[_T_co]): def __aiter__(self) -> _T_co: ... +class SupportsLen(Protocol): + def __len__(self) -> int: ... + class SupportsLenAndGetItem(Protocol[_T_co]): def __len__(self) -> int: ... def __getitem__(self, k: int, /) -> _T_co: ... diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 9da972240abb..9aa28f668e97 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -23,7 +23,7 @@ from _ctypes import ( set_errno as set_errno, sizeof as sizeof, ) -from _typeshed import StrPath +from _typeshed import StrPath, SupportsBool, SupportsLen from ctypes._endian import BigEndianStructure as BigEndianStructure, LittleEndianStructure as LittleEndianStructure from types import GenericAlias from typing import Any, ClassVar, Final, Generic, Literal, TypeVar, overload, type_check_only @@ -217,7 +217,7 @@ class py_object(_CanCastTo, _SimpleCData[_T]): class c_bool(_SimpleCData[bool]): _type_: ClassVar[Literal["?"]] - def __init__(self, value: bool = ...) -> None: ... + def __init__(self, value: SupportsBool | SupportsLen = ...) -> None: ... class c_byte(_SimpleCData[int]): _type_: ClassVar[Literal["b"]] From 17f4f8757b4d854e70ec5d52ab8603750de9ede8 Mon Sep 17 00:00:00 2001 From: GastonAQS Date: Mon, 6 Oct 2025 18:11:03 -0300 Subject: [PATCH 2/3] add None support --- stdlib/ctypes/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 9aa28f668e97..19bd261c67e0 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -217,7 +217,7 @@ class py_object(_CanCastTo, _SimpleCData[_T]): class c_bool(_SimpleCData[bool]): _type_: ClassVar[Literal["?"]] - def __init__(self, value: SupportsBool | SupportsLen = ...) -> None: ... + def __init__(self, value: SupportsBool | SupportsLen | None = ...) -> None: ... class c_byte(_SimpleCData[int]): _type_: ClassVar[Literal["b"]] From 9de585fb8e121b342ac724e715b80abe44bd9793 Mon Sep 17 00:00:00 2001 From: GastonAQS Date: Tue, 7 Oct 2025 09:46:09 -0300 Subject: [PATCH 3/3] remove test --- stdlib/@tests/test_cases/ctypes/check_bool.py | 25 ------------------- 1 file changed, 25 deletions(-) delete mode 100644 stdlib/@tests/test_cases/ctypes/check_bool.py diff --git a/stdlib/@tests/test_cases/ctypes/check_bool.py b/stdlib/@tests/test_cases/ctypes/check_bool.py deleted file mode 100644 index 4cbc3277cd62..000000000000 --- a/stdlib/@tests/test_cases/ctypes/check_bool.py +++ /dev/null @@ -1,25 +0,0 @@ -import ctypes -from typing_extensions import assert_type - - -class Foo: - def __bool__(self) -> bool: - return True - - -class Bar: - def __len__(self) -> int: - return 1 - - -assert_type(ctypes.c_bool(True), ctypes.c_bool) -assert_type(ctypes.c_bool(0), ctypes.c_bool) -assert_type(ctypes.c_bool([]), ctypes.c_bool) -assert_type(ctypes.c_bool({}), ctypes.c_bool) -assert_type(ctypes.c_bool(()), ctypes.c_bool) -assert_type(ctypes.c_bool(set[str]()), ctypes.c_bool) -assert_type(ctypes.c_bool(1.5), ctypes.c_bool) -assert_type(ctypes.c_bool("non-empty"), ctypes.c_bool) -assert_type(ctypes.c_bool(None), ctypes.c_bool) -assert_type(ctypes.c_bool(Foo()), ctypes.c_bool) -assert_type(ctypes.c_bool(Bar()), ctypes.c_bool)