Skip to content

Commit 30ce8df

Browse files
committed
Changed convert.upgrade to upgrade minor 4.x versions to 4.5
1 parent f0caf7d commit 30ce8df

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

docs/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ Changes in nbformat
77
In Development
88
==============
99

10+
5.1.1
11+
=====
12+
13+
- Changes convert.upgrade to upgrade minor 4.x versions to 4.5
14+
1015
5.1.0
1116
=====
1217

nbformat/v4/convert.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def _warn_if_invalid(nb, version):
2323
except ValidationError as e:
2424
get_logger().error("Notebook JSON is not valid v%i: %s", version, e)
2525

26-
def upgrade(nb, from_version=3, from_minor=0):
27-
"""Convert a notebook to v4.
26+
def upgrade(nb, from_version=None, from_minor=None):
27+
"""Convert a notebook to latest v4.
2828
2929
Parameters
3030
----------
@@ -35,6 +35,11 @@ def upgrade(nb, from_version=3, from_minor=0):
3535
from_minor : int
3636
The original minor version of the notebook to convert (only relevant for v >= 3).
3737
"""
38+
if not from_version:
39+
from_version = nb['nbformat']
40+
if not from_minor:
41+
from_minor = nb['nbformat_minor']
42+
3843
if from_version == 3:
3944
# Validate the notebook before conversion
4045
_warn_if_invalid(nb, from_version)
@@ -64,9 +69,18 @@ def upgrade(nb, from_version=3, from_minor=0):
6469
_warn_if_invalid(nb, nbformat)
6570
return nb
6671
elif from_version == 4:
67-
# nothing to do
68-
if from_minor != nbformat_minor:
69-
nb.metadata.orig_nbformat_minor = from_minor
72+
if from_minor == nbformat_minor:
73+
return
74+
75+
# other versions migration code e.g.
76+
# if from_minor < 3:
77+
# if from_minor < 4:
78+
79+
if from_minor < 5:
80+
for cell in nb.cells:
81+
cell.id = random_cell_id()
82+
83+
nb.metadata.orig_nbformat_minor = from_minor
7084
nb.nbformat_minor = nbformat_minor
7185

7286
return nb

nbformat/v4/tests/test_convert.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# -*- coding: utf-8 -*-
2+
import os
3+
import io
24
import copy
35

46
from unittest import mock
57
from nbformat import validate
68
from .. import convert
9+
from ..nbjson import reads
710

811
from . import nbexamples
912
from nbformat.v3.tests import nbexamples as v3examples
@@ -73,3 +76,16 @@ def test_downgrade_heading():
7376
]:
7477
downgraded = convert.downgrade_cell(v4cell)
7578
assert downgraded == expected
79+
80+
def test_upgrade_v4_to_4_dot_5():
81+
here = os.path.dirname(__file__)
82+
with io.open(os.path.join(here, os.pardir, os.pardir, 'tests', "test4.ipynb"), encoding='utf-8') as f:
83+
nb = reads(f.read())
84+
assert nb['nbformat_minor'] == 0
85+
validate(nb)
86+
assert nb.cells[0].get('id') is None
87+
88+
nb_up = convert.upgrade(nb)
89+
assert nb_up['nbformat_minor'] == 5
90+
validate(nb_up)
91+
assert nb_up.cells[0]['id'] is not None

0 commit comments

Comments
 (0)