PyPSA / PyPSA/linopy

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

Open
#731 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

data-model enhancement
Dominant language
Python
Stars
257
Forks
87
Avg merge
1d 3h
Merged PRs (30d)
29

Description

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

  • Tracked as a follow-up to #727 ("Variable.update() / Constraint.update() as canonical mutation API"). The pipeline rewrite was reverted from #727 because it would conflict with #726 / #722 on rebase.
  • Stack: #722 → #725 (merged) → #726 → this issue.
  • Should land after #726 to avoid double-touching the helpers.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Variable._validate_update and the analogous Constraint.update locations, then compare them with add_variables and add_constraints. Read the #726 helper rename and the tightened as_dataarray semantics before changing the update path. Done means changing bounds, coefficients, signs, or rhs uses the same validation, errors, and coordinate coercions as creation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.