diff --git a/nbformat/tests/no_min_version.ipynb b/nbformat/tests/no_min_version.ipynb new file mode 100644 index 00000000..7e8bc3a4 --- /dev/null +++ b/nbformat/tests/no_min_version.ipynb @@ -0,0 +1,20 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "test" + ] + } + ], + "metadata": { + "language_info": { + "name": "plaintext" + }, + "orig_nbformat": 4 + }, + "nbformat": 4 +} diff --git a/nbformat/tests/test_validator.py b/nbformat/tests/test_validator.py index 52936a39..8a01b428 100644 --- a/nbformat/tests/test_validator.py +++ b/nbformat/tests/test_validator.py @@ -232,3 +232,12 @@ def test_invalid_cell_id(): with pytest.raises(ValidationError): validate(nb) assert not isvalid(nb) + +def test_notebook_invalid_without_min_version(): + with TestsBase.fopen(u'no_min_version.ipynb', u'r') as f: + nb = read(f, as_version=4) + with pytest.raises(ValidationError): + validate(nb) + +def test_notebook_invalid_without_main_version(): + pass diff --git a/nbformat/v4/convert.py b/nbformat/v4/convert.py index 22914002..7b4321db 100644 --- a/nbformat/v4/convert.py +++ b/nbformat/v4/convert.py @@ -5,6 +5,7 @@ import json import re +from .. import validator from .nbbase import ( random_cell_id, @@ -38,6 +39,8 @@ def upgrade(nb, from_version=None, from_minor=None): if not from_version: from_version = nb['nbformat'] if not from_minor: + if not 'nbformat_minor' in nb: + raise validator.ValidationError('The notebook does not include the nbformat minor which is needed') from_minor = nb['nbformat_minor'] if from_version == 3: diff --git a/nbformat/v4/tests/test_convert.py b/nbformat/v4/tests/test_convert.py index f735d941..3e73e4a7 100644 --- a/nbformat/v4/tests/test_convert.py +++ b/nbformat/v4/tests/test_convert.py @@ -7,6 +7,8 @@ from nbformat import validate from .. import convert from ..nbjson import reads +import pytest +from nbformat import ValidationError from . import nbexamples from nbformat.v3.tests import nbexamples as v3examples @@ -89,3 +91,11 @@ def test_upgrade_v4_to_4_dot_5(): assert nb_up['nbformat_minor'] == 5 validate(nb_up) assert nb_up.cells[0]['id'] is not None + +def test_upgrade_without_nbminor_version(): + here = os.path.dirname(__file__) + with io.open(os.path.join(here, os.pardir, os.pardir, 'tests', "no_min_version.ipynb"), encoding='utf-8') as f: + nb = reads(f.read()) + + with pytest.raises(ValidationError): + convert.upgrade(nb) \ No newline at end of file