Skip to content

Commit 2da66b8

Browse files
authored
Merge pull request #524 from numpy/simplify-ctypeslib.as_ctypes_type
2 parents 7521a86 + 4637688 commit 2da66b8

4 files changed

Lines changed: 111 additions & 123 deletions

File tree

src/_numtype/op.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ __all__ = [ # noqa: RUF022
4040

4141
###
4242

43-
_T_contra = TypeVar("_T_contra", contravariant=True, default=object)
43+
_T_contra = TypeVar("_T_contra", contravariant=True, default=Any)
4444
_T_co = TypeVar("_T_co", covariant=True, default=Any)
4545

4646
###

src/numpy-stubs/@test/static/accept/ctypeslib.pyi

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,3 @@ assert_type(np.ctypeslib.as_ctypes(AR_f8), ct.Array[ct.c_double])
7878
assert_type(np.ctypeslib.as_array(AR_u1), npt.NDArray[np.ubyte])
7979
assert_type(np.ctypeslib.as_array(1), npt.NDArray[Any])
8080
assert_type(np.ctypeslib.as_array(pointer), npt.NDArray[Any])
81-
82-
assert_type(np.ctypeslib.as_ctypes_type(int), type[ct.c_ssize_t])
83-
assert_type(np.ctypeslib.as_ctypes_type("N"), type[ct.c_size_t])

src/numpy-stubs/__init__.pyi

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,14 @@ _HasDTypeWithItem: TypeAlias = _HasDType[_HasTypeWithItem[_T]]
10171017
_HasDTypeWithReal: TypeAlias = _HasDType[_HasTypeWithReal[_T]]
10181018
_HasDTypeWithImag: TypeAlias = _HasDType[_HasTypeWithImag[_T]]
10191019

1020+
_CT = TypeVar("_CT", bound=ct._SimpleCData[Any])
1021+
_CT_co = TypeVar("_CT_co", bound=ct._SimpleCData[Any], covariant=True)
1022+
1023+
@type_check_only
1024+
class _HasCType(Protocol[_CT_co]):
1025+
@property
1026+
def __ctype__(self, /) -> _CT_co: ...
1027+
10201028
@type_check_only
10211029
class _HasDateAttributes(Protocol):
10221030
# The `datetime64` constructors requires an object with the three attributes below,
@@ -1028,22 +1036,6 @@ class _HasDateAttributes(Protocol):
10281036
@property
10291037
def year(self) -> int: ...
10301038

1031-
@type_check_only
1032-
class _CanLT(Protocol):
1033-
def __lt__(self, x: Any, /) -> Any: ...
1034-
1035-
@type_check_only
1036-
class _CanLE(Protocol):
1037-
def __le__(self, x: Any, /) -> Any: ...
1038-
1039-
@type_check_only
1040-
class _CanGT(Protocol):
1041-
def __gt__(self, x: Any, /) -> Any: ...
1042-
1043-
@type_check_only
1044-
class _CanGE(Protocol):
1045-
def __ge__(self, x: Any, /) -> Any: ...
1046-
10471039
###
10481040
# Mixins (for internal use only)
10491041

@@ -1077,33 +1069,33 @@ class _CmpOpMixin(Generic[_ScalarLikeT_contra, _ArrayLikeT_contra]):
10771069
@overload
10781070
def __lt__(self, x: _ScalarLikeT_contra, /) -> bool_: ...
10791071
@overload
1080-
def __lt__(self, x: _ArrayLikeT_contra | _NestedSequence[_CanGT], /) -> NDArray[bool_]: ...
1072+
def __lt__(self, x: _ArrayLikeT_contra | _NestedSequence[_nt.op.CanGt], /) -> NDArray[bool_]: ...
10811073
@overload
1082-
def __lt__(self, x: _CanGT, /) -> bool_: ...
1074+
def __lt__(self, x: _nt.op.CanGt, /) -> bool_: ...
10831075

10841076
#
10851077
@overload
10861078
def __le__(self, x: _ScalarLikeT_contra, /) -> bool_: ...
10871079
@overload
1088-
def __le__(self, x: _ArrayLikeT_contra | _NestedSequence[_CanGE], /) -> NDArray[bool_]: ...
1080+
def __le__(self, x: _ArrayLikeT_contra | _NestedSequence[_nt.op.CanGe], /) -> NDArray[bool_]: ...
10891081
@overload
1090-
def __le__(self, x: _CanGE, /) -> bool_: ...
1082+
def __le__(self, x: _nt.op.CanGe, /) -> bool_: ...
10911083

10921084
#
10931085
@overload
10941086
def __gt__(self, x: _ScalarLikeT_contra, /) -> bool_: ...
10951087
@overload
1096-
def __gt__(self, x: _ArrayLikeT_contra | _NestedSequence[_CanLT], /) -> NDArray[bool_]: ...
1088+
def __gt__(self, x: _ArrayLikeT_contra | _NestedSequence[_nt.op.CanLt], /) -> NDArray[bool_]: ...
10971089
@overload
1098-
def __gt__(self, x: _CanLT, /) -> bool_: ...
1090+
def __gt__(self, x: _nt.op.CanLt, /) -> bool_: ...
10991091

11001092
#
11011093
@overload
11021094
def __ge__(self, x: _ScalarLikeT_contra, /) -> bool_: ...
11031095
@overload
1104-
def __ge__(self, x: _ArrayLikeT_contra | _NestedSequence[_CanLE], /) -> NDArray[bool_]: ...
1096+
def __ge__(self, x: _ArrayLikeT_contra | _NestedSequence[_nt.op.CanLe], /) -> NDArray[bool_]: ...
11051097
@overload
1106-
def __ge__(self, x: _CanLE, /) -> bool_: ...
1098+
def __ge__(self, x: _nt.op.CanLe, /) -> bool_: ...
11071099

11081100
@type_check_only
11091101
class _IntMixin(Generic[_IntSizeT_co]):
@@ -1908,6 +1900,11 @@ class _ArrayOrScalarCommon:
19081900
class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
19091901
__hash__: ClassVar[None] # type: ignore[assignment] # pyright: ignore[reportIncompatibleMethodOverride]
19101902

1903+
#
1904+
@property
1905+
@type_check_only
1906+
def __ctype__(self: _HasDType[_HasType[_HasCType[_CT]]], /) -> ct.Array[_CT]: ...
1907+
19111908
#
19121909
@property
19131910
def base(self) -> NDArray[Any] | None: ...
@@ -3927,6 +3924,11 @@ class generic(_ArrayOrScalarCommon, Generic[_ItemT_co]):
39273924

39283925
# NOTE: Naming it `bool_` results in less unreadable type-checker output
39293926
class bool_(generic[_BoolItemT_co], Generic[_BoolItemT_co]):
3927+
@property
3928+
@type_check_only
3929+
def __ctype__(self) -> ct.c_bool: ...
3930+
3931+
#
39303932
@type_check_only
39313933
def __nep50__(self, below: _nt.co_number | timedelta64, above: Never, /) -> bool_: ...
39323934
@type_check_only
@@ -4386,7 +4388,6 @@ class number(_CmpOpMixin[_nt.CoComplex_0d, _nt.CoComplex_1nd], generic[_NumberIt
43864388
@abc.abstractmethod
43874389
@type_check_only
43884390
def __nep50_complex__(self, /) -> complexfloating: ...
4389-
#
43904391
@type_check_only
43914392
def __nep50_rule6__(self, other: _JustNumber, /) -> number: ...
43924393

@@ -4682,6 +4683,11 @@ class signedinteger(integer):
46824683
def __nep50_rule5__(self, other: _JustInteger | _JustUnsignedInteger, /) -> signedinteger | float64: ...
46834684

46844685
class int8(_IntMixin[L[1]], signedinteger):
4686+
@property
4687+
@type_check_only
4688+
def __ctype__(self) -> ct.c_int8: ...
4689+
4690+
#
46854691
@override
46864692
@type_check_only
46874693
def __nep50__(
@@ -4703,6 +4709,11 @@ class int8(_IntMixin[L[1]], signedinteger):
47034709
byte = int8
47044710

47054711
class int16(_IntMixin[L[2]], signedinteger):
4712+
@property
4713+
@type_check_only
4714+
def __ctype__(self) -> ct.c_int16: ...
4715+
4716+
#
47064717
@override
47074718
@type_check_only
47084719
def __nep50__(
@@ -4724,6 +4735,11 @@ class int16(_IntMixin[L[2]], signedinteger):
47244735
short = int16
47254736

47264737
class int32(_IntMixin[L[4]], signedinteger):
4738+
@property
4739+
@type_check_only
4740+
def __ctype__(self) -> ct.c_int32: ...
4741+
4742+
#
47274743
@override
47284744
@type_check_only
47294745
def __nep50__(
@@ -4748,6 +4764,11 @@ class int32(_IntMixin[L[4]], signedinteger):
47484764
intc = int32
47494765

47504766
class int64(_IntMixin[L[8]], signedinteger):
4767+
@property
4768+
@type_check_only
4769+
def __ctype__(self) -> ct.c_int64: ...
4770+
4771+
#
47514772
@override
47524773
@type_check_only
47534774
def __nep50__(
@@ -4799,6 +4820,11 @@ class unsignedinteger(integer):
47994820
def __nep50_rule3__(self, other: _JustUnsignedInteger, /) -> unsignedinteger: ...
48004821

48014822
class uint8(_IntMixin[L[1]], unsignedinteger):
4823+
@property
4824+
@type_check_only
4825+
def __ctype__(self) -> ct.c_uint8: ...
4826+
4827+
#
48024828
@override
48034829
@type_check_only
48044830
def __nep50__(
@@ -4824,6 +4850,11 @@ class uint8(_IntMixin[L[1]], unsignedinteger):
48244850
ubyte = uint8
48254851

48264852
class uint16(_IntMixin[L[2]], unsignedinteger):
4853+
@property
4854+
@type_check_only
4855+
def __ctype__(self) -> ct.c_uint16: ...
4856+
4857+
#
48274858
@override
48284859
@type_check_only
48294860
def __nep50__(
@@ -4851,6 +4882,11 @@ class uint16(_IntMixin[L[2]], unsignedinteger):
48514882
ushort = uint16
48524883

48534884
class uint32(_IntMixin[L[4]], unsignedinteger):
4885+
@property
4886+
@type_check_only
4887+
def __ctype__(self) -> ct.c_uint32: ...
4888+
4889+
#
48544890
@override
48554891
@type_check_only
48564892
def __nep50__(
@@ -4878,6 +4914,11 @@ class uint32(_IntMixin[L[4]], unsignedinteger):
48784914
uintc = uint32
48794915

48804916
class uint64(_IntMixin[L[8]], unsignedinteger):
4917+
@property
4918+
@type_check_only
4919+
def __ctype__(self) -> ct.c_uint64: ...
4920+
4921+
#
48814922
@override
48824923
@type_check_only
48834924
def __nep50__(
@@ -5021,6 +5062,11 @@ class float16(_FloatMixin[L[2]], floating):
50215062
half = float16
50225063

50235064
class float32(_FloatMixin[L[4]], floating):
5065+
@property
5066+
@type_check_only
5067+
def __ctype__(self) -> ct.c_float: ...
5068+
5069+
#
50245070
@override
50255071
@type_check_only
50265072
def __nep50__(self, below: _float32_min | complexfloating, above: float16 | _nt.co_integer16, /) -> float32: ...
@@ -5040,6 +5086,11 @@ class float32(_FloatMixin[L[4]], floating):
50405086
single = float32
50415087

50425088
class float64(_FloatMixin[L[8]], floating, float): # type: ignore[misc]
5089+
@property
5090+
@type_check_only
5091+
def __ctype__(self) -> ct.c_double: ...
5092+
5093+
#
50435094
@override
50445095
@type_check_only
50455096
def __nep50__(self, below: _inexact64_min, above: _float32_max | _nt.co_integer, /) -> float64: ...
@@ -5076,6 +5127,11 @@ class float64(_FloatMixin[L[8]], floating, float): # type: ignore[misc]
50765127
double = float64
50775128

50785129
class longdouble(_FloatMixin[L[12, 16]], floating):
5130+
@property
5131+
@type_check_only
5132+
def __ctype__(self) -> ct.c_longdouble: ...
5133+
5134+
#
50795135
@override
50805136
@type_check_only
50815137
def __nep50__(

0 commit comments

Comments
 (0)