Skip to content

Commit 31fb30d

Browse files
petercorkeclaude
andcommitted
refactor: make graph constructor arguments keyword-only
In 0.6.5 the first positional argument was accepted and ignored, so UGraph(6) looked as though it set a dimension but did nothing. With the current signature it would land in metric. Making every argument keyword-only turns UGraph(6) into a clear TypeError. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent e68d477 commit 31fb30d

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎src/pgraph/PGraph.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class BaseGraph(ABC):
2525

2626
def __init__(
2727
self,
28+
*,
2829
metric: Callable[[NDArray], float] | str | None = None,
2930
heuristic: Callable[[NDArray], float] | str | None = None,
3031
verbose: bool = False,
@@ -60,6 +61,14 @@ def __init__(
6061
This is the common base class of :class:`UGraph` and :class:`DGraph`
6162
and should not be instantiated directly.
6263
64+
All arguments are keyword-only, so ``UGraph(dim=6)`` is correct and
65+
``UGraph(6)`` raises :exc:`TypeError`.
66+
67+
.. versionchanged:: 1.0
68+
Arguments are keyword-only. In 0.6.5 the first positional
69+
argument was accepted and ignored, so ``UGraph(6)`` silently did
70+
nothing.
71+
6372
``vertex_class`` and ``edge_class`` let user subclasses be built by the
6473
graph, so they never need to be constructed by hand::
6574

‎tests/test_constructor_keywords.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Graph constructor arguments are keyword-only.
2+
3+
In 0.6.5 the first positional argument was accepted and ignored, so
4+
``UGraph(6)`` looked as though it set a dimension but did nothing.
5+
"""
6+
7+
import unittest
8+
9+
from pgraph import DGraph, UGraph
10+
11+
12+
class _Keywords:
13+
cls = None
14+
15+
def test_positional_rejected(self):
16+
for arg in (6, "L2", None, True):
17+
with self.subTest(arg=arg):
18+
with self.assertRaises(TypeError):
19+
self.cls(arg)
20+
21+
def test_keywords_accepted(self):
22+
g = self.cls(dim=6, metric="L1", heuristic="L2", verbose=False)
23+
self.assertEqual(g.n, 0)
24+
with self.assertRaises(ValueError):
25+
g.add_vertex([1, 2])
26+
27+
def test_no_arguments(self):
28+
self.assertEqual(self.cls().n, 0)
29+
30+
31+
class TestUGraphKeywords(_Keywords, unittest.TestCase):
32+
cls = UGraph
33+
34+
35+
class TestDGraphKeywords(_Keywords, unittest.TestCase):
36+
cls = DGraph
37+
38+
39+
if __name__ == "__main__":
40+
unittest.main()

0 commit comments

Comments
 (0)