Skip to content

Commit

Permalink
Merge pull request #384 from European-XFEL/fix/variableNameValidation
Browse files Browse the repository at this point in the history
fix variable name validation
  • Loading branch information
tmichela authored Feb 3, 2025
2 parents 5113981 + 302f299 commit ab692ff
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions damnit/ctxsupport/damnit_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def __call__(self, func):

def check(self):
problems = []
if not re.fullmatch(r"[a-zA-Z_]\w+", self.name, flags=re.A):
if not self.name.isidentifier():
problems.append(
f"The variable name {self.name!r} is not of the form '[a-zA-Z_]\\w+'"
f"The variable name {self.name!r} is not a valid Python identifier"
)
if self._data not in (None, "raw", "proc"):
problems.append(
Expand Down
5 changes: 4 additions & 1 deletion tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ def bar(run, foo: "var#foo"):
@Variable(title="foo")
def foo(run, sample: "mymdc#sample_name", run_type: "mymdc#run_type"):
return 42
@Variable(title='X')
def X(run, foo: "var#foo"):
return foo
"""
# This should not raise an exception
mkcontext(good_mymdc_code).check()
Expand Down Expand Up @@ -198,7 +202,6 @@ def foo(run):
with pytest.raises(ContextFileErrors, match='must be a non-empty string'):
ctx.check()


def run_ctx_helper(context, run, run_number, proposal, caplog, input_vars=None):
# Track all error messages during creation. This is necessary because a
# variable that throws an error will be logged by Results, the exception
Expand Down

0 comments on commit ab692ff

Please sign in to comment.