Description
Add per-node source-location metadata to the snapshot returned by kedro.inspection.get_project_snapshot(...), so a consumer can show a node's source code without importing/running the project or holding live Node/function objects.
Add one field to NodeSnapshot:
source: NodeSourceSnapshot | None — the function's source location (file path + line range), or None when it cannot be determined.
Context
Kedro-Viz is moving its graph backend onto the inspection snapshot (kedro-org/kedro-viz#2265). The target is that Viz builds the entire flowchart from a single get_project_snapshot(...) call, without bootstrapping and running the project and without keeping live function objects in memory.
One thing Viz still needs the live objects for is the node Code panel. Today it calls inspect.getsource on the live function object. In a snapshot-only backend there is no live function to inspect, so the source location has to travel with the snapshot. Viz would then read the file, slice the given line range to render the panel, and fall back to no code panel when source is None.
Proposed API
@dataclass
class NodeSourceSnapshot:
"""Source location metadata for a pipeline node's underlying function."""
filepath: str # project-relative when the file is inside the project root, else absolute
line_start: int # 1-based line number of the first line of the definition
line_end: int # 1-based line number of the last line of the definition
Add to NodeSnapshot:
source: NodeSourceSnapshot | None = None # None when the location cannot be resolved
Behaviour
source is None when the location cannot be resolved: lambdas, functools.partial, dynamically generated functions, built-ins, or files that cannot be read. Consumers must handle None (for example, render the node with no code panel).
filepath is project-relative when the file lives inside the project root, otherwise absolute.
line_start and line_end are 1-based and inclusive.
Description
Add per-node source-location metadata to the snapshot returned by
kedro.inspection.get_project_snapshot(...), so a consumer can show a node's source code without importing/running the project or holding liveNode/function objects.Add one field to
NodeSnapshot:source: NodeSourceSnapshot | None— the function's source location (file path + line range), orNonewhen it cannot be determined.Context
Kedro-Viz is moving its graph backend onto the inspection snapshot (kedro-org/kedro-viz#2265). The target is that Viz builds the entire flowchart from a single
get_project_snapshot(...)call, without bootstrapping and running the project and without keeping live function objects in memory.One thing Viz still needs the live objects for is the node Code panel. Today it calls
inspect.getsourceon the live function object. In a snapshot-only backend there is no live function to inspect, so the source location has to travel with the snapshot. Viz would then read the file, slice the given line range to render the panel, and fall back to no code panel whensourceisNone.Proposed API
Add to
NodeSnapshot:Behaviour
sourceisNonewhen the location cannot be resolved: lambdas,functools.partial, dynamically generated functions, built-ins, or files that cannot be read. Consumers must handleNone(for example, render the node with no code panel).filepathis project-relative when the file lives inside the project root, otherwise absolute.line_startandline_endare 1-based and inclusive.