Skip to content

Commit 5a7c064

Browse files
authored
pyquaternion: Patch for numpy2 support (#375540)
2 parents 3a0a866 + c109417 commit 5a7c064

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

pkgs/development/python-modules/pyquaternion/default.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ buildPythonPackage rec {
1919
hash = "sha256-L0wT9DFUDRcmmN7OpmIDNvtQWQrM7iFnZt6R2xrJ+3A=";
2020
};
2121

22+
patches = [
23+
./numpy2-repr.patch
24+
];
25+
2226
# The VERSION.txt file is required for setup.py
2327
# See: https://github.com/KieranWynn/pyquaternion/blob/master/setup.py#L14-L15
2428
postPatch = ''
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
diff --git a/pyquaternion/test/test_quaternion.py b/pyquaternion/test/test_quaternion.py
2+
index f56afff..7178b52 100644
3+
--- a/pyquaternion/test/test_quaternion.py
4+
+++ b/pyquaternion/test/test_quaternion.py
5+
@@ -50,6 +50,16 @@ ALMOST_EQUAL_TOLERANCE = 13
6+
def randomElements():
7+
return tuple(np.random.uniform(-1, 1, 4))
8+
9+
+# In numpy 2, repr(np.float64(0.123)) becomes "np.float64(0.123)"
10+
+# which means it's not directly a parseable float. In numpy 1 that
11+
+# used to be the case. This hack papers over that
12+
+def repr_np(x):
13+
+ has_item = hasattr(x, 'item')
14+
+ if isinstance(x, np.generic) and has_item:
15+
+ return repr(x.item())
16+
+ else:
17+
+ return repr(x)
18+
+
19+
class TestQuaternionInitialisation(unittest.TestCase):
20+
21+
def test_init_default(self):
22+
@@ -77,7 +87,7 @@ class TestQuaternionInitialisation(unittest.TestCase):
23+
def test_init_from_scalar(self):
24+
s = random()
25+
q1 = Quaternion(s)
26+
- q2 = Quaternion(repr(s))
27+
+ q2 = Quaternion(repr_np(s))
28+
self.assertIsInstance(q1, Quaternion)
29+
self.assertIsInstance(q2, Quaternion)
30+
self.assertEqual(q1, Quaternion(s, 0.0, 0.0, 0.0))
31+
@@ -90,8 +100,8 @@ class TestQuaternionInitialisation(unittest.TestCase):
32+
def test_init_from_elements(self):
33+
a, b, c, d = randomElements()
34+
q1 = Quaternion(a, b, c, d)
35+
- q2 = Quaternion(repr(a), repr(b), repr(c), repr(d))
36+
- q3 = Quaternion(a, repr(b), c, d)
37+
+ q2 = Quaternion(repr_np(a), repr_np(b), repr_np(c), repr_np(d))
38+
+ q3 = Quaternion(a, repr_np(b), c, d)
39+
self.assertIsInstance(q1, Quaternion)
40+
self.assertIsInstance(q2, Quaternion)
41+
self.assertIsInstance(q3, Quaternion)
42+
@@ -154,7 +164,7 @@ class TestQuaternionInitialisation(unittest.TestCase):
43+
def test_init_from_explicit_elements(self):
44+
e1, e2, e3, e4 = randomElements()
45+
q1 = Quaternion(w=e1, x=e2, y=e3, z=e4)
46+
- q2 = Quaternion(a=e1, b=repr(e2), c=e3, d=e4)
47+
+ q2 = Quaternion(a=e1, b=repr_np(e2), c=e3, d=e4)
48+
q3 = Quaternion(a=e1, i=e2, j=e3, k=e4)
49+
q4 = Quaternion(a=e1)
50+
self.assertIsInstance(q1, Quaternion)
51+
@@ -525,7 +535,7 @@ class TestQuaternionArithmetic(unittest.TestCase):
52+
q3 = q1
53+
self.assertEqual(q1 * s, q2) # post-multiply by scalar
54+
self.assertEqual(s * q1, q2) # pre-multiply by scalar
55+
- q3 *= repr(s)
56+
+ q3 *= repr_np(s)
57+
self.assertEqual(q3, q2)
58+
59+
def test_multiply_incorrect_type(self):
60+
@@ -595,7 +605,7 @@ class TestQuaternionArithmetic(unittest.TestCase):
61+
with self.assertRaises(ZeroDivisionError):
62+
s / q1
63+
64+
- q3 /= repr(s)
65+
+ q3 /= repr_np(s)
66+
self.assertEqual(q3, q2)
67+
68+
with self.assertRaises(ZeroDivisionError):

0 commit comments

Comments
 (0)