coin-or / coin-or/python-mip

Passing Solver explicitly causes UnboundLocalError

Open
#355 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Linear Programming
Stars
600
Forks
108
PR merge metrics
No merged PRs in 30d

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
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).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.