From 30f3d81e7113ad7628443fc4cd389ca16b2d186a Mon Sep 17 00:00:00 2001 From: Min RK Date: Wed, 11 Jan 2023 14:24:17 +0100 Subject: [PATCH] Only require nbformat_minor for v4 (#342) --- nbformat/v4/convert.py | 12 ++++++++---- tests/test3_no_min_version.ipynb | 12 ++++++++++++ tests/test_validator.py | 6 ++++++ 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 tests/test3_no_min_version.ipynb diff --git a/nbformat/v4/convert.py b/nbformat/v4/convert.py index 1e76b872..c197e555 100644 --- a/nbformat/v4/convert.py +++ b/nbformat/v4/convert.py @@ -40,10 +40,14 @@ def upgrade(nb, from_version=None, from_minor=None): from_version = nb["nbformat"] if not from_minor: if "nbformat_minor" not in nb: - raise validator.ValidationError( - "The notebook does not include the nbformat minor which is needed" - ) - from_minor = nb["nbformat_minor"] + if from_version == 4: + raise validator.ValidationError( + "The v4 notebook does not include the nbformat minor, which is needed." + ) + else: + from_minor = 0 + else: + from_minor = nb["nbformat_minor"] if from_version == 3: # Validate the notebook before conversion diff --git a/tests/test3_no_min_version.ipynb b/tests/test3_no_min_version.ipynb new file mode 100644 index 00000000..02a70281 --- /dev/null +++ b/tests/test3_no_min_version.ipynb @@ -0,0 +1,12 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "worksheets": [ + { + "metadata": {}, + "cells": [] + } + ] +} diff --git a/tests/test_validator.py b/tests/test_validator.py index 468ad937..800043c7 100644 --- a/tests/test_validator.py +++ b/tests/test_validator.py @@ -355,6 +355,12 @@ def test_notebook_invalid_without_min_version(): validate(nb) +def test_notebook_v3_valid_without_min_version(): + with TestsBase.fopen("test3_no_min_version.ipynb", "r") as f: + nb = read(f, as_version=4) + validate(nb) + + def test_notebook_invalid_without_main_version(): pass