diff --git a/src/pgraph/PGraph.py b/src/pgraph/PGraph.py index 9f1c50da..a89dab33 100644 --- a/src/pgraph/PGraph.py +++ b/src/pgraph/PGraph.py @@ -1,5 +1,6 @@ from abc import ABC import sys +import warnings import numpy as np import matplotlib.pyplot as plt import copy @@ -1582,7 +1583,20 @@ def next(self, vertex): raise ValueError("shouldnt happen") def vertices(self): - raise DeprecationWarning("use endpoints instead") + """ + Vertices of an edge (deprecated) + + :return: the two vertices of this edge + :rtype: list of Vertex subclass + + .. deprecated:: use :attr:`endpoints` instead + """ + warnings.warn( + "vertices() is deprecated, use endpoints instead", + DeprecationWarning, + stacklevel=2, + ) + return self.endpoints @property def endpoints(self): @@ -1673,6 +1687,22 @@ def neighbors(self): """ return [e.next(self) for e in self._edgelist] + def adjacent(self): + """ + Neighbours of a vertex (deprecated) + + :return: a list of neighbours of this vertex + :rtype: list of Vertex subclass + + .. deprecated:: use :meth:`neighbours` instead + """ + warnings.warn( + "adjacent() is deprecated, use neighbours() instead", + DeprecationWarning, + stacklevel=2, + ) + return self.neighbours() + def isneighbour(self, vertex): """ Test if vertex is a neigbour diff --git a/tests/test_graph.py b/tests/test_graph.py index f0541b24..812ef9d0 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -151,6 +151,18 @@ def test_neighbours(self): self.assertFalse(v1.isneighbour(v3)) self.assertFalse(v3.isneighbour(v1)) + def test_adjacent_deprecated(self): + # adjacent() must still work (same result as neighbours()) but + # emit a DeprecationWarning, not a hard failure + g = UGraph() + v1 = g.add_vertex(name='v1') + v2 = g.add_vertex(name='v2') + v1.connect(v2) + + with self.assertWarns(DeprecationWarning): + n = v1.adjacent() + self.assertEqual(n, v1.neighbours()) + def test_getitem(self): g = UGraph() v1 = g.add_vertex(name='v1') @@ -282,6 +294,20 @@ def test_edge2(self): self.assertTrue(v3 in v1.neighbours()) self.assertFalse(v4 in v1.neighbours()) + def test_edge_vertices_deprecated(self): + # vertices() must still work (same result as endpoints) but emit + # a DeprecationWarning, not raise DeprecationWarning as a hard + # failure (raising it aborts the call exactly like a missing + # method would -- no backward compatibility at all) + g = UGraph() + v1 = g.add_vertex(name='v1') + v2 = g.add_vertex(name='v2') + e = v1.connect(v2) + + with self.assertWarns(DeprecationWarning): + verts = e.vertices() + self.assertEqual(verts, e.endpoints) + def test_edge3(self): g = UGraph()