Skip to content

feat: one Vertex class; Graph(directed=...) chooses the kind of graph - #47

Merged
petercorke merged 3 commits into
mainfrom
refactor/unified-graph-vertex
Oct 12, 2026
Merged

petercorke merged 3 commits into
mainfrom
refactor/unified-graph-vertex

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

There is one vertex class, Vertex, and Graph(directed=...) chooses the kind of graph.

UVertex and DVertex differed only in four private methods that recorded an edge at its ends, and that is the graph's decision, not the vertex's: a vertex has no direction of its own. Having the kind in both the graph and its vertices meant keeping them consistent, with checks (add_vertex's kind test, vertex_class's kind test) whose only job was to refuse the combinations the design allowed to be written.

  • The hooks _attach, _detach, _incoming and _endpoints_match move to the graph kinds: UGraph records an edge at both ends, DGraph as leaving its start and entering its end. Vertex.connect calls the graph's hook.
  • UVertex and DVertex remain as deprecated names for Vertex (module __getattr__, DeprecationWarning), so existing code that subclasses or constructs them runs. They are not in __all__.
  • Graph is concrete: Graph(directed=True) is a DGraph and Graph() a UGraph, so the kind can be a variable; Graph.Adjacency and Graph.Dict take directed= the same way. UGraph(directed=True) is a ValueError. isinstance, the hooks and pickling see the honest subclass.
  • Graph.directed reports the kind, replacing the isinstance tests in Adjacency, iscyclic, incidence, validate, the exporters and Edge.__str__.
  • vertex_class need only be a subclass of Vertex.

Behaviour changes (1.0)

  • A bare Vertex, which could not join any graph, now can; there is no longer a wrong-kind vertex for add_vertex to refuse.
  • An Edge between standalone vertices has no kind to show, so its str uses --, not ->.
  • Vertex no longer has _attach / _detach; code overriding those on a vertex moves the override to a UGraph / DGraph subclass (see test_vertex_hook.py::TestHookIsTheExtensionPoint).

Downstream

Checked against this branch with the deprecated aliases: MVTB's bundle-adjust tests (4) and RTB's mobile and planner tests (21) pass. RTB's PoseGraph / LatticePlanner and MVTB's ViewPoint / Landmark should change UVertex / DVertex to Vertex; they get a DeprecationWarning until then.

Tests

The hook tests exercise the graph hooks. New tests/test_unified_graph.py (28 tests): the dispatch, directed and its contradictions, one vertex class serving both kinds, the deprecated names (warn, subclass, not advertised, not in __all__), the kind-aware constructors, copy and pickle keeping the kind, the edge arrow.

Docs

Only what the new API makes wrong, or runnable with a warning, is changed (examples, policy.rst, README, the two removed vertex pages). The full documentation and diagram pass follows.

🤖 Generated with Claude Code

petercorke and others added 2 commits October 12, 2026 10:07
UVertex and DVertex differed only in four private methods that recorded an
edge at its ends, which is the graph's decision, not the vertex's: a vertex
has no direction of its own.  Having the kind in both the graph and its
vertices meant keeping them consistent, with checks (add_vertex's
kind test, vertex_class's kind test) whose only job was to refuse the
combinations the design allowed to be written.

- the hooks _attach, _detach, _incoming and _endpoints_match move to the
  graph kinds: UGraph records an edge at both ends, DGraph as leaving its
  start and entering its end.  Vertex.connect calls the graph's hook
- there is one vertex class, Vertex.  UVertex and DVertex remain as
  deprecated names for it (module __getattr__, DeprecationWarning), so
  existing code that subclasses or constructs them runs; they are not in
  __all__
- Graph is concrete: Graph(directed=True) is a DGraph and Graph() a UGraph,
  so the kind can be a variable; Graph.Adjacency and Graph.Dict take
  directed= the same way.  UGraph(directed=True) is a ValueError.
  isinstance, the hooks and pickling see the honest subclass
- Graph.directed reports the kind, replacing the isinstance tests in
  Adjacency, iscyclic, incidence, validate, the exporters and Edge.__str__
- vertex_class need only be a subclass of Vertex

Behaviour change: add_vertex no longer has a wrong-kind vertex to refuse, and
a bare Vertex, which could not join any graph, now can.  An Edge between
standalone vertices has no kind to show, so its str uses -- not ->.

Tests: the hook tests exercise the graph hooks; test_unified_graph covers the
dispatch, the aliases and the kind-aware constructors.  Downstream, MVTB's
bundle-adjust tests and RTB's mobile and planner tests pass through the
aliases.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The examples, the policy page and the README use Vertex, the pages for
UVertex and DVertex are removed, and the common page says that a vertex
has no direction of its own.  Only what the new API makes wrong or
runnable-with-a-warning is changed; the full documentation and diagram pass
follows.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codacy-production

codacy-production Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 48 complexity · -2 duplication

Metric Results
Complexity 48
Duplication -2

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.50000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.27%. Comparing base (c57db9a) to head (0182c19).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
src/pgraph/PGraph.py 97.22% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #47      +/-   ##
==========================================
+ Coverage   94.79%   95.27%   +0.48%     
==========================================
  Files           3        3              
  Lines        1249     1271      +22     
==========================================
+ Hits         1184     1211      +27     
+ Misses         65       60       -5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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>
@petercorke
petercorke merged commit 1a424e6 into main Oct 12, 2026
12 checks passed
@petercorke
petercorke deleted the refactor/unified-graph-vertex branch October 12, 2026 00:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant