Passing Solver explicitly causes UnboundLocalError
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 45/100
Research direction
Start in model.py at Model.init, especially the solver-selection branch and the later use of mip. Reproduce the issue with an explicitly passed solver, then verify that valid solver usage no longer raises UnboundLocalError and that the incorrect argument produces a clear error if applicable.
Written by the indexing model from the issue text.
Description
Describe the bug
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).
- Dominant language
- Linear Programming
- Stars
- 601
- Forks
- 108
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from coin-or/python-mip
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
coin-or/python-mip#426 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
coin-or/python-mip#425 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
coin-or/python-mip#423 · 4 comments · 3 reactions ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
coin-or/python-mip#422 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
coin-or/python-mip#420 · 4 comments ·
All issues in coin-or/python-mip
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
mksglu/context-mode#1200 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
clawsweeper:needs-maintainer-review clawsweeper:needs-product-decision clawsweeper:no-new-fix-pr impact:auth-provider issue-rating: 🌊 off-meta tidepool P2
Difficulty 1/5 Under an hour Newbie friendliness 80/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100