Skip to content

Commit 3670a47

Browse files
petercorkeclaude
andcommitted
fix: plot()/highlight_edge() crashes that mypy flagged; clear mypy to zero
mypy's 7 baseline errors were real runtime bugs, not noise: - plot(text=None) and plot(text=True) raised TypeError ('** must be a mapping') although the docstring says None means default formatting - plot() on a graph without coordinates raised 'NoneType has no len()', and on an empty graph IndexError; both are now a clear ValueError - in 3D with colorcomponents=False the vertex marker was drawn at z=0 - highlight_edge() on a removed edge raised AttributeError; now ValueError - vopt/eopt/text had mutable {} defaults; now None - block is documented and used as bool|None (None = don't show) Also drops python_version=3.9 from [tool.mypy], which current mypy rejects. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 15994d2 commit 3670a47

2 files changed

Lines changed: 57 additions & 23 deletions

File tree

‎pyproject.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ target-version = ['py311']
7676
omit = []
7777

7878
[tool.mypy]
79-
python_version = "3.9"
8079
ignore_missing_imports = true
8180
warn_unused_ignores = true
8281
warn_redundant_casts = true

‎src/pgraph/PGraph.py‎

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -990,10 +990,10 @@ def plot(
990990
self,
991991
colorcomponents: bool = True,
992992
force2d: bool = False,
993-
vopt: dict = {},
994-
eopt: dict = {},
995-
text: dict | bool = {},
996-
block: bool = False,
993+
vopt: dict | None = None,
994+
eopt: dict | None = None,
995+
text: dict | bool | None = None,
996+
block: bool | None = False,
997997
grid: bool = True,
998998
ax: Any = None,
999999
) -> None:
@@ -1002,23 +1002,34 @@ def plot(
10021002
10031003
:param vopt: vertex format, defaults to 12pt o-marker
10041004
:type vopt: dict, optional
1005-
:param eopt: edge format, defaults to None
1005+
:param eopt: edge format, defaults to 3pt line width
10061006
:type eopt: dict, optional
10071007
:param text: text label format, defaults to None
1008-
:type text: False or dict, optional
1009-
:param colorcomponents: color vertices and edges by component, defaults to None
1010-
:type color: bool, optional
1011-
:param block: block until figure is dismissed, defaults to True
1012-
:type block: bool, optional
1008+
:type text: dict, bool or None, optional
1009+
:param colorcomponents: color vertices and edges by component, defaults to True
1010+
:type colorcomponents: bool, optional
1011+
:param force2d: plot only the first two coordinates of each vertex
1012+
even if they have more, defaults to False
1013+
:type force2d: bool, optional
1014+
:param block: block until figure is dismissed, defaults to False. If
1015+
None the figure is not shown at all, which is useful when
1016+
adding to the plot or saving it.
1017+
:type block: bool or None, optional
1018+
:raises ValueError: the graph is empty, or a vertex has no coordinate
1019+
or fewer than two coordinate values
10131020
10141021
The graph is plotted using matplotlib.
10151022
10161023
If ``colorcomponents`` is True then each component is assigned a unique
10171024
color. ``vertex`` and ``edge`` cannot include a color keyword.
10181025
10191026
If ``text`` is a dict it is used to format text labels for the vertices
1020-
which are the vertex names. If ``text`` is None default formatting is
1021-
used. If ``text`` is False no labels are added.
1027+
which are the vertex names. If ``text`` is None or True default
1028+
formatting is used. If ``text`` is False no labels are added.
1029+
1030+
.. note:: Only embedded graphs can be plotted: every vertex must have
1031+
a coordinate of at least two values. A coordinate of more than
1032+
two values is drawn in 3D, using the first three.
10221033
10231034
.. runblock:: pycon
10241035
@@ -1040,21 +1051,37 @@ def plot(
10401051
10411052
:seealso: :meth:`highlight_path`
10421053
"""
1043-
vopt = {**dict(marker="o", markersize=12), **vopt}
1044-
eopt = {**dict(linewidth=3), **eopt}
1054+
if self.n == 0:
1055+
raise ValueError("cannot plot an empty graph")
1056+
ndim = 0 # number of coordinate values, taken from the first vertex
1057+
for i, vertex in enumerate(self):
1058+
if vertex.coord is None or len(vertex.coord) < 2:
1059+
raise ValueError(
1060+
f"cannot plot: vertex {vertex.name} needs a coordinate "
1061+
"of at least two values"
1062+
)
1063+
if i == 0:
1064+
ndim = len(vertex.coord)
1065+
1066+
vopt = {**dict(marker="o", markersize=12), **(vopt or {})}
1067+
eopt = {**dict(linewidth=3), **(eopt or {})}
1068+
# text labels: None or True is default formatting, False is no labels
1069+
textopt: dict | None = None
1070+
if text is not False:
1071+
textopt = text if isinstance(text, dict) else {}
10451072

10461073
if colorcomponents:
10471074
color = plt.cm.coolwarm(np.linspace(0, 1, self.nc))
10481075

1049-
if len(self[0].coord) == 2 or force2d:
1076+
if ndim == 2 or force2d:
10501077
# 2D plotting
10511078
if ax is None:
10521079
ax = axes_logic(ax, 2)
10531080
for c in range(self.nc):
10541081
# for each component
10551082
for vertex in self.component(c):
1056-
if text is not False:
1057-
ax.text(vertex.x, vertex.y, " " + vertex.name, **text)
1083+
if textopt is not None:
1084+
ax.text(vertex.x, vertex.y, f" {vertex.name}", **textopt)
10581085
if colorcomponents:
10591086
ax.plot(vertex.x, vertex.y, color=color[c, :], **vopt)
10601087
for v in vertex.neighbours():
@@ -1075,9 +1102,9 @@ def plot(
10751102
for c in range(self.nc):
10761103
# for each component
10771104
for vertex in self.component(c):
1078-
if text is not False:
1105+
if textopt is not None:
10791106
ax.text(
1080-
vertex.x, vertex.y, vertex.z, " " + vertex.name, **text
1107+
vertex.x, vertex.y, vertex.z, f" {vertex.name}", **textopt
10811108
)
10821109
if colorcomponents:
10831110
ax.plot(
@@ -1094,7 +1121,7 @@ def plot(
10941121
**{**dict(color=color[c, :]), **eopt},
10951122
)
10961123
else:
1097-
ax.plot(vertex.x, vertex.y, **vopt)
1124+
ax.plot(vertex.x, vertex.y, vertex.z, **vopt)
10981125
for v in vertex.neighbours():
10991126
ax.plot(
11001127
[vertex.x, v.x],
@@ -1109,13 +1136,17 @@ def plot(
11091136
if block is not None:
11101137
plt.show(block=block)
11111138

1112-
def highlight_path(self, path: list[BaseVertex], block: bool = False, **kwargs: Any) -> None:
1139+
def highlight_path(
1140+
self, path: list[BaseVertex], block: bool | None = False, **kwargs: Any
1141+
) -> None:
11131142
"""
11141143
Highlight a path through the graph
11151144
11161145
:param path: sequence of vertices forming a path
11171146
:type path: list of BaseVertex subclass
1118-
:param block: block until figure is dismissed, defaults to False
1147+
:param block: block until figure is dismissed, defaults to False. If
1148+
None the figure is not shown.
1149+
:type block: bool or None, optional
11191150
:param kwargs: arguments passed to :meth:`highlight_edge` and
11201151
:meth:`highlight_vertex`
11211152
@@ -1171,6 +1202,8 @@ def highlight_edge(
11711202
:type color: str, optional
11721203
:param alpha: Transparency of the highlight, defaults to 0.5
11731204
:type alpha: float, optional
1205+
:raises ValueError: ``edge`` does not connect two vertices, for
1206+
example because it has been removed from the graph
11741207
:rtype: None
11751208
11761209
.. runblock:: pycon
@@ -1199,6 +1232,8 @@ def highlight_edge(
11991232
"""
12001233
p1 = edge.v1
12011234
p2 = edge.v2
1235+
if p1 is None or p2 is None:
1236+
raise ValueError("edge does not connect two vertices")
12021237
plt.plot(
12031238
[p1.x, p2.x], [p1.y, p2.y], color=color, linewidth=3 * scale, alpha=alpha
12041239
)

0 commit comments

Comments
 (0)