`tuplelist` in Gurobipy 11
AnsweredWe would like to migrate from Gurobipy 10 to 11. `tuplelist` in version 11 doesn't accept custom Python objects. For instance, the code below worked in Gurobipy 10, but raises typing error in 11. How can we enable the support for custom Python objects?
grb.tuplelist((custom_obj1, custom_obj2, custom_obj3))
-
Hi!
There have been indeed some updates to the tupledict and tuplelist implementations in version 11 to make them more Pythonic. Here's the list of updates: Release Notes for Gurobi 11.0
Our documentation on tuplelist, both version 10 and 11, explains that a list needs to be passed when creating a tuplelist. So, your code may have only worked "by accident". Can you please try updating it as follows, by adding the square brackets:
grb.tuplelist([(custom_obj1, custom_obj2, custom_obj3)])
Cheers,
Matthias0 -
There might be something else going on here too. The following runs without problem on v11.0.3 (with Python 3.11)
import gurobipy as gp
class Foo:
...
gp.tuplelist((Foo(),))- Riley
0 -
Hi Matthias, The typing issue still continues even with the list as the list is expected to have `int | float | str`
error: List item 0 has incompatible type "<Custom Python class>"; expected "int | float | str" [list-item]
0 -
Hi Riley, The code may even be able to run without issues, but I am blocked by this typing issue. MyPy, the type checker, doesn't see the custom Python class objects as valid ones in the list
0 -
Ah ok, it's a MyPy issue. Can't you just tell MyPy to ignore the error?
https://mypy.readthedocs.io/en/stable/error_codes.html0 -
That can be a last resort. But I still wonder why this error is thrown in the first place. I don't want to have unexpected results from Gurobi as I suppress this error.
0 -
The error is thrown because arbitrary objects are not intended to be used with tuplelists. The typing introduced in v11 and MyPy are doing their job as intended here.
From the tupledict docs linked by Matthias:
A
tuplelist
is designed to store tuples containing scalar values (int
,float
,string
, …). It may produce unpredictable results with other Python objectsYou can also see this in the docstring when running help(grb.tupledict) within Python. This goes back as least as far as v8 (2018).
If you want to use custom objects in a tuplelist then you can suppress the error, and you are taking a risk - it seems this is a risk that you have been unknowingly taking all along with v10 (and earlier).
If there is some functionality you use from tuplelist that you want to use with your custom objects then feel free to open up a support request (assuming you are a commercial customer) and I'll help you engineer a safe solution.
- Riley
0 -
Thanks Riley!
Indeed it seems that we have been already unknowingly taking the risk by passing the custom objects to a tuplelist.
As we didn't include `gurobipy-stubs` for gurobipy 10, we never noticed the typing issue. As type hints are included in gurobipy 11, we get to see the issue now.
Will explore further on how to deal with this issue. If required, I will then raise a support request.0
Please sign in to leave a comment.
Comments
8 comments