Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Adding descriptor methods to _SimpleCData #11272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
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
7 changes: 7 additions & 0 deletions stdlib/_ctypes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ class _SimpleCData(_CData, Generic[_T]):
# The TypeVar can be unsolved here,
# but we can't use overloads without creating many, many mypy false-positive errors
def __init__(self, value: _T = ...) -> None: ... # pyright: ignore[reportInvalidTypeVarUse]
@overload
def __get__(self, __instance: None, __owner: type[Any] | None) -> Self: ...
@overload
def __get__(self, __instance: Any, __owner: type[_StructUnionBase]) -> _T: ...
@overload
def __get__(self, __instance: Any, __owner: type[Any] | None) -> Self: ...
def __set__(self, __instance: Any, __value: _T) -> None: ...

class _CanCastTo(_CData): ...
class _PointerLike(_CanCastTo): ...
Expand Down
45 changes: 45 additions & 0 deletions test_cases/stdlib/check_ctypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# pyright: reportUninitializedInstanceVariable=false

from __future__ import annotations

import ctypes
from typing import Any
from typing_extensions import assert_type


class Annotated(ctypes.Structure):
_fields_ = [("i", ctypes.c_int), ("f", ctypes.c_float)]
i: ctypes.c_int
f: ctypes.c_float


class NoAnnotation(ctypes.Structure):
_fields_ = [("i", ctypes.c_int), ("f", ctypes.c_float)]


class NonCType:
i: ctypes.c_int
f: ctypes.c_float


assert_type(ctypes.c_int().value, int)
ctypes.c_int() + 2 # type: ignore

assert_type(ctypes.c_float().value, float)
ctypes.c_float() + 2 # type: ignore

# All passes; all access are Any
assert_type(NoAnnotation().x + 2, Any)
assert_type(NoAnnotation().vec.x + 2, Any)

Annotated.x.value + 2 # type: ignore
Annotated.x + 2 # type: ignore
assert_type(Annotated().i, int)
Annotated().i.value + 2 # type: ignore
assert_type(Annotated().f, float)
Annotated().f.value + 3.14 # type: ignore

assert_type(NonCType.i.value, int)
NonCType.i + 2 # type: ignore
NonCType().i + 2 # type: ignore
assert_type(NonCType().i.value, int)
4 changes: 4 additions & 0 deletions tests/stubtest_allowlists/py3_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ _collections_abc.KeysView.__reversed__
_collections_abc.ValuesView.__reversed__
_csv.Dialect.__init__ # C __init__ signature is inaccurate
_ctypes.CFuncPtr # stubtest erroneously thinks it can't be subclassed
_ctypes._SimpleCData.__get__ # Does not exist on runtime object
_ctypes._SimpleCData.__set__ # Does not exist on runtime object
_threading_local.local.__new__
ast.Bytes.__new__
ast.Ellipsis.__new__
Expand All @@ -37,6 +39,8 @@ ctypes.memmove # CFunctionType
ctypes.memset # CFunctionType
ctypes.string_at # docstring argument name is wrong
ctypes.wstring_at # docstring argument name is wrong
ctypes._SimpleCData.__get__ # Does not exist on runtime object
ctypes._SimpleCData.__set__ # Does not exist on runtime object
fractions.Fraction.__new__ # overload is too complicated for stubtest to resolve
ftplib.FTP.trust_server_pasv_ipv4_address # Dangerous to use, intentionally undocumented, intentionally missing from typeshed. #6154
functools.cached_property.__set__ # Stub is a white lie; see comments in the stub
Expand Down