Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,14 @@ therefore be updated in a near future to not take any of the argument
related to auto-fixing, and fail instead of silently modifying its
parameters on invalid notebooks.

`nbformat` now contain a `normalize` function that will return a
normalized copy of a notebook that is suitable for validation. While
offered as a convenience we discourage its use and suggest library make
sure to generate valid notebooks.
`nbformat` now provides a `normalize` function that returns a normalized
copy of a notebook suitable for validation. This function is intended as
a helper for tools that need to prepare notebooks before calling
`validate()`.

Most libraries and applications should continue to generate valid
notebooks directly and rely on `validate()` to check correctness, rather
than using `normalize()` as part of normal notebook creation.

### Other changes

Expand Down
5 changes: 4 additions & 1 deletion nbformat/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def get_version(nb):


def reads(s, **kwargs):
"""Read a notebook from a json string and return the
"""
Note: This function reads notebook content from a string and does not perform file I/O.

Read a notebook from a json string and return the
NotebookNode object.

This function properly reads notebooks of any version. No version
Expand Down
2 changes: 2 additions & 0 deletions nbformat/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ def validate(
"""Checks whether the given notebook dict-like object
conforms to the relevant notebook format schema.

Note: This function validates notebooks but does not modify them; use `normalize()` if a normalized copy is required before validation.

Parameters
----------
nbdict : dict
Expand Down
19 changes: 19 additions & 0 deletions tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,22 @@ def test_strip_invalid_metadata():
):
validate(nb, strip_invalid_metadata=True)
assert isvalid(nb)


def test_validate_does_not_mutate_notebook():
from nbformat import v4
from nbformat.validator import validate

# Create a simple valid notebook
nb = v4.new_notebook(cells=[v4.new_markdown_cell("hello")])

# Make a deep copy to compare after validation
import copy

nb_before = copy.deepcopy(nb)

# Validate the notebook
validate(nb)

# Ensure validation did not modify the notebook
assert nb == nb_before