Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: allow any value for expr #429

Merged
merged 9 commits into from
Jun 23, 2023
Merged
8 changes: 8 additions & 0 deletions myst_nb/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ def __post_init__(self):
"sections": (Section.global_lvl, Section.execute),
},
)
eval_name_regex: str = dc.field(
default=r"^[a-zA-Z_][a-zA-Z0-9_]*$",
metadata={
"validator": instance_of(str),
"help": "Regex that matches permitted values of eval expressions",
"sections": (Section.global_lvl, Section.file_lvl, Section.execute),
},
)
execution_mode: Literal["off", "force", "auto", "cache", "inline"] = dc.field(
default="auto",
metadata={
Expand Down
4 changes: 0 additions & 4 deletions myst_nb/core/execute/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import annotations

from pathlib import Path
import re
from typing import Any

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


EVAL_NAME_REGEX = re.compile(r"^[a-zA-Z_][a-zA-Z0-9_]*$")


class NotebookClientBase:
"""A base client for interacting with Jupyter notebooks.

Expand Down
5 changes: 3 additions & 2 deletions myst_nb/core/execute/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import asyncio
from datetime import datetime
import re
import shutil
from tempfile import mkdtemp
import time
Expand All @@ -22,7 +23,7 @@

from myst_nb.ext.glue import extract_glue_data_cell

from .base import EVAL_NAME_REGEX, EvalNameError, ExecutionError, NotebookClientBase
from .base import EvalNameError, ExecutionError, NotebookClientBase


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

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

Expand Down
6 changes: 4 additions & 2 deletions myst_nb/ext/eval/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ def retrieve_eval_data(document: nodes.document, key: str) -> list[VariableOutpu
except NotImplementedError:
raise RetrievalError("This document does not have a running kernel")
except EvalNameError:
raise RetrievalError(f"The variable {key!r} is not a valid name")
raise RetrievalError(
f"The expression {key!r} is not valid according to the configured pattern"
)
except Exception as exc:
raise RetrievalError(f"variable evaluation error: {exc}")
if not outputs:
raise RetrievalError(f"variable {key!r} does not return any outputs")
raise RetrievalError(f"expression {key!r} does not return any outputs")

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