Skip to content

Variable.update() / Constraint.update() should validate inputs the same way add_variables() / add_constraints() do #731

@FBumann

Description

@FBumann

This is a tracking issue

Problem

Variable.update() and Constraint.update() currently validate inputs differently from their corresponding add_* constructors. Coord-aware checks (extra dims, mismatched coord values, reindex on equal-but-reordered) live in add_variables / add_constraints but not in the update path.

Concretely on feat/typed-update-api:

# Variable._validate_update — current
new_val = DataArray(val).broadcast_like(ref)
if not set(new_val.dims).issubset(own_dims):
    raise ValueError("Cannot assign new dimensions to existing variable.")

vs.

# add_variables — current
if isinstance(lower, DataArray):
    lower = _validate_dataarray_bounds(lower, coords)
as_dataarray(lower, coords, **kwargs)

The add_* path catches a strict superset of what update() catches: pandas Series with a wrong-named index, DataArrays with extra dims, DataArrays with shared dims but mismatched coord values, etc. update() silently broadcasts in those cases.

What should happen

After #726 lands (which renames _validate_dataarray_boundsassert_compatible_with_coords and tightens as_dataarray's semantics), rebase Variable._validate_update and the analogous places in Constraint.update onto the same as_dataarray + assert_compatible_with_coords pipeline used by add_variables / add_constraints.

Net effect: the validation contract for changing a bound, coefficient, sign, or rhs becomes identical to the contract for creating one. Same errors, same coord coercions, no surprise behavior at the update site.

Notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions