Skip to content

Commit 9059faa

Browse files
petercorkeclaude
andcommitted
fix: CI typing and Codacy findings for the pickle work
The built-in metrics return float(...) so that mypy, against the CI's numpy, sees a float rather than floating[Any]. The tests pickle their own graphs, so the pickle calls go through one marked helper pair. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent bf56932 commit 9059faa

2 files changed

Lines changed: 29 additions & 21 deletions

File tree

‎src/pgraph/PGraph.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
def _L1(v: NDArray) -> float:
2020
"""L1 (Manhattan) norm, the built-in ``"L1"`` metric"""
21-
return np.linalg.norm(v, 1)
21+
return float(np.linalg.norm(v, 1))
2222

2323

2424
def _L2(v: NDArray) -> float:
2525
"""L2 (Euclidean) norm, the built-in ``"L2"`` metric"""
26-
return np.linalg.norm(v)
26+
return float(np.linalg.norm(v))
2727

2828

2929
def _SE2(v: NDArray) -> float:
@@ -34,7 +34,7 @@ def _SE2(v: NDArray) -> float:
3434
)
3535
# wrap angle to range [-pi, pi)
3636
v[2] = (v[2] + np.pi) % (2 * np.pi) - np.pi
37-
return np.linalg.norm(v)
37+
return float(np.linalg.norm(v))
3838

3939

4040
#: the built-in metrics by name. They are module level, and pickled by name,

‎tests/test_pickle.py‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@
77
"""
88

99
import copy
10-
import pickle
10+
import pickle # nosec B403 nosemgrep - the tests pickle their own graphs
1111
import unittest
1212

1313
import numpy as np
1414

1515
from pgraph import DGraph, DVertex, Edge, UGraph, UVertex
1616

1717

18+
def dumps(obj, protocol=None):
19+
return pickle.dumps(obj, protocol) # nosec nosemgrep
20+
21+
22+
def loads(data):
23+
return pickle.loads(data) # nosec B301 nosemgrep
24+
25+
1826
def manhattan(v):
1927
return float(np.abs(v).sum())
2028

@@ -92,13 +100,13 @@ def check(self, g, h):
92100

93101
def test_pickle(self):
94102
g = self.make()
95-
self.check(g, pickle.loads(pickle.dumps(g)))
103+
self.check(g, loads(dumps(g)))
96104

97105
def test_pickle_every_protocol(self):
98106
g = self.make()
99107
for protocol in range(2, pickle.HIGHEST_PROTOCOL + 1):
100108
with self.subTest(protocol=protocol):
101-
self.check(g, pickle.loads(pickle.dumps(g, protocol)))
109+
self.check(g, loads(dumps(g, protocol)))
102110

103111
def test_copy(self):
104112
g = self.make()
@@ -119,7 +127,7 @@ def test_copy_is_independent(self):
119127
self.assertEqual(h.validate(), [])
120128

121129
def test_restored_graph_is_usable(self):
122-
h = pickle.loads(pickle.dumps(self.make()))
130+
h = loads(dumps(self.make()))
123131
e = h.add_edge(h["d"], h["c"])
124132
self.assertEqual(h.ne, self.make().ne + 1)
125133
h.remove_edge(e)
@@ -131,7 +139,7 @@ def test_restored_graph_is_usable(self):
131139

132140
def test_settings_survive(self):
133141
g = self.cls(metric="L1", heuristic="SE2", dim=3, verbose=False)
134-
h = pickle.loads(pickle.dumps(g))
142+
h = loads(dumps(g))
135143
self.assertEqual(h.metric([3, -4, 0]), 7)
136144
with self.assertRaises(ValueError):
137145
h.add_vertex([1, 2]) # dim is still 3
@@ -141,54 +149,54 @@ def test_builtin_metrics_pickle_by_name(self):
141149
for name, value in (("L1", 7.0), ("L2", 5.0)):
142150
with self.subTest(metric=name):
143151
g = self.cls(metric=name, heuristic=name)
144-
h = pickle.loads(pickle.dumps(g))
152+
h = loads(dumps(g))
145153
self.assertEqual(h.metric(np.r_[3.0, -4.0]), value)
146154
self.assertIs(h.metric, g.metric)
147155
self.assertIs(h.heuristic, g.heuristic)
148156

149157
def test_default_metric_pickles(self):
150158
g = self.cls()
151-
h = pickle.loads(pickle.dumps(g))
159+
h = loads(dumps(g))
152160
self.assertAlmostEqual(h.metric(np.r_[3.0, 4.0]), 5.0)
153161

154162
def test_se2_metric_pickles(self):
155163
g = self.cls(metric="SE2")
156-
h = pickle.loads(pickle.dumps(g))
164+
h = loads(dumps(g))
157165
self.assertIs(h.metric, g.metric)
158166

159167
def test_user_module_level_metric(self):
160168
g = self.cls(metric=manhattan)
161-
h = pickle.loads(pickle.dumps(g))
169+
h = loads(dumps(g))
162170
self.assertIs(h.metric, manhattan)
163171

164172
def test_lambda_metric_cannot_be_pickled_but_can_be_copied(self):
165173
g = self.cls(metric=lambda v: 1.0)
166174
with self.assertRaises((pickle.PicklingError, AttributeError)):
167-
pickle.dumps(g)
175+
dumps(g)
168176
self.assertEqual(g.copy().metric(np.r_[1.0]), 1.0)
169177

170178
def test_empty_graph(self):
171179
g = self.cls()
172-
h = pickle.loads(pickle.dumps(g))
180+
h = loads(dumps(g))
173181
self.assertEqual((h.n, h.ne), (0, 0))
174182

175183
def test_long_chain_pickles_and_copies(self):
176184
g = chain(5000, self.cls)
177-
for h in (pickle.loads(pickle.dumps(g)), g.copy(), copy.deepcopy(g)):
185+
for h in (loads(dumps(g)), g.copy(), copy.deepcopy(g)):
178186
self.assertEqual((h.n, h.ne), (5000, 4999))
179187
self.assertEqual(h[2500].adjacent()[0].name, g[2500].adjacent()[0].name)
180188
self.assertEqual(h.validate(), [])
181189

182190
def test_vertex_alone_is_refused(self):
183191
g = self.make()
184-
for f in (pickle.dumps, copy.deepcopy, copy.copy):
192+
for f in (dumps, copy.deepcopy, copy.copy):
185193
with self.subTest(f=f.__name__):
186194
with self.assertRaises(TypeError):
187195
f(g["a"])
188196

189197
def test_edge_alone_is_refused(self):
190198
g = self.make()
191-
for f in (pickle.dumps, copy.deepcopy, copy.copy):
199+
for f in (dumps, copy.deepcopy, copy.copy):
192200
with self.subTest(f=f.__name__):
193201
with self.assertRaises(TypeError):
194202
f(g.edges()[0])
@@ -204,14 +212,14 @@ def test_data_referring_to_a_vertex_is_refused(self):
204212
g = self.make()
205213
g["a"].data = g["b"]
206214
with self.assertRaises(TypeError):
207-
pickle.dumps(g)
215+
dumps(g)
208216

209217

210218
class TestUGraphPickle(_RoundTrip, unittest.TestCase):
211219
cls = UGraph
212220

213221
def test_loop_listed_twice_at_vertex(self):
214-
h = pickle.loads(pickle.dumps(self.make()))
222+
h = loads(dumps(self.make()))
215223
loop = [e for e in h.edges() if e.v1 is e.v2][0]
216224
self.assertEqual(h["c"].edges().count(loop), 2)
217225

@@ -221,7 +229,7 @@ class TestDGraphPickle(_RoundTrip, unittest.TestCase):
221229

222230
def test_in_edge_order_survives(self):
223231
g = self.make()
224-
h = pickle.loads(pickle.dumps(g))
232+
h = loads(dumps(g))
225233
for v in g:
226234
self.assertEqual(
227235
[(e.v1.name, e.v2.name, e.cost) for e in v.in_edges()],
@@ -239,7 +247,7 @@ def test_vertex_and_edge_classes(self):
239247
a.tag, b.tag = "first", "second"
240248
e = g.add_edge(a, b)
241249
e.weight = 11
242-
for h in (pickle.loads(pickle.dumps(g)), g.copy()):
250+
for h in (loads(dumps(g)), g.copy()):
243251
self.assertIsInstance(h["a"], vcls)
244252
self.assertEqual((h["a"].tag, h["b"].tag), ("first", "second"))
245253
self.assertIsInstance(h.edges()[0], WeightedEdge)

0 commit comments

Comments
 (0)