Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/pgraph/PGraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BaseGraph(ABC):

def __init__(
self,
*,
metric: Callable[[NDArray], float] | str | None = None,
heuristic: Callable[[NDArray], float] | str | None = None,
verbose: bool = False,
Expand Down Expand Up @@ -60,6 +61,14 @@ def __init__(
This is the common base class of :class:`UGraph` and :class:`DGraph`
and should not be instantiated directly.

All arguments are keyword-only, so ``UGraph(dim=6)`` is correct and
``UGraph(6)`` raises :exc:`TypeError`.

.. versionchanged:: 1.0
Arguments are keyword-only. In 0.6.5 the first positional
argument was accepted and ignored, so ``UGraph(6)`` silently did
nothing.

``vertex_class`` and ``edge_class`` let user subclasses be built by the
graph, so they never need to be constructed by hand::

Expand Down
40 changes: 40 additions & 0 deletions tests/test_constructor_keywords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Graph constructor arguments are 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.
"""

import unittest

from pgraph import DGraph, UGraph


class _Keywords:
cls: type

def test_positional_rejected(self):
for arg in (6, "L2", None, True):
with self.subTest(arg=arg):
with self.assertRaises(TypeError):
self.cls(arg)

def test_keywords_accepted(self):
g = self.cls(dim=6, metric="L1", heuristic="L2", verbose=False)
self.assertEqual(g.n, 0)
with self.assertRaises(ValueError):
g.add_vertex([1, 2])

def test_no_arguments(self):
self.assertEqual(self.cls().n, 0)


class TestUGraphKeywords(_Keywords, unittest.TestCase):
cls = UGraph


class TestDGraphKeywords(_Keywords, unittest.TestCase):
cls = DGraph


if __name__ == "__main__":
unittest.main()
Loading