From 1c2add788f4a9e8107e1b11c6c18dc66c60332f3 Mon Sep 17 00:00:00 2001 From: ozpau Date: Wed, 5 Feb 2025 02:34:24 -0800 Subject: [PATCH] Better handling of cells that contain string 'nbdev_export(' --- nbdev/processors.py | 4 ++-- nbdev/test.py | 1 - nbs/api/10_processors.ipynb | 4 ++-- nbs/api/12_test.ipynb | 1 - 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/nbdev/processors.py b/nbdev/processors.py index 204a253e4..3ae745144 100644 --- a/nbdev/processors.py +++ b/nbdev/processors.py @@ -221,7 +221,7 @@ def _ast_contains(trees, types): def _do_eval(cell): if cell_lang(cell) != 'python': return - if not cell.source or 'nbdev_export'+'()' in cell.source: return + if not cell.source: return trees = cell.parsed_() if cell.cell_type != 'code' or not trees: return if cell.directives_.get('eval:', [''])[0].lower() == 'false': return @@ -229,7 +229,7 @@ def _do_eval(cell): _show_dirs = {'export','exports','exporti','exec_doc'} if cell.directives_.keys() & _show_dirs: return True if _ast_contains(trees, (ast.Import, ast.ImportFrom)): - if _ast_contains(trees, (ast.Expr, ast.Assign)): + if _ast_contains(trees, (ast.Expr, ast.Assign)) and 'nbdev_export' not in cell.source: warn(f'Found cells containing imports and other code. See FAQ.\n---\n{cell.source}\n---\n') return True if _show_docs(trees): return True diff --git a/nbdev/test.py b/nbdev/test.py index ee223232a..a6f69d4f3 100644 --- a/nbdev/test.py +++ b/nbdev/test.py @@ -39,7 +39,6 @@ def test_nb(fn, # file name of notebook to test def _no_eval(cell): if cell.cell_type != 'code': return True - if 'nbdev_export'+'(' in cell.source: return True direc = getattr(cell, 'directives_', {}) or {} if direc.get('eval:', [''])[0].lower() == 'false': return True return flags & direc.keys() diff --git a/nbs/api/10_processors.ipynb b/nbs/api/10_processors.ipynb index 19e5fb047..1fa901b42 100644 --- a/nbs/api/10_processors.ipynb +++ b/nbs/api/10_processors.ipynb @@ -683,7 +683,7 @@ "\n", "def _do_eval(cell):\n", " if cell_lang(cell) != 'python': return\n", - " if not cell.source or 'nbdev_export'+'()' in cell.source: return\n", + " if not cell.source: return\n", " trees = cell.parsed_()\n", " if cell.cell_type != 'code' or not trees: return\n", " if cell.directives_.get('eval:', [''])[0].lower() == 'false': return\n", @@ -691,7 +691,7 @@ " _show_dirs = {'export','exports','exporti','exec_doc'}\n", " if cell.directives_.keys() & _show_dirs: return True\n", " if _ast_contains(trees, (ast.Import, ast.ImportFrom)):\n", - " if _ast_contains(trees, (ast.Expr, ast.Assign)):\n", + " if _ast_contains(trees, (ast.Expr, ast.Assign)) and 'nbdev_export' not in cell.source:\n", " warn(f'Found cells containing imports and other code. See FAQ.\\n---\\n{cell.source}\\n---\\n')\n", " return True\n", " if _show_docs(trees): return True" diff --git a/nbs/api/12_test.ipynb b/nbs/api/12_test.ipynb index 5cbb90f12..3aaca9f88 100644 --- a/nbs/api/12_test.ipynb +++ b/nbs/api/12_test.ipynb @@ -69,7 +69,6 @@ "\n", " def _no_eval(cell):\n", " if cell.cell_type != 'code': return True\n", - " if 'nbdev_export'+'(' in cell.source: return True\n", " direc = getattr(cell, 'directives_', {}) or {}\n", " if direc.get('eval:', [''])[0].lower() == 'false': return True\n", " return flags & direc.keys()\n",