Skip to content

Commit 0182c19

Browse files
petercorkeclaude
andcommitted
fix: Graph is concrete through its dispatch, not an abstract class
Graph(directed=...) returns a UGraph or DGraph, so it is no longer abstract: the abstractmethod made static checkers report that an abstract class was instantiated. _graphcolor in the base raises NotImplementedError like the other hooks, and a direct subclass that does not say its kind is a TypeError at construction. Also tests the module-level deprecated names and the base hooks, and removes an unused import. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent bec2ded commit 0182c19

3 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎src/pgraph/PGraph.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from abc import ABC, abstractmethod
43
import os
54
import sys
65
import warnings
@@ -46,7 +45,7 @@ def _SE2(v: NDArray) -> float:
4645
}
4746

4847

49-
class Graph(ABC):
48+
class Graph:
5049

5150
#: whether the edges of this kind of graph are directed, set by
5251
#: :class:`UGraph` and :class:`DGraph`
@@ -57,6 +56,11 @@ def __new__(cls, *args: Any, directed: bool | None = None, **kwargs: Any) -> Gra
5756
# underneath stay honest, so isinstance and the hooks work as usual
5857
if cls is Graph:
5958
cls = DGraph if directed else UGraph
59+
elif not hasattr(cls, "_directed"):
60+
raise TypeError(
61+
f"{cls.__name__} does not say whether its edges are directed; "
62+
"subclass UGraph or DGraph"
63+
)
6064
return super().__new__(cls)
6165

6266
def __init__(
@@ -1291,15 +1295,19 @@ def ne(self) -> int:
12911295
"""
12921296
return len(self._edges)
12931297

1294-
@abstractmethod
12951298
def _graphcolor(self) -> int | None:
12961299
"""
12971300
Color the graph (subclass method)
12981301
12991302
Concrete graph coloring algorithm, provided by :meth:`UGraph._graphcolor`
13001303
and :meth:`DGraph._graphcolor`.
13011304
1305+
:raises NotImplementedError: not provided by this graph kind
13021306
"""
1307+
raise NotImplementedError(
1308+
f"{type(self).__name__} does not define how to color the graph; "
1309+
"subclass UGraph or DGraph"
1310+
)
13031311

13041312
@property
13051313
def nc(self) -> int:

‎tests/test_detach_hook.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import unittest
99

10-
from pgraph import DGraph, Edge, Graph, UGraph, Vertex
10+
from pgraph import DGraph, Edge, Graph, UGraph
1111

1212

1313
class TestHookIsDefinedPerKind(unittest.TestCase):

‎tests/test_unified_graph.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,62 @@ def test_copy_and_pickle_keep_the_kind(self):
194194
self.assertEqual(h.validate(), [])
195195

196196

197+
class TestNotInstantiatedDirectly(unittest.TestCase):
198+
"""Graph is concrete only through the kind it dispatches to"""
199+
200+
def test_a_subclass_must_say_its_kind(self):
201+
class Vague(Graph):
202+
pass
203+
204+
with self.assertRaises(TypeError):
205+
Vague()
206+
207+
def test_a_subclass_of_a_kind_is_fine(self):
208+
class Mine(DGraph):
209+
pass
210+
211+
self.assertTrue(Mine().directed)
212+
self.assertTrue(Mine(directed=True).directed)
213+
214+
def test_the_base_hooks_say_which_kinds_provide_them(self):
215+
g = UGraph()
216+
a, b = g.add_vertex(name="a"), g.add_vertex(name="b")
217+
e = Edge(a, b)
218+
for call in (
219+
lambda: Graph._attach(g, e),
220+
lambda: Graph._detach(g, e),
221+
lambda: Graph._incoming(g, a),
222+
lambda: Graph._graphcolor(g),
223+
):
224+
with self.assertRaises(NotImplementedError):
225+
call()
226+
227+
def test_the_base_endpoint_test_is_directional(self):
228+
g = DGraph()
229+
a, b = g.add_vertex(name="a"), g.add_vertex(name="b")
230+
e = Edge(a, b)
231+
self.assertTrue(Graph._endpoints_match(g, e, a, b))
232+
self.assertFalse(Graph._endpoints_match(g, e, b, a))
233+
234+
235+
class TestModuleLevelDeprecatedNames(unittest.TestCase):
236+
"""The names are also reachable as attributes of pgraph.PGraph"""
237+
238+
def test_alias_in_the_module(self):
239+
import pgraph.PGraph as module
240+
241+
with self.assertWarns(DeprecationWarning):
242+
self.assertIs(module.UVertex, Vertex)
243+
with self.assertWarns(DeprecationWarning):
244+
self.assertIs(module.DVertex, Vertex)
245+
246+
def test_other_names_in_the_module_are_errors(self):
247+
import pgraph.PGraph as module
248+
249+
with self.assertRaises(AttributeError):
250+
module.NoSuchThing
251+
252+
197253
class TestEdgeArrow(unittest.TestCase):
198254
def test_arrow_follows_the_graph(self):
199255
for flag, arrow in ((False, "--"), (True, "->")):

0 commit comments

Comments
 (0)