Identifiers¶
The identifiers module defines typed ID wrappers using typing.NewType. These are thin wrappers over str (UUID v4 values) that give the type-checker a way to enforce that, for example, a NodeId cannot be passed where a GraphId is expected.
Why typed IDs?¶
Without typed IDs, all entity identifiers are plain str, and bugs like the following are invisible to the type-checker:
# BUG: passing a graph_id where a node_id is expected
engine.remove_node(graph_id=workspace_id, node_id=graph_id) # silently wrong
With NewType, static analysis tools (mypy, Pyright) catch this at development time.
Types¶
| Type | Description |
|---|---|
WorkspaceId |
Identifies a Workspace |
GraphId |
Identifies a Graph within a workspace |
NodeId |
Identifies a Node within a graph |
EdgeId |
Identifies an Edge within a graph |
Factory Functions¶
Each NewType has a corresponding factory that generates a fresh UUID:
from knowledge_platform.core.identifiers import (
new_workspace_id, new_graph_id, new_node_id, new_edge_id,
)
ws_id = new_workspace_id() # WorkspaceId("3f2504e0-4f89-11d3-9a0c-0305e82c3301")
g_id = new_graph_id() # GraphId("...")