Skip to content

Commit

Permalink
Catch AttributeErrors stemming from ipython_genutils as ValidationErr…
Browse files Browse the repository at this point in the history
…ors on read (#241)

* Catch AttributeErrors thrown by ipython_genutils during reading and conversion as ValidationErrors

* fix docstrings

* catch attribute errors as validation errors while reading json

* revert changes to converter (in another PR)
  • Loading branch information
therzka authored Dec 17, 2021
1 parent c521262 commit f2722ad
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
15 changes: 14 additions & 1 deletion nbformat/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Distributed under the terms of the Modified BSD License.

import json
from .validator import ValidationError

class NotJSONError(ValueError):
pass
Expand Down Expand Up @@ -52,13 +53,24 @@ def reads(s, **kwargs):
-------
nb : NotebookNode
The notebook that was read.
Raises
------
ValidationError
Notebook JSON for a given version is missing an expected key and cannot be read.
NBFormatError
Specified major version is invalid or unsupported.
"""
from . import versions, NBFormatError

nb_dict = parse_json(s, **kwargs)
(major, minor) = get_version(nb_dict)
if major in versions:
return versions[major].to_notebook_json(nb_dict, minor=minor)
try:
return versions[major].to_notebook_json(nb_dict, minor=minor)
except AttributeError as e:
raise ValidationError(f"The notebook is invalid and is missing an expected key: {e}")
else:
raise NBFormatError('Unsupported nbformat version %s' % major)

Expand All @@ -80,3 +92,4 @@ def read(fp, **kwargs):
The notebook that was read.
"""
return reads(fp.read(), **kwargs)

7 changes: 7 additions & 0 deletions nbformat/tests/test3_no_worksheets.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0
}
12 changes: 12 additions & 0 deletions nbformat/tests/test3_worksheet_with_no_cells.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"metadata": {}
}
]
}
12 changes: 12 additions & 0 deletions nbformat/tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .base import TestsBase

from ..reader import read, get_version
from ..validator import ValidationError

#-----------------------------------------------------------------------------
# Classes and functions
Expand All @@ -36,3 +37,14 @@ def test_read(self):
nb = read(f)
(major, minor) = get_version(nb)
self.assertEqual(major, 2)

def test_read_fails_on_missing_worksheets(self):
with self.fopen(u'test3_no_worksheets.ipynb', u'r') as f:
with self.assertRaisesRegex(ValidationError, r'worksheets'):
nb = read(f)

def test_read_fails_on_missing_worksheet_cells(self):
with self.fopen(u'test3_worksheet_with_no_cells.ipynb', u'r') as f:
with self.assertRaisesRegex(ValidationError, r'cells'):
nb = read(f)

0 comments on commit f2722ad

Please sign in to comment.