pytest_notebook.diffing module#

Diffing of notebooks.

pytest_notebook.diffing.diff_notebooks(initial: NotebookNode, final: NotebookNode, initial_path: str = '') list[DiffEntry][source]#

Compare two notebooks.

This is a simplified version of nbdime.diff_notebooks(), where we replace nbdime.diff_sequence_multilevel() with diff_sequence_simple() to diff the cell and output lists. diff_sequence_multilevel use ‘snakes’ computation, to guess where cells have been inserted/removed. However, this can lead to longer diffs, where cells with changed outputs are assigned as removed/inserted, rather than simply modified. Moreover, since we are comparing the same notebook before/after execution, we shouldn’t need to worry about insertions.

pytest_notebook.diffing.diff_sequence_simple(initial: Sequence, final: Sequence, path: str = '', config: DiffConfig = None) dict[source]#

Compute diff of two lists with configurable behaviour.

If the lists are of different lengths, we assume that items have been appended or removed from the end of the initial list.

pytest_notebook.diffing.diff_to_string(notebook: NotebookNode, diff_obj: dict, use_git: bool = True, use_diff: bool = True, use_color: bool = True, color_words: bool = False) str[source]#

Convert diff to formatted string.

Parameters:
  • use_git – use git for formatting diff/merge text output

  • use_diff – use diff/diff3 for formatting diff/merge text output

  • use_color – whether to prevent use of ANSI color code escapes for text output

  • color_words – whether to pass the –color-words flag to any internal calls to git diff

pytest_notebook.diffing.filter_diff(diff: list[DiffEntry], remove_paths: list[str], path: str = '') list[DiffEntry][source]#

Filter a notebook diff object, removing a list of paths.

Paths are joined by ‘/’ and may be starred, e.g. ‘/cells/*/outputs’.

pytest_notebook.diffing.load_nbdime_ignore_config(path: str | Path) tuple[str, ...][source]#

Extract diff-ignore paths from an nbdime configuration file.

The file should be in the nbdime configuration format, e.g.:

{
  "Diff": {
    "Ignore": {
      "/cells/*/execution_count": true,
      "/cells/*/metadata": ["collapsed", "autoscroll"],
      "/metadata": ["language_info"]
    }
  }
}

Ignore mappings are read from the Diff, GitDiff and NbDiff sections (with later sections taking precedence, per path), mirroring what nbdime itself reads for the nbdiff command. A value of True ignores the whole path, False de-selects the path, null removes the path (as if it was never set), and a list of strings ignores <path>/<key> for each key.

Returns:

a tuple of diff-ignore paths, suitable for filter_diff or NBRegressionFixture.diff_ignore.

pytest_notebook.diffing.star_path(path)[source]#

Replace integers and integer-strings in a path with * .