Skip to content

Add node source location to the inspection snapshot #5668

Description

@jitu5

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue: Feature RequestNew feature or improvement to existing feature

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions