1From efc78eb26886e59a303a280466c4837e1671890f Mon Sep 17 00:00:00 2001
2From: Jasper Phelps <jasper.s.phelps@gmail.com>
3Date: Mon, 1 Jul 2024 18:01:33 +0200
4Subject: [PATCH] Grant basic compatibility with numpy2.0.0
5
6This commit includes the results of running
7`ruff check --select NPY201 --fix` on the code in this repo,
8plus the removal of `np.{dtype}0` aliases that currently aren't
9caught by the NPY201 ruff rule (see
10https://github.com/numpy/numpy/issues/26800 where this is mentioned)
11plus removing the `numpy<2.0.0` pin from `requirements.txt`.
12After this commit, `import nptyping` succeeds in an environment with
13`numpy==2.0.0`. No additional testing has been done at this time,
14and additional changes could very well be necessary before nptyping
15is fully compatible with numpy2.0.0. But this should be a start!
16---
17 dependencies/requirements.txt | 2 +-
18 nptyping/__init__.py | 14 --------------
19 nptyping/typing_.py | 32 +++++++++-----------------------
20 nptyping/typing_.pyi | 18 +++++++++---------
21 tests/test_ndarray.py | 2 +-
22 5 files changed, 20 insertions(+), 48 deletions(-)
23
24diff --git a/dependencies/requirements.txt b/dependencies/requirements.txt
25index 14a87b7..8a6f6b6 100644
26--- a/dependencies/requirements.txt
27+++ b/dependencies/requirements.txt
28@@ -1,3 +1,3 @@
29 numpy==1.21.5; python_version<'3.8'
30-numpy>=1.20.0,<2.0.0; python_version>='3.8'
31+numpy>=1.20.0; python_version>='3.8'
32 typing_extensions>=4.0.0,<5.0.0; python_version<'3.10'
33diff --git a/nptyping/__init__.py b/nptyping/__init__.py
34index 5fd5b2c..feb5f12 100644
35--- a/nptyping/__init__.py
36+++ b/nptyping/__init__.py
37@@ -41,10 +41,8 @@
38 from nptyping.structure import Structure
39 from nptyping.typing_ import (
40 Bool,
41- Bool8,
42 Byte,
43 Bytes,
44- Bytes0,
45 CDouble,
46 CFloat,
47 Character,
48@@ -67,7 +65,6 @@
49 Half,
50 Inexact,
51 Int,
52- Int0,
53 Int8,
54 Int16,
55 Int32,
56@@ -81,17 +78,14 @@
57 LongLong,
58 Number,
59 Object,
60- Object0,
61 Short,
62 SignedInteger,
63 Single,
64 SingleComplex,
65- Str0,
66 String,
67 Timedelta64,
68 UByte,
69 UInt,
70- UInt0,
71 UInt8,
72 UInt16,
73 UInt32,
74@@ -103,7 +97,6 @@
75 UnsignedInteger,
76 UShort,
77 Void,
78- Void0,
79 )
80
81 __all__ = [
82@@ -123,9 +116,7 @@
83 "DType",
84 "Number",
85 "Bool",
86- "Bool8",
87 "Object",
88- "Object0",
89 "Datetime64",
90 "Integer",
91 "SignedInteger",
92@@ -137,7 +128,6 @@
93 "Short",
94 "IntC",
95 "IntP",
96- "Int0",
97 "Int",
98 "LongLong",
99 "Timedelta64",
100@@ -150,7 +140,6 @@
101 "UShort",
102 "UIntC",
103 "UIntP",
104- "UInt0",
105 "UInt",
106 "ULongLong",
107 "Inexact",
108@@ -177,12 +166,9 @@
109 "LongComplex",
110 "Flexible",
111 "Void",
112- "Void0",
113 "Character",
114 "Bytes",
115 "String",
116- "Bytes0",
117 "Unicode",
118- "Str0",
119 "DataFrame",
120 ]
121diff --git a/nptyping/typing_.py b/nptyping/typing_.py
122index 2639e07..e40126e 100644
123--- a/nptyping/typing_.py
124+++ b/nptyping/typing_.py
125@@ -48,10 +48,8 @@
126
127 Number = np.number
128 Bool = np.bool_
129-Bool8 = np.bool8
130 Obj = np.object_ # Obj is a common abbreviation and should be usable.
131 Object = np.object_
132-Object0 = np.object0
133 Datetime64 = np.datetime64
134 Integer = np.integer
135 SignedInteger = np.signedinteger
136@@ -63,7 +61,6 @@
137 Short = np.short
138 IntC = np.intc
139 IntP = np.intp
140-Int0 = np.int0
141 Int = np.integer # Int should translate to the "generic" int type.
142 Int_ = np.int_
143 LongLong = np.longlong
144@@ -77,7 +74,6 @@
145 UShort = np.ushort
146 UIntC = np.uintc
147 UIntP = np.uintp
148-UInt0 = np.uint0
149 UInt = np.uint
150 ULongLong = np.ulonglong
151 Inexact = np.inexact
152@@ -88,38 +84,33 @@
153 Half = np.half
154 Single = np.single
155 Double = np.double
156-Float = np.float_
157+Float = np.float64
158 LongDouble = np.longdouble
159-LongFloat = np.longfloat
160+LongFloat = np.longdouble
161 ComplexFloating = np.complexfloating
162 Complex64 = np.complex64
163 Complex128 = np.complex128
164 CSingle = np.csingle
165-SingleComplex = np.singlecomplex
166+SingleComplex = np.complex64
167 CDouble = np.cdouble
168-Complex = np.complex_
169-CFloat = np.cfloat
170+Complex = np.complex128
171+CFloat = np.complex128
172 CLongDouble = np.clongdouble
173-CLongFloat = np.clongfloat
174-LongComplex = np.longcomplex
175+CLongFloat = np.clongdouble
176+LongComplex = np.clongdouble
177 Flexible = np.flexible
178 Void = np.void
179-Void0 = np.void0
180 Character = np.character
181 Bytes = np.bytes_
182 Str = np.str_
183-String = np.string_
184-Bytes0 = np.bytes0
185-Unicode = np.unicode_
186-Str0 = np.str0
187+String = np.bytes_
188+Unicode = np.str_
189
190 dtypes = [
191 (Number, "Number"),
192 (Bool, "Bool"),
193- (Bool8, "Bool8"),
194 (Obj, "Obj"),
195 (Object, "Object"),
196- (Object0, "Object0"),
197 (Datetime64, "Datetime64"),
198 (Integer, "Integer"),
199 (SignedInteger, "SignedInteger"),
200@@ -131,7 +122,6 @@
201 (Short, "Short"),
202 (IntC, "IntC"),
203 (IntP, "IntP"),
204- (Int0, "Int0"),
205 (Int, "Int"),
206 (LongLong, "LongLong"),
207 (Timedelta64, "Timedelta64"),
208@@ -144,7 +134,6 @@
209 (UShort, "UShort"),
210 (UIntC, "UIntC"),
211 (UIntP, "UIntP"),
212- (UInt0, "UInt0"),
213 (UInt, "UInt"),
214 (ULongLong, "ULongLong"),
215 (Inexact, "Inexact"),
216@@ -171,14 +160,11 @@
217 (LongComplex, "LongComplex"),
218 (Flexible, "Flexible"),
219 (Void, "Void"),
220- (Void0, "Void0"),
221 (Character, "Character"),
222 (Bytes, "Bytes"),
223 (String, "String"),
224 (Str, "Str"),
225- (Bytes0, "Bytes0"),
226 (Unicode, "Unicode"),
227- (Str0, "Str0"),
228 ]
229
230 name_per_dtype = dict(dtypes)
231diff --git a/nptyping/typing_.pyi b/nptyping/typing_.pyi
232index fcf83ce..157641a 100644
233--- a/nptyping/typing_.pyi
234+++ b/nptyping/typing_.pyi
235@@ -85,29 +85,29 @@ Float64: TypeAlias = np.dtype[np.float64]
236 Half: TypeAlias = np.dtype[np.half]
237 Single: TypeAlias = np.dtype[np.single]
238 Double: TypeAlias = np.dtype[np.double]
239-Float: TypeAlias = np.dtype[np.float_]
240+Float: TypeAlias = np.dtype[np.float64]
241 LongDouble: TypeAlias = np.dtype[np.longdouble]
242-LongFloat: TypeAlias = np.dtype[np.longfloat]
243+LongFloat: TypeAlias = np.dtype[np.longdouble]
244 ComplexFloating: TypeAlias = np.dtype[np.complexfloating[Any, Any]]
245 Complex64: TypeAlias = np.dtype[np.complex64]
246 Complex128: TypeAlias = np.dtype[np.complex128]
247 CSingle: TypeAlias = np.dtype[np.csingle]
248-SingleComplex: TypeAlias = np.dtype[np.singlecomplex]
249+SingleComplex: TypeAlias = np.dtype[np.complex64]
250 CDouble: TypeAlias = np.dtype[np.cdouble]
251-Complex: TypeAlias = np.dtype[np.complex_]
252-CFloat: TypeAlias = np.dtype[np.cfloat]
253+Complex: TypeAlias = np.dtype[np.complex128]
254+CFloat: TypeAlias = np.dtype[np.complex128]
255 CLongDouble: TypeAlias = np.dtype[np.clongdouble]
256-CLongFloat: TypeAlias = np.dtype[np.clongfloat]
257-LongComplex: TypeAlias = np.dtype[np.longcomplex]
258+CLongFloat: TypeAlias = np.dtype[np.clongdouble]
259+LongComplex: TypeAlias = np.dtype[np.clongdouble]
260 Flexible: TypeAlias = np.dtype[np.flexible]
261 Void: TypeAlias = np.dtype[np.void]
262 Void0: TypeAlias = np.dtype[np.void0]
263 Character: TypeAlias = np.dtype[np.character]
264 Bytes: TypeAlias = np.dtype[np.bytes_]
265 Str: TypeAlias = np.dtype[np.str_]
266-String: TypeAlias = np.dtype[np.string_]
267+String: TypeAlias = np.dtype[np.bytes_]
268 Bytes0: TypeAlias = np.dtype[np.bytes0]
269-Unicode: TypeAlias = np.dtype[np.unicode_]
270+Unicode: TypeAlias = np.dtype[np.str_]
271 Str0: TypeAlias = np.dtype[np.str0]
272
273 dtype_per_name: Dict[str, np.dtype[Any]]
274diff --git a/tests/test_ndarray.py b/tests/test_ndarray.py
275index f4f7676..c957e19 100644
276--- a/tests/test_ndarray.py
277+++ b/tests/test_ndarray.py
278@@ -264,7 +264,7 @@ def test_str(self):
279
280 def test_types_with_numpy_dtypes(self):
281 self.assertIsInstance(np.array([42]), NDArray[Any, np.int_])
282- self.assertIsInstance(np.array([42.0]), NDArray[Any, np.float_])
283+ self.assertIsInstance(np.array([42.0]), NDArray[Any, np.float64])
284 self.assertIsInstance(np.array([np.uint8(42)]), NDArray[Any, np.uint8])
285 self.assertIsInstance(np.array([True]), NDArray[Any, np.bool_])
286