Skip to content

Commit d8f7911

Browse files
petercorkeclaude
andcommitted
fix: Edge.v1 and Edge.v2 are read-only
They were plain attributes, so e.v1 = None on an edge that was in a graph left it wedged: remove_edge and remove_vertex raised, incidence() hit an assert, and the edge could not be removed. Assigning to one of them to another vertex left the edge disagreeing with the vertices' edge lists. Only the library binds an edge to its vertices -- the constructor, connect(), and remove_edge() -- so they are now properties over private fields, set through one method, and assigning raises AttributeError saying to use the constructor or connect() instead. An edge that is in a graph always has both of its ends. The downstream Edge subclasses (MVTB Observation, RTB LatticeEdge and PGEdge) all call super().__init__(v1, v2), and nothing in RTB, MVTB, RVC3 or spatialmath assigns an endpoint. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 190b21e commit d8f7911

1 file changed

Lines changed: 65 additions & 10 deletions

File tree

‎src/pgraph/PGraph.py‎

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,7 @@ def remove_edge(self, edge: Edge) -> None:
642642
v2._connectivitychange = True
643643
self._connectivitychange = True
644644

645-
edge.v1 = None
646-
edge.v2 = None
645+
edge._set_endpoints(None, None)
647646

648647
self._edgelist.remove(edge)
649648

@@ -2629,6 +2628,12 @@ def _graphcolor(self) -> int | None:
26292628
# ========================================================================== #
26302629

26312630

2631+
_ENDPOINT_MESSAGE = (
2632+
"Edge.{0} is read-only. Give the vertices to the constructor, Edge(v1, v2), "
2633+
"or connect the edge with v1.connect(v2, edge=e) or e.connect(v1, v2)"
2634+
)
2635+
2636+
26322637
class Edge:
26332638
"""
26342639
Edge class
@@ -2638,8 +2643,8 @@ class Edge:
26382643
Each edge has:
26392644
- ``cost`` cost of traversing this edge, required for planning methods
26402645
- ``data`` reference to arbitrary data associated with the edge
2641-
- ``v1`` first vertex, start vertex for a directed edge
2642-
- ``v2`` second vertex, end vertex for a directed edge
2646+
- ``v1`` first vertex, start vertex for a directed edge (read-only)
2647+
- ``v2`` second vertex, end vertex for a directed edge (read-only)
26432648
26442649
.. note::
26452650
@@ -2696,8 +2701,8 @@ def __init__(
26962701
26972702
:seealso: :meth:`Edge.connect` :meth:`BaseVertex.connect`
26982703
"""
2699-
self.v1 = v1
2700-
self.v2 = v2
2704+
self._v1 = v1
2705+
self._v2 = v2
27012706

27022707
self.data = data
27032708

@@ -2717,6 +2722,58 @@ def __init__(
27172722
else:
27182723
self.cost = None
27192724

2725+
@property
2726+
def v1(self) -> BaseVertex | None:
2727+
"""
2728+
First vertex of the edge, the start vertex for a directed edge
2729+
2730+
:return: the vertex, or None if the edge is not connected
2731+
:rtype: BaseVertex subclass or None
2732+
2733+
.. versionchanged:: 1.0
2734+
Read-only. It was a plain attribute, so assigning to it on an edge
2735+
that was in a graph left the graph inconsistent: the vertices'
2736+
edge lists no longer agreed with the edge, and the edge could not
2737+
be removed. Give the vertices to the constructor, ``Edge(v1,
2738+
v2)``, or to :meth:`connect` or :meth:`BaseVertex.connect`.
2739+
2740+
:seealso: :attr:`v2` :meth:`connect`
2741+
"""
2742+
return self._v1
2743+
2744+
@v1.setter
2745+
def v1(self, value: BaseVertex | None) -> None:
2746+
raise AttributeError(_ENDPOINT_MESSAGE.format("v1"))
2747+
2748+
@property
2749+
def v2(self) -> BaseVertex | None:
2750+
"""
2751+
Second vertex of the edge, the end vertex for a directed edge
2752+
2753+
:return: the vertex, or None if the edge is not connected
2754+
:rtype: BaseVertex subclass or None
2755+
2756+
Read-only, see :attr:`v1`.
2757+
2758+
:seealso: :attr:`v1` :meth:`connect`
2759+
"""
2760+
return self._v2
2761+
2762+
@v2.setter
2763+
def v2(self, value: BaseVertex | None) -> None:
2764+
raise AttributeError(_ENDPOINT_MESSAGE.format("v2"))
2765+
2766+
def _set_endpoints(self, v1: BaseVertex | None, v2: BaseVertex | None) -> None:
2767+
"""
2768+
Bind the edge to its vertices, or unbind it (private method)
2769+
2770+
Only the library calls this: the constructor's own assignment, then
2771+
:meth:`connect`, :meth:`BaseVertex.connect` for an edge built without
2772+
vertices, and :meth:`_BaseGraph.remove_edge`, which clears them.
2773+
"""
2774+
self._v1 = v1
2775+
self._v2 = v2
2776+
27202777
def __repr__(self) -> str:
27212778
"""
27222779
Detailed representation of the edge
@@ -2804,8 +2861,7 @@ def connect(self, v1: BaseVertex, v2: BaseVertex) -> None:
28042861
raise ValueError("vertices must belong to the same graph")
28052862

28062863
# connect edge to its vertices
2807-
self.v1 = v1
2808-
self.v2 = v2
2864+
self._set_endpoints(v1, v2)
28092865

28102866
# tell the vertices to add edge to their edgelists as appropriate for
28112867
# DGraph or UGraph
@@ -3279,8 +3335,7 @@ def _bind_edge(self, edge: Edge, dest: BaseVertex) -> None:
32793335
endpoints disagree with the vertices' edge lists.
32803336
"""
32813337
if edge.v1 is None and edge.v2 is None:
3282-
edge.v1 = self
3283-
edge.v2 = dest
3338+
edge._set_endpoints(self, dest)
32843339
elif not self._endpoints_match(edge, dest):
32853340
raise ValueError(
32863341
f"edge already connects {edge.v1} and {edge.v2}, "

0 commit comments

Comments
 (0)