Skip to content

Commit 5033030

Browse files
authored
Honor notebook-level eval directives in nbdev-test
1 parent 92f3307 commit 5033030

3 files changed

Lines changed: 32 additions & 10 deletions

File tree

nbdev/test.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ async def test_nb(
4848
fm = nb_frontmatter(nb)
4949
if str2bool(fm.get('skip_exec', False)) or nb_lang(nb) != 'python': return True, 0
5050

51+
dflt = fm_default_eval(fm)
5152
def _no_eval(cell):
5253
if cell.cell_type != 'code': return True
53-
if 'nbdev_export'+'(' in cell.source: return True
54-
direc = getattr(cell, 'directives_', {}) or {}
55-
if direc.get('eval', '').lower() == 'false': return True
56-
return flags & direc.keys()
54+
if not does_cell_eval(cell, dflt): return True
55+
return flags & (getattr(cell, 'directives_', {}) or {}).keys()
5756

5857
start = time.time()
5958
if profile is None: profile = bool(get_config(fn.parent).exec_profile)

nbs/api/12_test.ipynb

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,11 @@
7676
" fm = nb_frontmatter(nb)\n",
7777
" if str2bool(fm.get('skip_exec', False)) or nb_lang(nb) != 'python': return True, 0\n",
7878
"\n",
79+
" dflt = fm_default_eval(fm)\n",
7980
" def _no_eval(cell):\n",
8081
" if cell.cell_type != 'code': return True\n",
81-
" if 'nbdev_export'+'(' in cell.source: return True\n",
82-
" direc = getattr(cell, 'directives_', {}) or {}\n",
83-
" if direc.get('eval', '').lower() == 'false': return True\n",
84-
" return flags & direc.keys()\n",
82+
" if not does_cell_eval(cell, dflt): return True\n",
83+
" return flags & (getattr(cell, 'directives_', {}) or {}).keys()\n",
8584
"\n",
8685
" start = time.time()\n",
8786
" if profile is None: profile = bool(get_config(fn.parent).exec_profile)\n",
@@ -314,7 +313,31 @@
314313
"id": "8ee3f4db",
315314
"metadata": {},
316315
"source": [
317-
"## Eval -"
316+
"## Eval"
317+
]
318+
},
319+
{
320+
"cell_type": "markdown",
321+
"id": "0317b83e",
322+
"metadata": {},
323+
"source": [
324+
"`test_nb` decides which cells run through the `eval` cascade (`fastcore.nbio.does_cell_eval`): a cell's own `#| eval:` directive wins; otherwise the notebook-level `eval` directive — in frontmatter, or the notebook's `metadata.nbdev` mapping — sets the default; with neither, cells run. `#| eval: false` therefore skips one cell, as it always has, while a notebook-level `eval: false` flips the whole notebook to opt-in: only cells marked `#| eval: true` run, which suits a slow or service-dependent notebook where just a few cells are worth testing. Unlike `skip_exec: true`, which skips a notebook unconditionally, marked cells still run — here the unmarked cell would raise if executed, so the passing test is the proof it was skipped:"
325+
]
326+
},
327+
{
328+
"cell_type": "code",
329+
"execution_count": null,
330+
"id": "2c358146",
331+
"metadata": {},
332+
"outputs": [],
333+
"source": [
334+
"with tempfile.TemporaryDirectory() as td:\n",
335+
" cells = [mk_cell('---\\neval: false\\n---', 'raw'), mk_cell('raise Exception(\"unmarked: must not run\")'),\n",
336+
" mk_cell('#| eval: true\\nx = 1')]\n",
337+
" fn = Path(td)/'optin.ipynb'\n",
338+
" write_nb(new_nb(cells), fn)\n",
339+
" success,_ = await test_nb(fn)\n",
340+
"assert success"
318341
]
319342
},
320343
{

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ classifiers = [
1919
"License :: OSI Approved :: Apache Software License",
2020
]
2121
dynamic = ["version"]
22-
dependencies = [ "fastcore>=2.2.3", "execnb>=0.2.11", "astunparse", "ghapi>=2.0.2", "watchdog", "asttokens",
22+
dependencies = [ "fastcore>=2.2.7", "execnb>=0.2.11", "astunparse", "ghapi>=2.0.2", "watchdog", "asttokens",
2323
"setuptools", "build", "fastgit>=0.0.7", "pyyaml", "tomli; python_version < '3.11'", ]
2424

2525
[project.optional-dependencies]

0 commit comments

Comments
 (0)