Skip to content

Commit a9ce9b4

Browse files
authored
Merge pull request #3717 from sadavis1/solver-check-fix
Validate an argument using the new api_version() method, also implement that method on wrapper classes
2 parents 613f2ad + 3b00f60 commit a9ce9b4

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

pyomo/contrib/appsi/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,18 @@ def __enter__(self):
16861686
def __exit__(self, t, v, traceback):
16871687
pass
16881688

1689+
@classmethod
1690+
def api_version(self):
1691+
"""
1692+
Return the public API supported by this interface.
1693+
1694+
Returns
1695+
-------
1696+
~pyomo.common.enums.SolverAPIVersion
1697+
A solver API enum object
1698+
"""
1699+
return SolverAPIVersion.V1
1700+
16891701

16901702
class SolverFactoryClass(Factory):
16911703
def register(self, name, doc=None):

pyomo/contrib/solver/common/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,3 +744,15 @@ def warm_start_capable(self):
744744

745745
def default_variable_value(self):
746746
return None
747+
748+
@classmethod
749+
def api_version(self):
750+
"""
751+
Return the public API supported by this interface.
752+
753+
Returns
754+
-------
755+
~pyomo.common.enums.SolverAPIVersion
756+
A solver API enum object
757+
"""
758+
return SolverAPIVersion.V1

pyomo/contrib/solver/tests/unit/test_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def test_context_manager(self):
140140
class TestLegacySolverWrapper(unittest.TestCase):
141141
def test_class_method_list(self):
142142
expected_list = [
143+
'api_version',
143144
'available',
144145
'config_block',
145146
'default_variable_value',

pyomo/gdp/plugins/multiple_bigm.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from pyomo.common.gc_manager import PauseGC
2828
from pyomo.common.modeling import unique_component_name
2929
from pyomo.common.dependencies import dill, dill_available, multiprocessing
30+
from pyomo.common.enums import SolverAPIVersion
3031

3132
from pyomo.core import (
3233
Block,
@@ -54,8 +55,6 @@
5455
from pyomo.gdp.plugins.gdp_to_mip_transformation import GDP_to_MIP_Transformation
5556
from pyomo.gdp.util import _to_dict
5657
from pyomo.opt import SolverFactory, TerminationCondition
57-
from pyomo.contrib.solver.common.base import SolverBase as NewSolverBase
58-
from pyomo.contrib.solver.common.base import LegacySolverWrapper
5958
from pyomo.repn import generate_standard_repn
6059

6160
from weakref import ref as weakref_ref
@@ -92,11 +91,8 @@ def Solver(val):
9291
return SolverFactory(val)
9392
if not hasattr(val, 'solve'):
9493
raise ValueError("Expected a string or solver object (with solve() method)")
95-
if isinstance(val, NewSolverBase) and not isinstance(val, LegacySolverWrapper):
96-
raise ValueError(
97-
"Please pass an old-style solver object, using the "
98-
"LegacySolverWrapper mechanism if necessary."
99-
)
94+
if not hasattr(val, 'api_version') or val.api_version() is not SolverAPIVersion.V1:
95+
raise ValueError("Solver object should support the V1 solver API version")
10096
return val
10197

10298

0 commit comments

Comments
 (0)