Skip to content

Commit 5b7dc36

Browse files
authored
Merge pull request #2 from petercorke/deprecate/adjacent-to-neighbours
2 parents 43be830 + c29e3f3 commit 5b7dc36

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)