Skip to content

[Bug]: UnassignedVariableError.free_variables includes variables that are already bound #19

Description

@fedonman

Bug Description

UnassignedVariableError.__init__ sets free_variables to expression.variables(), which returns every variable in the tree whether or not it holds a value. Evaluate an expression over one bound and one unbound variable and the error reports both, and its message lists both as unassigned, so the caller is sent to fix a variable that is already fine. The attribute's own docstring says it holds "The unbound variables that caused the failure". Filtering expression.variables() down to the ones whose value is UNASSIGNED would fix the attribute and the message together.

Minimal Reproducible Example

p = qp.QProgram()
amp, freq = p.variable("amp"), p.variable("freq")
amp.set_value(0.5)

try:
    (amp * freq).evaluate_or_raise()
except qp.UnassignedVariableError as e:
    print(sorted(v.id for v in e.free_variables))
    print(e)

Expected Behavior

free_variables holds only freq, the one variable with no value, and the message names only that variable.

Actual Behavior

['amp', 'freq']
Cannot evaluate expression (Variable('amp') * Variable('freq')): unassigned variable(s) {Variable('freq'), Variable('amp')}

System Information

qprogram 0.1.0
python   3.14.3 | Linux-7.0.0-30-generic-x86_64-with-glibc2.43
vendors  none

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    expressionsTouches the expression AST: variables, comparisons, math functions

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions