镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Unexpected lack of input validation for Pyomo slices #4051

Open

Description

@dallan-keylogic

Summary

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

  1. jsiirola commented on Oct 7, 2026

    @jsiirola
    Member

    It is unclear in this situation when a KeyError should be raised:

    1. any time the slice is empty
    2. when an index is not in the domain for the underlying set

    Your case is (2), but what about:

    m = ConcreteModel()
    m.I = Set(within=['a', 'b', 'c', 'd'], initialize=['a', 'b', 'c'])
    m.x = Var(m.I, [1, 2, 3])
    
    m.x["d", :].fix(0)

    Should that raise a KeyError?

    The original design was that unmatched patterns would return empty iterators. This was motivated by slicing in list:

    >>> a = []
    >>> a[5:7]
    []
    
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions