Skip to content

feat: UGraph/DGraph take vertex_class and edge_class - #31

Merged
petercorke merged 4 commits into
mainfrom
feat/graph-vertex-edge-class
Oct 11, 2026
Merged

petercorke merged 4 commits into
mainfrom
feat/graph-vertex-edge-class

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Stacked on #30 (fix/connect-edge-endpoints): the base of this PR is that branch, so the diff shows only this change. It retargets to main when #30 merges.

Summary

A graph now owns the policy of which vertex and edge classes it builds, so user subclasses never have to be constructed by hand and passed in:

class MyVertex(UVertex): ...
class MyEdge(Edge): ...

g = UGraph(vertex_class=MyVertex, edge_class=MyEdge)
v1 = g.add_vertex([0, 0])      # a MyVertex
v2 = g.add_vertex([1, 1])
e = g.add_edge(v1, v2)         # a MyEdge, cost computed as usual

Defaults are unchanged (UVertex/DVertex and Edge), so existing code is unaffected.

Behavior

  • vertex_class must subclass the graph's own vertex kind (UVertex for UGraph, DVertex for DGraph); edge_class must subclass Edge. Otherwise the constructor raises TypeError.
  • add_vertex(v) only accepts instances of the graph's vertex_class; connect(edge=e) only accepts instances of its edge_class (TypeError otherwise). For a default graph this still admits any UVertex/DVertex/Edge subclass.
  • Exposed as read-only g.vertex_class / g.edge_class; preserved by copy().
  • UGraph.Dict() and UGraph.Adjacency() forward extra keyword arguments to the constructor.
  • The classes are called as vertex_class(coord, name=name) and edge_class(v1, v2, cost=cost, data=data); this contract is in the constructor docstring.

For review

The strictness is the part most worth a second look: a graph configured with vertex_class=MyVertex rejects a plain UVertex. That is the point of configuring it (the type of g[i] can be relied on), but it is a choice.

Docstrings for UGraph, DGraph, Edge, connect() and the factories are updated, including the exceptions.

Tests

tests/test_graph_classes.py, 28 tests across UGraph and DGraph. mypy: no new errors.

🤖 Generated with Claude Code

petercorke and others added 3 commits October 11, 2026 10:59
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A graph now owns the policy of which vertex and edge classes it builds, so
user subclasses no longer have to be constructed by hand and passed in:

    g = UGraph(vertex_class=MyVertex, edge_class=MyEdge)
    g.add_vertex([0, 0])    # MyVertex
    g.add_edge(v1, v2)      # MyEdge

add_vertex() and connect(edge=) enforce the configured classes; the
Dict() and Adjacency() factories forward constructor arguments; copy()
preserves them. Defaults are unchanged (UVertex/DVertex and Edge).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…, DGraph, factories

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

codacy-production Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 35 complexity · 0 duplication

Metric Results
Complexity 35
Duplication 0

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.

Base automatically changed from fix/connect-edge-endpoints to main October 11, 2026 03:57
@codecov

codecov Bot commented Oct 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.31%. Comparing base (53890d1) to head (fbb258d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #31      +/-   ##
==========================================
+ Coverage   89.37%   90.31%   +0.94%     
==========================================
  Files           2        2              
  Lines         828      847      +19     
==========================================
+ Hits          740      765      +25     
+ Misses         88       82       -6     

☔ 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.

@petercorke
petercorke merged commit 3b203d9 into main Oct 11, 2026
14 checks passed
@petercorke
petercorke deleted the feat/graph-vertex-edge-class branch October 11, 2026 04:11
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