Skip to content

fzc/fzr: pre-evaluate formulas from inline variable defaults - #85

Merged
yannrichet-asnr merged 1 commit into
mainfrom
fix/fzc-fzr-preeval-inline-defaults
Sep 9, 2026
Merged

fzc/fzr: pre-evaluate formulas from inline variable defaults#85
yannrichet-asnr merged 1 commit into
mainfrom
fix/fzc-fzr-preeval-inline-defaults

Conversation

@yannrichet-asnr

Copy link
Copy Markdown
Member

Problem

Given an input file that declares a variable default and a formula that uses it:

x = $(x~3)
y = @{x * 2}

fzi already pre-evaluated both ({'x': 3, 'x * 2': 6}), but fzc / fzr only
substituted the variable and left the formula uncompiled:

x = 3
y = @{x * 2}          # should have been "y = 6"

Cause

compile_to_result_directories (used by both fzc and fzr) passed only the
caller-supplied input_variables to replace_variables_in_content and
evaluate_formulas, and never looked at the $(var~default) defaults embedded
in the file. replace_variables_in_content recovers defaults on its own (so the
bare variable was substituted), but evaluate_formulas failed with
name 'x' is not defined and left the formula verbatim.

Fix

  • Factor fzi's inline-default extraction into a shared helper
    fz.interpreter.parse_variable_defaults_from_content(content, varprefix, delim).
  • fzi now uses it (behaviour unchanged; ~34 inline lines removed).
  • compile_to_result_directories seeds the compile context with those defaults
    ({**inline_defaults, **var_combo}), so formulas depending only on defaulted
    variables are evaluated during compilation, matching fzi.
  • Explicitly passed input_variables still win; list/bounds defaults that don't
    resolve to a usable scalar are skipped.

Tests

New tests/test_fzc_defaults.py (11 tests):

  • reference guard that fzi pre-evaluates default + formula;
  • fzc: pre-eval from default, parity with fzi, explicit override, formula
    left 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 in results/ and the
    parsed output), explicit override over a 2-point grid;
  • unit tests for parse_variable_defaults_from_content (int/float/bare & quoted
    string/scientific notation/valid list literal/truncated [ -> None/;
    metadata ignored/no default/empty delim).

NEWS.md updated under ## Unreleased.

The unrelated tests/test_demos.py::TestDisplayResultsTmp::test_get_analysis_tmp_is_called
failure 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

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
Copilot AI lite review requested due to automatic review settings September 9, 2026 09:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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 refactored fzi() to use it.
  • Updated compile_to_result_directories() (used by fzc/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 thread fz/helpers.py
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,
}
@yannrichet-asnr
yannrichet-asnr merged commit ac1446e into main Sep 9, 2026
37 of 38 checks passed
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.

2 participants