Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Passing Solver explicitly causes UnboundLocalError

Open
#355 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python
Domain
backend

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from coin-or/python-mip

All issues in coin-or/python-mip

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.