Skip to content

Commit 543ece9

Browse files
committed
User-friendly error if py file has # %% comments with unexpected format
fixes #1188
1 parent 8021812 commit 543ece9

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

nbdev/doclinks.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ def _iter_py_cells(p):
4949
cells = p.read_text().split("\n# %% ")
5050
for cell in cells[1:]:
5151
top,code = cell.split('\n', 1)
52-
nb,idx = top.split()
52+
try: nb,idx = top.split()
53+
except ValueError:
54+
raise NotImplementedError(f"Unexpected format in '{p}' at cell:\n```\n# %% {cell.strip()}.\n```\n"
55+
"The expected format is: '# %% {nb_path} {cell_idx}'.")
5356
nb_path = None if nb=='auto' else (p.parent/nb).resolve() # NB paths are stored relative to .py file
5457
if code.endswith('\n'): code=code[:-1]
5558
yield AttrDict(nb=nb, idx=int(idx), code=code, nb_path=nb_path, py_path=p.resolve())

nbs/api/doclinks.ipynb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@
137137
" cells = p.read_text().split(\"\\n# %% \")\n",
138138
" for cell in cells[1:]:\n",
139139
" top,code = cell.split('\\n', 1)\n",
140-
" nb,idx = top.split()\n",
140+
" try: nb,idx = top.split()\n",
141+
" except ValueError:\n",
142+
" raise NotImplementedError(f\"Unexpected format in '{p}' at cell:\\n```\\n# %% {cell.strip()}.\\n```\\n\"\n",
143+
" \"The expected format is: '# %% {nb_path} {cell_idx}'.\")\n",
141144
" nb_path = None if nb=='auto' else (p.parent/nb).resolve() # NB paths are stored relative to .py file\n",
142145
" if code.endswith('\\n'): code=code[:-1]\n",
143146
" yield AttrDict(nb=nb, idx=int(idx), code=code, nb_path=nb_path, py_path=p.resolve())"

0 commit comments

Comments
 (0)