Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions stdlib/@tests/test_cases/ctypes/check_bool.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/ctypes/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = ...) -> None: ...

class c_byte(_SimpleCData[int]):
_type_: ClassVar[Literal["b"]]
Expand Down