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.
Describe the bug
Variable.VariableTypeis not normalized, so it can hold three different representations of the same type:VTypemember when built through the Python API withVType.INTEGER/INTEGER;str("I") when the problem comes fromProblem.readMPS/Problem.read— the parser buildsvar_typeswithdtype='str'and_from_data_modelpasses each element straight toaddVariable(vtype=...);bytes(b"I") when a caller passes them —addVariableandsetVariableTypeaccept any value unchecked.Downstream code copes ad hoc:
Problem.IsMIPtestsin ("I", "S", b"I", b"S")andsolver.is_mipbranches onisinstance(var_types[0], bytes).VType("I")works becauseVTypeis astrenum, butVType(b"I")raisesValueError.Steps/Code to reproduce bug
Expected behavior
VariableTypeis always aVTypemember, normalized inVariable.__init__/setVariableType(decoding bytes, thenVType(value)), soIsMIP,solver.is_mip, and display code do not each need to handle the variants.Environment details
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 beforeVType(...)until this is normalized.