Skip to content

fix: reject variable names SymPy cannot parse before the search starts - #1418

Merged
MilesCranmer merged 2 commits into
astroautomata:masterfrom
M-Colley:fix/variable-name-validation
Oct 8, 2026
Merged

MilesCranmer merged 2 commits into
astroautomata:masterfrom
M-Colley:fix/variable-name-validation

Conversation

@M-Colley

@M-Colley M-Colley commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Equations are converted to SymPy by parsing them as Python expressions, but variable names were only checked against a character regex and existing function names. Some names that passed those checks break that parsing:

  • Names that look like numbers, such as 12 (e.g. wavelength column headers), are parsed as constants, so predictions were silently wrong.
  • Python keywords such as lambda or in, names starting with a digit such as 1x, and names with subscript digits such as x₁ ran the entire search, and fit then failed while parsing the hall of fame, losing the in-memory result.
  • A name equal to a key of extra_sympy_mappings resolved to the function instead of the variable.

For expression specs that export to SymPy, fit now rejects these names before the search starts, with a hint on how to rename them. Specs without SymPy export, such as TemplateExpressionSpec, are unaffected. The variable_names docstring lists the rules.

Tests

test_variable_names_sympy_cannot_parse_fail_before_search covers lambda, in, 1x, 12, x₁, and a name clashing with extra_sympy_mappings.

M-Colley and others added 2 commits October 8, 2026 19:48
Equations are converted to SymPy by parsing them as Python expressions,
but variable names were only checked against a character regex and
against existing function names. Names that pass those checks yet are not
valid Python identifiers ran the whole search, and then `fit` failed in
`get_hof` when parsing the hall of fame, e.g. with
`SympifyError: could not parse '0.0659 / (x₁ + 0.86)'`:

- Python keywords (`lambda`, a common physics symbol, or `in`);
- names starting with a digit (`1x`);
- names with subscript digits (`x₁`), which the regex allows explicitly.

A variable named like a key of `extra_sympy_mappings` was also accepted,
although the parser then resolves the name to the function.

Check all of these up front for expression specs that export to SymPy,
so the error is raised before any time is spent searching.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Reword the error for names that are not valid non-keyword Python identifiers, add a rename hint, document the constraint in the fit docstring, and test a numeric-looking name such as "12", which master silently turns into a constant.

Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>

@MilesCranmer MilesCranmer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a helpful error indeed. Thanks

@MilesCranmer
MilesCranmer enabled auto-merge (squash) October 8, 2026 21:30
@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@MilesCranmer
MilesCranmer merged commit e5db909 into astroautomata:master Oct 8, 2026
48 checks passed
@M-Colley
M-Colley deleted the fix/variable-name-validation branch October 9, 2026 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants