Skip to content

[BUG] Variable.VariableType is not normalized to VType (str/bytes/enum coexist) #1736

Description

@jackthepunished

Describe the bug
Variable.VariableType is not normalized, so it can hold three different representations of the same type:

  • a VType member when built through the Python API with VType.INTEGER / INTEGER;
  • a plain str ("I") when the problem comes from Problem.readMPS / Problem.read — the parser builds var_types with dtype='str' and _from_data_model passes each element straight to addVariable(vtype=...);
  • bytes (b"I") when a caller passes them — addVariable and setVariableType accept any value unchecked.

Downstream code copes ad hoc: Problem.IsMIP tests in ("I", "S", b"I", b"S") and solver.is_mip branches on isinstance(var_types[0], bytes). VType("I") works because VType is a str enum, but VType(b"I") raises ValueError.

Steps/Code to reproduce bug

from cuopt.linear_programming.problem import Problem, VType, INTEGER

p = Problem()
a = p.addVariable(vtype=INTEGER)
b = p.addVariable(vtype="I")
c = p.addVariable(vtype=b"I")
print([type(v.VariableType) for v in (a, b, c)])
# [<enum 'VType'>, <class 'str'>, <class 'bytes'>]

q = Problem.readMPS("<any MIP>.mps")
print(type(q.getVariables()[0].VariableType))   # <class 'str'>

Expected behavior
VariableType is always a VType member, normalized in Variable.__init__ / setVariableType (decoding bytes, then VType(value)), so IsMIP, solver.is_mip, and display code do not each need to handle the variants.

Environment details

  • From source on main (Problem.addVariable, Problem._from_data_model, io/parser_wrapper.pyx); not environment-specific.

Additional context
Surfaced while adding __repr__ in #1400, which decodes bytes before VType(...) until this is normalized.

Activity

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

Metadata

Metadata

Assignees

Labels

awaiting responseThis expects a response from maintainer or contributor depending on who requested in last comment.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions