Skip to content

Commit

Permalink
Merge pull request #85 from vidartf/validate-empty
Browse files Browse the repository at this point in the history
Fix: Validation of empty dict raised AttributeError
  • Loading branch information
takluyver committed Feb 10, 2017
2 parents 1b4f8cd + b72a8aa commit df9ff6f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
4 changes: 4 additions & 0 deletions nbformat/tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def test_invalid(self):
validate(nb)
self.assertEqual(isvalid(nb), False)

def test_validate_empty(self):
"""Test that an empty dict can be validated without error"""
validate({})

def test_future(self):
"""Test than a notebook from the future with extra keys passes validation"""
with self.fopen(u'test4plus.ipynb', u'r') as f:
Expand Down
22 changes: 11 additions & 11 deletions nbformat/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
verbose_msg = """
Jupyter notebook format depends on the jsonschema package:
https://pypi.python.org/pypi/jsonschema
Please install it first.
"""
raise ImportError(str(e) + verbose_msg)
Expand Down Expand Up @@ -57,7 +57,7 @@ def get_validator(version=None, version_minor=None):
version = current_nbformat

v = import_item("nbformat.v%s" % version)
current_minor = v.nbformat_minor
current_minor = getattr(v, 'nbformat_minor', 0)
if version_minor is None:
version_minor = current_minor

Expand Down Expand Up @@ -100,7 +100,7 @@ def isvalid(nbjson, ref=None, version=None, version_minor=None):
def _format_as_index(indices):
"""
(from jsonschema._utils.format_as_index, copied to avoid relying on private API)
Construct a single string containing indexing operations for the indices.
For example, [1, 2, "foo"] -> [1][2]["foo"]
Expand All @@ -115,7 +115,7 @@ def _format_as_index(indices):

def _truncate_obj(obj):
"""Truncate objects for use in validation tracebacks
Cell and output lists are squashed, as are long strings, lists, and dicts.
"""
if isinstance(obj, dict):
Expand Down Expand Up @@ -143,25 +143,25 @@ def _truncate_obj(obj):

class NotebookValidationError(ValidationError):
"""Schema ValidationError with truncated representation
to avoid massive verbose tracebacks.
"""
def __init__(self, original, ref=None):
self.original = original
self.ref = getattr(self.original, 'ref', ref)
self.message = self.original.message

def __getattr__(self, key):
return getattr(self.original, key)

def __unicode__(self):
"""Custom str for validation errors
avoids dumping full schema and notebook to logs
"""
error = self.original
instance = _truncate_obj(error.instance)

return u'\n'.join([
error.message,
u'',
Expand All @@ -173,7 +173,7 @@ def __unicode__(self):
u'On instance%s:' % _format_as_index(error.relative_path),
pprint.pformat(instance, width=78),
])

if sys.version_info >= (3,):
__str__ = __unicode__

Expand Down

0 comments on commit df9ff6f

Please sign in to comment.