fzc/fzr: pre-evaluate formulas from inline variable defaults - #85
Merged
Conversation
When an input file declared a variable default ($(x~3)) and a formula
using it (@{x * 2}), fzi pre-evaluated both but fzc/fzr only substituted
the variable and left the formula uncompiled (y = @{x * 2} instead of
y = 6). compile_to_result_directories only passed the caller-supplied
input_variables to evaluate_formulas and never looked at the inline
$(var~default) values.
Factor fzi's default extraction into the shared helper
interpreter.parse_variable_defaults_from_content and use it during
compilation to seed the substitution/formula context. Explicitly passed
input_variables still take precedence over inline defaults; list/bounds
defaults that don't resolve to a scalar are skipped.
Add tests/test_fzc_defaults.py covering the regression for fzc and fzr
plus unit tests for the shared helper.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UCiJZSajaqyKYJ9sNoE6fn
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Inline defaults that represent bounds/discrete-values metadata (e.g. [0,1], {0,0.1}) can currently be seeded into formula evaluation as real values, risking incorrect pre-evaluation instead of leaving formulas unevaluated.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Aligns compilation (fzc/fzr) with inspection (fzi) by pre-evaluating formulas that depend only on inline variable defaults ($(x~3)), so compiled inputs no longer retain unevaluated formulas like @{x * 2} when all required values are available from defaults.
Changes:
- Added shared helper
parse_variable_defaults_from_content()and refactoredfzi()to use it. - Updated
compile_to_result_directories()(used byfzc/fzr) to seed the formula/replace context with inline defaults so compilation can pre-evaluate those formulas. - Added regression/unit tests and documented the behavior change in
NEWS.md.
File summaries
| File | Description |
|---|---|
fz/interpreter.py |
Adds parse_variable_defaults_from_content() to extract inline defaults for reuse across code paths. |
fz/core.py |
Refactors fzi() to use the new shared inline-defaults helper. |
fz/helpers.py |
Seeds compilation variable context with inline defaults to enable formula pre-evaluation during fzc/fzr. |
tests/test_fzc_defaults.py |
Adds regression/unit coverage for fzc/fzr pre-evaluation and the new helper. |
NEWS.md |
Documents the new fzc/fzr behavior under Unreleased. |
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+1876
to
+1880
| inline_defaults = parse_variable_defaults_from_content(content, varprefix, delim) | ||
| effective_combo = { | ||
| **{k: v for k, v in inline_defaults.items() if v is not None}, | ||
| **var_combo, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Given an input file that declares a variable default and a formula that uses it:
fzialready pre-evaluated both ({'x': 3, 'x * 2': 6}), butfzc/fzronlysubstituted the variable and left the formula uncompiled:
Cause
compile_to_result_directories(used by bothfzcandfzr) passed only thecaller-supplied
input_variablestoreplace_variables_in_contentandevaluate_formulas, and never looked at the$(var~default)defaults embeddedin the file.
replace_variables_in_contentrecovers defaults on its own (so thebare variable was substituted), but
evaluate_formulasfailed withname 'x' is not definedand left the formula verbatim.Fix
fzi's inline-default extraction into a shared helperfz.interpreter.parse_variable_defaults_from_content(content, varprefix, delim).fzinow uses it (behaviour unchanged; ~34 inline lines removed).compile_to_result_directoriesseeds the compile context with those defaults(
{**inline_defaults, **var_combo}), so formulas depending only on defaultedvariables are evaluated during compilation, matching
fzi.input_variablesstill win; list/bounds defaults that don'tresolve to a usable scalar are skipped.
Tests
New
tests/test_fzc_defaults.py(11 tests):fzipre-evaluates default + formula;fzc: pre-eval from default, parity withfzi, explicit override, formulaleft unevaluated when a needed variable has no default, default still used with
partial vars, float default;
fzr: end-to-end pre-eval (checks the compiled file inresults/and theparsed
output), explicit override over a 2-point grid;parse_variable_defaults_from_content(int/float/bare & quotedstring/scientific notation/valid list literal/truncated
[->None/;metadata ignored/no default/empty delim).
NEWS.mdupdated under## Unreleased.The unrelated
tests/test_demos.py::TestDisplayResultsTmp::test_get_analysis_tmp_is_calledfailure seen locally is a broken environment (NumPy 2.4.6 vs matplotlib ABI,
_ARRAY_API not found) and fails identically on a clean tree.🤖 Generated with Claude Code
https://claude.ai/code/session_01UCiJZSajaqyKYJ9sNoE6fn