Skip to content

Commit dee3e4f

Browse files
authored
Ensure nbformat minor version is present when upgrading (#237)
* Ensure nbformat minor version is present when upgrading Motivation: - We have seen a number of notebooks which do not contain the nbformat minor version, this throws a key error when trying to update. This pr adds a check to ensure it is present and throws a validation error if not. * fix test
1 parent 6be91cc commit dee3e4f

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"test"
10+
]
11+
}
12+
],
13+
"metadata": {
14+
"language_info": {
15+
"name": "plaintext"
16+
},
17+
"orig_nbformat": 4
18+
},
19+
"nbformat": 4
20+
}

nbformat/tests/test_validator.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,12 @@ def test_invalid_cell_id():
232232
with pytest.raises(ValidationError):
233233
validate(nb)
234234
assert not isvalid(nb)
235+
236+
def test_notebook_invalid_without_min_version():
237+
with TestsBase.fopen(u'no_min_version.ipynb', u'r') as f:
238+
nb = read(f, as_version=4)
239+
with pytest.raises(ValidationError):
240+
validate(nb)
241+
242+
def test_notebook_invalid_without_main_version():
243+
pass

nbformat/v4/convert.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import json
77
import re
8+
from .. import validator
89

910
from .nbbase import (
1011
random_cell_id,
@@ -38,6 +39,8 @@ def upgrade(nb, from_version=None, from_minor=None):
3839
if not from_version:
3940
from_version = nb['nbformat']
4041
if not from_minor:
42+
if not 'nbformat_minor' in nb:
43+
raise validator.ValidationError('The notebook does not include the nbformat minor which is needed')
4144
from_minor = nb['nbformat_minor']
4245

4346
if from_version == 3:

nbformat/v4/tests/test_convert.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from nbformat import validate
88
from .. import convert
99
from ..nbjson import reads
10+
import pytest
11+
from nbformat import ValidationError
1012

1113
from . import nbexamples
1214
from nbformat.v3.tests import nbexamples as v3examples
@@ -89,3 +91,11 @@ def test_upgrade_v4_to_4_dot_5():
8991
assert nb_up['nbformat_minor'] == 5
9092
validate(nb_up)
9193
assert nb_up.cells[0]['id'] is not None
94+
95+
def test_upgrade_without_nbminor_version():
96+
here = os.path.dirname(__file__)
97+
with io.open(os.path.join(here, os.pardir, os.pardir, 'tests', "no_min_version.ipynb"), encoding='utf-8') as f:
98+
nb = reads(f.read())
99+
100+
with pytest.raises(ValidationError):
101+
convert.upgrade(nb)

0 commit comments

Comments
 (0)