Skip to content

Commit

Permalink
Allow cell tags as alternative options source
Browse files Browse the repository at this point in the history
Reads cell metadata tags as an alternative to cell comments as a source
of options.
  • Loading branch information
vidartf committed Feb 7, 2017
1 parent c71226a commit 6d10341
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
27 changes: 25 additions & 2 deletions nbval/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
import re
import hashlib
import warnings
from collections import OrderedDict, defaultdict

# for python 3 compatibility
Expand Down Expand Up @@ -105,6 +106,12 @@ def pytest_collect_file(path, parent):
'NBVAL_RAISES_EXCEPTION': 'check_exception',
}

metadata_tags = {
k.lower().replace('_', '-'): v
for (k, v) in comment_markers.items()
}


def find_comment_markers(cellsource):
"""Look through the cell source for comments which affect nbval's behaviour
Expand All @@ -120,6 +127,18 @@ def find_comment_markers(cellsource):
yield (comment_markers[comment], True)


def find_metadata_tags(cell_metadata):
tags = cell_metadata.get('tags', None)
if tags is None:
return
elif not isinstance(tags, list):
warnings.warn("Cell tags is not a list, ignoring.")
return
for tag in tags:
if tag in metadata_tags:
yield (metadata_tags[tag], True)


class IPyNbFile(pytest.File):
"""
This class represents a pytest collector object.
Expand Down Expand Up @@ -212,7 +231,8 @@ def collect(self):
# The cell may contain a comment indicating that its output
# should be checked or ignored. If it doesn't, use the default
# behaviour. The --nbval option checks unmarked cells.
options = defaultdict(bool, find_comment_markers(cell.source))
options = defaultdict(bool, find_metadata_tags(cell.metadata))
options.update(find_comment_markers(cell.source))
options.setdefault('check', self.compare_outputs)
yield IPyNbCell('Cell ' + str(cell_num), self, cell_num,
cell, options)
Expand Down Expand Up @@ -267,6 +287,9 @@ def reportinfo(self):
description = "cell %d" % self.cell_num
return self.fspath, 0, description

def should_compare_outputs(self):
return self.options['check'] and not self.options['ignore']

def compare_outputs(self, test, ref, skip_compare=None):
# Use stored skips unless passed a specific value
skip_compare = skip_compare or self.parent.skip_compare
Expand Down Expand Up @@ -570,7 +593,7 @@ def runtest(self):
# self.diff_number_outputs(outs, self.cell.outputs)
# failed = True
failed = False
if self.options['check'] and not self.options['ignore']:
if self.should_compare_outputs():
if not self.compare_outputs(outs, self.cell.outputs):
failed = True

Expand Down
77 changes: 77 additions & 0 deletions tests/sample_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
"from datetime import datetime"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Different ways to ignore output differences."
]
},
{
"cell_type": "code",
"execution_count": 6,
Expand All @@ -67,6 +74,76 @@
"datetime.now()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2017, 1, 3, 16, 6, 20, 529355)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# NBVAL_IGNORE_OUTPUT\n",
"datetime.now()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"tags": [
"nbval-ignore-output"
]
},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2017, 1, 3, 16, 6, 20, 543350)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"datetime.now()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"tags": [
"nbval-check-output"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(5)"
]
},
{
"cell_type": "code",
"execution_count": 31,
Expand Down

0 comments on commit 6d10341

Please sign in to comment.