Skip to content

Commit 46c58de

Browse files
Feat: allow any value for expr (#429)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5d97244 commit 46c58de

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

myst_nb/core/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ def __post_init__(self):
188188
"sections": (Section.global_lvl, Section.execute),
189189
},
190190
)
191+
eval_name_regex: str = dc.field(
192+
default=r"^[a-zA-Z_][a-zA-Z0-9_]*$",
193+
metadata={
194+
"validator": instance_of(str),
195+
"help": "Regex that matches permitted values of eval expressions",
196+
"sections": (Section.global_lvl, Section.file_lvl, Section.execute),
197+
},
198+
)
191199
execution_mode: Literal["off", "force", "auto", "cache", "inline"] = dc.field(
192200
default="auto",
193201
metadata={

myst_nb/core/execute/base.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import annotations
33

44
from pathlib import Path
5-
import re
65
from typing import Any
76

87
from nbformat import NotebookNode
@@ -39,9 +38,6 @@ class EvalNameError(Exception):
3938
"""An exception for if an evaluation variable name is invalid."""
4039

4140

42-
EVAL_NAME_REGEX = re.compile(r"^[a-zA-Z_][a-zA-Z0-9_]*$")
43-
44-
4541
class NotebookClientBase:
4642
"""A base client for interacting with Jupyter notebooks.
4743

myst_nb/core/execute/inline.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import asyncio
55
from datetime import datetime
6+
import re
67
import shutil
78
from tempfile import mkdtemp
89
import time
@@ -22,7 +23,7 @@
2223

2324
from myst_nb.ext.glue import extract_glue_data_cell
2425

25-
from .base import EVAL_NAME_REGEX, EvalNameError, ExecutionError, NotebookClientBase
26+
from .base import EvalNameError, ExecutionError, NotebookClientBase
2627

2728

2829
class NotebookClientInline(NotebookClientBase):
@@ -148,7 +149,7 @@ def code_cell_outputs(
148149
return cell.get("execution_count", None), cell.get("outputs", [])
149150

150151
def eval_variable(self, name: str) -> list[NotebookNode]:
151-
if not EVAL_NAME_REGEX.match(name):
152+
if not re.match(self.nb_config.eval_name_regex, name):
152153
raise EvalNameError(name)
153154
return self._client.eval_expression(name)
154155

myst_nb/ext/eval/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ def retrieve_eval_data(document: nodes.document, key: str) -> list[VariableOutpu
4343
except NotImplementedError:
4444
raise RetrievalError("This document does not have a running kernel")
4545
except EvalNameError:
46-
raise RetrievalError(f"The variable {key!r} is not a valid name")
46+
raise RetrievalError(
47+
f"The expression {key!r} is not valid according to the configured pattern"
48+
)
4749
except Exception as exc:
4850
raise RetrievalError(f"variable evaluation error: {exc}")
4951
if not outputs:
50-
raise RetrievalError(f"variable {key!r} does not return any outputs")
52+
raise RetrievalError(f"expression {key!r} does not return any outputs")
5153

5254
# the returned outputs could be one of the following:
5355
# https://nbformat.readthedocs.io/en/latest/format_description.html#code-cell-outputs

0 commit comments

Comments
 (0)