Coverage for /usr/lib/python3/dist-packages/numpy/_typing/_dtype_like.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-06-14 15:25 +0200

1from collections.abc import Sequence 

2from typing import ( 

3 Any, 

4 Sequence, 

5 Union, 

6 TypeVar, 

7 Protocol, 

8 TypedDict, 

9 runtime_checkable, 

10) 

11 

12import numpy as np 

13 

14from ._shape import _ShapeLike 

15 

16from ._char_codes import ( 

17 _BoolCodes, 

18 _UInt8Codes, 

19 _UInt16Codes, 

20 _UInt32Codes, 

21 _UInt64Codes, 

22 _Int8Codes, 

23 _Int16Codes, 

24 _Int32Codes, 

25 _Int64Codes, 

26 _Float16Codes, 

27 _Float32Codes, 

28 _Float64Codes, 

29 _Complex64Codes, 

30 _Complex128Codes, 

31 _ByteCodes, 

32 _ShortCodes, 

33 _IntCCodes, 

34 _IntPCodes, 

35 _IntCodes, 

36 _LongLongCodes, 

37 _UByteCodes, 

38 _UShortCodes, 

39 _UIntCCodes, 

40 _UIntPCodes, 

41 _UIntCodes, 

42 _ULongLongCodes, 

43 _HalfCodes, 

44 _SingleCodes, 

45 _DoubleCodes, 

46 _LongDoubleCodes, 

47 _CSingleCodes, 

48 _CDoubleCodes, 

49 _CLongDoubleCodes, 

50 _DT64Codes, 

51 _TD64Codes, 

52 _StrCodes, 

53 _BytesCodes, 

54 _VoidCodes, 

55 _ObjectCodes, 

56) 

57 

58_SCT = TypeVar("_SCT", bound=np.generic) 

59_DType_co = TypeVar("_DType_co", covariant=True, bound=np.dtype[Any]) 

60 

61_DTypeLikeNested = Any # TODO: wait for support for recursive types 

62 

63 

64# Mandatory keys 

65class _DTypeDictBase(TypedDict): 

66 names: Sequence[str] 

67 formats: Sequence[_DTypeLikeNested] 

68 

69 

70# Mandatory + optional keys 

71class _DTypeDict(_DTypeDictBase, total=False): 

72 # Only `str` elements are usable as indexing aliases, 

73 # but `titles` can in principle accept any object 

74 offsets: Sequence[int] 

75 titles: Sequence[Any] 

76 itemsize: int 

77 aligned: bool 

78 

79 

80# A protocol for anything with the dtype attribute 

81@runtime_checkable 

82class _SupportsDType(Protocol[_DType_co]): 

83 @property 

84 def dtype(self) -> _DType_co: ... 

85 

86 

87# A subset of `npt.DTypeLike` that can be parametrized w.r.t. `np.generic` 

88_DTypeLike = Union[ 

89 np.dtype[_SCT], 

90 type[_SCT], 

91 _SupportsDType[np.dtype[_SCT]], 

92] 

93 

94 

95# Would create a dtype[np.void] 

96_VoidDTypeLike = Union[ 

97 # (flexible_dtype, itemsize) 

98 tuple[_DTypeLikeNested, int], 

99 # (fixed_dtype, shape) 

100 tuple[_DTypeLikeNested, _ShapeLike], 

101 # [(field_name, field_dtype, field_shape), ...] 

102 # 

103 # The type here is quite broad because NumPy accepts quite a wide 

104 # range of inputs inside the list; see the tests for some 

105 # examples. 

106 list[Any], 

107 # {'names': ..., 'formats': ..., 'offsets': ..., 'titles': ..., 

108 # 'itemsize': ...} 

109 _DTypeDict, 

110 # (base_dtype, new_dtype) 

111 tuple[_DTypeLikeNested, _DTypeLikeNested], 

112] 

113 

114# Anything that can be coerced into numpy.dtype. 

115# Reference: https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html 

116DTypeLike = Union[ 

117 np.dtype[Any], 

118 # default data type (float64) 

119 None, 

120 # array-scalar types and generic types 

121 type[Any], # NOTE: We're stuck with `type[Any]` due to object dtypes 

122 # anything with a dtype attribute 

123 _SupportsDType[np.dtype[Any]], 

124 # character codes, type strings or comma-separated fields, e.g., 'float64' 

125 str, 

126 _VoidDTypeLike, 

127] 

128 

129# NOTE: while it is possible to provide the dtype as a dict of 

130# dtype-like objects (e.g. `{'field1': ..., 'field2': ..., ...}`), 

131# this syntax is officially discourged and 

132# therefore not included in the Union defining `DTypeLike`. 

133# 

134# See https://github.com/numpy/numpy/issues/16891 for more details. 

135 

136# Aliases for commonly used dtype-like objects. 

137# Note that the precision of `np.number` subclasses is ignored herein. 

138_DTypeLikeBool = Union[ 

139 type[bool], 

140 type[np.bool_], 

141 np.dtype[np.bool_], 

142 _SupportsDType[np.dtype[np.bool_]], 

143 _BoolCodes, 

144] 

145_DTypeLikeUInt = Union[ 

146 type[np.unsignedinteger], 

147 np.dtype[np.unsignedinteger], 

148 _SupportsDType[np.dtype[np.unsignedinteger]], 

149 _UInt8Codes, 

150 _UInt16Codes, 

151 _UInt32Codes, 

152 _UInt64Codes, 

153 _UByteCodes, 

154 _UShortCodes, 

155 _UIntCCodes, 

156 _UIntPCodes, 

157 _UIntCodes, 

158 _ULongLongCodes, 

159] 

160_DTypeLikeInt = Union[ 

161 type[int], 

162 type[np.signedinteger], 

163 np.dtype[np.signedinteger], 

164 _SupportsDType[np.dtype[np.signedinteger]], 

165 _Int8Codes, 

166 _Int16Codes, 

167 _Int32Codes, 

168 _Int64Codes, 

169 _ByteCodes, 

170 _ShortCodes, 

171 _IntCCodes, 

172 _IntPCodes, 

173 _IntCodes, 

174 _LongLongCodes, 

175] 

176_DTypeLikeFloat = Union[ 

177 type[float], 

178 type[np.floating], 

179 np.dtype[np.floating], 

180 _SupportsDType[np.dtype[np.floating]], 

181 _Float16Codes, 

182 _Float32Codes, 

183 _Float64Codes, 

184 _HalfCodes, 

185 _SingleCodes, 

186 _DoubleCodes, 

187 _LongDoubleCodes, 

188] 

189_DTypeLikeComplex = Union[ 

190 type[complex], 

191 type[np.complexfloating], 

192 np.dtype[np.complexfloating], 

193 _SupportsDType[np.dtype[np.complexfloating]], 

194 _Complex64Codes, 

195 _Complex128Codes, 

196 _CSingleCodes, 

197 _CDoubleCodes, 

198 _CLongDoubleCodes, 

199] 

200_DTypeLikeDT64 = Union[ 

201 type[np.timedelta64], 

202 np.dtype[np.timedelta64], 

203 _SupportsDType[np.dtype[np.timedelta64]], 

204 _TD64Codes, 

205] 

206_DTypeLikeTD64 = Union[ 

207 type[np.datetime64], 

208 np.dtype[np.datetime64], 

209 _SupportsDType[np.dtype[np.datetime64]], 

210 _DT64Codes, 

211] 

212_DTypeLikeStr = Union[ 

213 type[str], 

214 type[np.str_], 

215 np.dtype[np.str_], 

216 _SupportsDType[np.dtype[np.str_]], 

217 _StrCodes, 

218] 

219_DTypeLikeBytes = Union[ 

220 type[bytes], 

221 type[np.bytes_], 

222 np.dtype[np.bytes_], 

223 _SupportsDType[np.dtype[np.bytes_]], 

224 _BytesCodes, 

225] 

226_DTypeLikeVoid = Union[ 

227 type[np.void], 

228 np.dtype[np.void], 

229 _SupportsDType[np.dtype[np.void]], 

230 _VoidCodes, 

231 _VoidDTypeLike, 

232] 

233_DTypeLikeObject = Union[ 

234 type, 

235 np.dtype[np.object_], 

236 _SupportsDType[np.dtype[np.object_]], 

237 _ObjectCodes, 

238] 

239 

240_DTypeLikeComplex_co = Union[ 

241 _DTypeLikeBool, 

242 _DTypeLikeUInt, 

243 _DTypeLikeInt, 

244 _DTypeLikeFloat, 

245 _DTypeLikeComplex, 

246]