You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unexpected lack of input validation for Pyomo slices #4051
I spent a while troubleshooting a bug in a PrOMMiS model. The problem is that a variable was being fixed for an invalid index.
# Should be fixing it for "H_+"
self.leach.liquid_inlet.conc_mass_comp[:, "H"].fix(
2 * 0.05 * 1e3 * units.mg / units.L
)
Using the wrong index was my own fault, but I'd expect Pyomo input validation to recognize that the index is invalid and return a KeyError.
Steps to reproduce the issue
# example.py
import pyomo.environ as pyo
m = pyo.ConcreteModel()
m.x = pyo.Var(["a", "b", "c"], [1, 2, 3])
m.x["d", :].fix(0)
Error Message
I'd expect a KeyError to be raised, because "d" isn't a valid index for the first index. Instead, it continues, returning an empty list. Trying to fix the slice along a valid index returns a list with three Nones.
Information on your system
Pyomo version: 6.10.1
Python version: 3.11
Operating system: Windows
How Pyomo was installed (PyPI, conda, source): PrOMMiS dev installation
Solver (if applicable): n/a
Activity
jsiirola commentedon Oct 7, 2026
It is unclear in this situation when a KeyError should be raised:
Your case is (2), but what about:
Should that raise a
KeyError?The original design was that unmatched patterns would return empty iterators. This was motivated by slicing in
list: