Skip to content

Commit a602728

Browse files
committed
Add broad definition for c_bool constructor
1 parent bbe48c7 commit a602728

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import ctypes
2+
from typing_extensions import assert_type
3+
4+
5+
class Foo:
6+
def __bool__(self) -> bool:
7+
return True
8+
9+
10+
class Bar:
11+
def __len__(self) -> int:
12+
return 1
13+
14+
15+
assert_type(ctypes.c_bool(True), ctypes.c_bool)
16+
assert_type(ctypes.c_bool(0), ctypes.c_bool)
17+
assert_type(ctypes.c_bool([]), ctypes.c_bool)
18+
assert_type(ctypes.c_bool({}), ctypes.c_bool)
19+
assert_type(ctypes.c_bool(()), ctypes.c_bool)
20+
assert_type(ctypes.c_bool(set[str]()), ctypes.c_bool)
21+
assert_type(ctypes.c_bool(1.5), ctypes.c_bool)
22+
assert_type(ctypes.c_bool("non-empty"), ctypes.c_bool)
23+
assert_type(ctypes.c_bool(None), ctypes.c_bool)
24+
assert_type(ctypes.c_bool(Foo()), ctypes.c_bool)
25+
assert_type(ctypes.c_bool(Bar()), ctypes.c_bool)

stdlib/_typeshed/__init__.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ class SupportsIter(Protocol[_T_co]):
142142
class SupportsAiter(Protocol[_T_co]):
143143
def __aiter__(self) -> _T_co: ...
144144

145+
class SupportsLen(Protocol):
146+
def __len__(self) -> int: ...
147+
145148
class SupportsLenAndGetItem(Protocol[_T_co]):
146149
def __len__(self) -> int: ...
147150
def __getitem__(self, k: int, /) -> _T_co: ...

stdlib/ctypes/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ from _ctypes import (
2323
set_errno as set_errno,
2424
sizeof as sizeof,
2525
)
26-
from _typeshed import StrPath
26+
from _typeshed import StrPath, SupportsBool, SupportsLen
2727
from ctypes._endian import BigEndianStructure as BigEndianStructure, LittleEndianStructure as LittleEndianStructure
2828
from types import GenericAlias
2929
from typing import Any, ClassVar, Final, Generic, Literal, TypeVar, overload, type_check_only
@@ -217,7 +217,7 @@ class py_object(_CanCastTo, _SimpleCData[_T]):
217217

218218
class c_bool(_SimpleCData[bool]):
219219
_type_: ClassVar[Literal["?"]]
220-
def __init__(self, value: bool = ...) -> None: ...
220+
def __init__(self, value: SupportsBool | SupportsLen = ...) -> None: ...
221221

222222
class c_byte(_SimpleCData[int]):
223223
_type_: ClassVar[Literal["b"]]

0 commit comments

Comments
 (0)