Skip to content

Commit

Permalink
ignoring standard error in notebook introspector
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed Aug 11, 2022
1 parent 3befdf8 commit 7b069b4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/sklearn_evaluation/nb/NotebookIntrospector.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,17 @@ def _process_output(output):
'implemented'.format(out_type))


def _filter_and_process_outputs(outputs):
def _allowed_output(output):
allowed = {'stream', 'display_data', 'execute_result'}
outputs = [o for o in outputs if o['output_type'] in allowed]

if output['output_type'] in allowed:
return output.get('name') != 'stderr'
else:
return False


def _filter_and_process_outputs(outputs):
outputs = [o for o in outputs if _allowed_output(o)]
# just return the latest output if any
return None if not outputs else _process_output(outputs[-1])

Expand Down
23 changes: 23 additions & 0 deletions tests/nb/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,26 @@ def nb_str():
print('something')
"""
return save_notebook(content, 'nb.ipynb')


@pytest.fixture
def nb_many_str():
content = """
# + tags=["str"]
print('a')
print('b')
print('c')
"""
return save_notebook(content, 'nb.ipynb')


@pytest.fixture
def nb_stderr():
content = """
import sys
# + tags=["str"]
print('this should not appear', file=sys.stderr)
print('something')
"""
return save_notebook(content, 'nb.ipynb')
10 changes: 8 additions & 2 deletions tests/nb/test_NotebookIntrospector.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@ def test_get_injected_parameters_multiple_lines(
}


def test_ignores_standard_error():
raise NotImplementedError
def test_ignores_standard_error(tmp_directory, nb_stderr):
d = NotebookIntrospector('nb.ipynb')
assert d['str'] == 'something'


def test_strips_whitespace(tmp_directory, nb_str):
d = NotebookIntrospector('nb.ipynb')
assert d['str'] == 'something'


def test_many_str(tmp_directory, nb_many_str):
d = NotebookIntrospector('nb.ipynb')
assert d['str'] == 'a\nb\nc'

0 comments on commit 7b069b4

Please sign in to comment.