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
I accidentally wrote the following (incorrect - the parameter should be solver_name) code:
import mip
model = mip.Model(sense=mip.MINIMIZE, solver=mip.CBC)
This fails with the following rather surprising error:
Traceback (most recent call last):
File "/Users/drmaciver/Projects/sat-sampling/check_mip.py", line 3, in <module>
model = mip.Model(sense=mip.MINIMIZE, solver=mip.CBC)
File "/Users/drmaciver/Projects/sat-sampling/.venv/lib/python3.10/site-packages/mip/model.py", line 104, in __init__
self.constrs = mip.ConstrList(self)
UnboundLocalError: local variable 'mip' referenced before assignment
The reason for this is that the if not solver path in model.py's Model.__init__ includes various imports that look like e.g. import mip.gurobi. Because of the way Python imports work, this means that mip is now a local variable shadowing the global import mip. In paths where that import statement has not been run (i.e. when a solver argument is explicitly passed) attempts to use mip results in this UnboundLocalError
Expected behavior
As mentioned this is incorrect code, so it's reasonable to raise an error here, although the specific error raised is quite confusing (probably a TypeError would be appropriate, but if I'd been running a static checker it would have caught it).
The more important problem is that correct code using this parameter would also fail in an identical way - currently the solver parameter cannot be used at all, because any attempt to use it will trigger this bug.
This should be an easy enough fix (adding an unconditional import mip after the if block will do it for example).
Desktop (please complete the following information):
Tested on OSX, 3.10.11 (this shouldn't matter), with mip 1.15.0 (latest version right now).
The text was updated successfully, but these errors were encountered:
Describe the bug
I accidentally wrote the following (incorrect - the parameter should be
solver_name
) code:This fails with the following rather surprising error:
The reason for this is that the
if not solver
path in model.py'sModel.__init__
includes various imports that look like e.g.import mip.gurobi
. Because of the way Python imports work, this means thatmip
is now a local variable shadowing the globalimport mip
. In paths where that import statement has not been run (i.e. when a solver argument is explicitly passed) attempts to usemip
results in thisUnboundLocalError
Expected behavior
As mentioned this is incorrect code, so it's reasonable to raise an error here, although the specific error raised is quite confusing (probably a TypeError would be appropriate, but if I'd been running a static checker it would have caught it).
The more important problem is that correct code using this parameter would also fail in an identical way - currently the
solver
parameter cannot be used at all, because any attempt to use it will trigger this bug.This should be an easy enough fix (adding an unconditional
import mip
after the if block will do it for example).Desktop (please complete the following information):
Tested on OSX, 3.10.11 (this shouldn't matter), with mip 1.15.0 (latest version right now).
The text was updated successfully, but these errors were encountered: