Skip to content

Commit c29e3f3

Browse files
petercorkeclaude
andcommitted
fix: deprecate adjacent()/vertices() properly instead of hard-breaking
UVertex.adjacent() had been removed outright with no migration path -- renamed to neighbours()/neighbors() with zero deprecation phase, so any caller still using the old name got a bare AttributeError. Restored it as a thin wrapper: still calls neighbours() and returns its result, but now emits a DeprecationWarning first, giving callers a working upgrade path instead of an immediate break. Also fixed the same underlying anti-pattern already present nearby in Edge.vertices(): it did `raise DeprecationWarning(...)`, which aborts the call exactly like a missing method would (DeprecationWarning is just an Exception subclass when raised directly) -- no backward compatibility at all, and it never goes through the actual warnings machinery (no -W flag control, no filterwarnings support). Now uses warnings.warn(..., DeprecationWarning, stacklevel=2) and still returns self.endpoints, matching the same real deprecation pattern as adjacent() above. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 13582c4 commit c29e3f3

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎src/pgraph/PGraph.py‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from abc import ABC
22
import sys
3+
import warnings
34
import numpy as np
45
import matplotlib.pyplot as plt
56
import copy
@@ -1582,7 +1583,20 @@ def next(self, vertex):
15821583
raise ValueError("shouldnt happen")
15831584

15841585
def vertices(self):
1585-
raise DeprecationWarning("use endpoints instead")
1586+
"""
1587+
Vertices of an edge (deprecated)
1588+
1589+
:return: the two vertices of this edge
1590+
:rtype: list of Vertex subclass
1591+
1592+
.. deprecated:: use :attr:`endpoints` instead
1593+
"""
1594+
warnings.warn(
1595+
"vertices() is deprecated, use endpoints instead",
1596+
DeprecationWarning,
1597+
stacklevel=2,
1598+
)
1599+
return self.endpoints
15861600

15871601
@property
15881602
def endpoints(self):
@@ -1673,6 +1687,22 @@ def neighbors(self):
16731687
"""
16741688
return [e.next(self) for e in self._edgelist]
16751689

1690+
def adjacent(self):
1691+
"""
1692+
Neighbours of a vertex (deprecated)
1693+
1694+
:return: a list of neighbours of this vertex
1695+
:rtype: list of Vertex subclass
1696+
1697+
.. deprecated:: use :meth:`neighbours` instead
1698+
"""
1699+
warnings.warn(
1700+
"adjacent() is deprecated, use neighbours() instead",
1701+
DeprecationWarning,
1702+
stacklevel=2,
1703+
)
1704+
return self.neighbours()
1705+
16761706
def isneighbour(self, vertex):
16771707
"""
16781708
Test if vertex is a neigbour

‎tests/test_graph.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ def test_neighbours(self):
151151
self.assertFalse(v1.isneighbour(v3))
152152
self.assertFalse(v3.isneighbour(v1))
153153

154+
def test_adjacent_deprecated(self):
155+
# adjacent() must still work (same result as neighbours()) but
156+
# emit a DeprecationWarning, not a hard failure
157+
g = UGraph()
158+
v1 = g.add_vertex(name='v1')
159+
v2 = g.add_vertex(name='v2')
160+
v1.connect(v2)
161+
162+
with self.assertWarns(DeprecationWarning):
163+
n = v1.adjacent()
164+
self.assertEqual(n, v1.neighbours())
165+
154166
def test_getitem(self):
155167
g = UGraph()
156168
v1 = g.add_vertex(name='v1')
@@ -282,6 +294,20 @@ def test_edge2(self):
282294
self.assertTrue(v3 in v1.neighbours())
283295
self.assertFalse(v4 in v1.neighbours())
284296

297+
def test_edge_vertices_deprecated(self):
298+
# vertices() must still work (same result as endpoints) but emit
299+
# a DeprecationWarning, not raise DeprecationWarning as a hard
300+
# failure (raising it aborts the call exactly like a missing
301+
# method would -- no backward compatibility at all)
302+
g = UGraph()
303+
v1 = g.add_vertex(name='v1')
304+
v2 = g.add_vertex(name='v2')
305+
e = v1.connect(v2)
306+
307+
with self.assertWarns(DeprecationWarning):
308+
verts = e.vertices()
309+
self.assertEqual(verts, e.endpoints)
310+
285311
def test_edge3(self):
286312

287313
g = UGraph()

0 commit comments

Comments
 (0)