From ac7938f99e105d3713b8d5b6d49e6affe00ca3db Mon Sep 17 00:00:00 2001 From: John Siirola Date: Wed, 10 Jan 2024 15:43:03 -0700 Subject: [PATCH 1/4] Track change in PyPy behavior starting in 7.3.14 --- pyomo/core/tests/unit/test_initializer.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pyomo/core/tests/unit/test_initializer.py b/pyomo/core/tests/unit/test_initializer.py index 5767406bdf7..a79c9c0efe6 100644 --- a/pyomo/core/tests/unit/test_initializer.py +++ b/pyomo/core/tests/unit/test_initializer.py @@ -11,6 +11,7 @@ import functools import pickle +import platform import types import pyomo.common.unittest as unittest @@ -37,6 +38,9 @@ from pyomo.environ import ConcreteModel, Var +is_pypy = platform.python_implementation().lower().startswith("pypy") + + def _init_scalar(m): return 1 @@ -561,11 +565,18 @@ def test_no_argspec(self): self.assertFalse(a.contains_indices()) self.assertEqual(a('111', 2), 7) - # Special case: getfullargspec fails on int, so we assume it is - # always an IndexedCallInitializer + # Special case: getfullargspec fails for int under CPython and + # PyPy<7.3.14, so we assume it is an IndexedCallInitializer. basetwo = functools.partial(int, '101', base=2) a = Initializer(basetwo) - self.assertIs(type(a), IndexedCallInitializer) + if is_pypy and sys.pypy_version_info[:3] >= (7, 3, 14): + # PyPy behavior diverged from CPython in 7.3.14. Arguably + # this is "more correct", so we will allow the difference to + # persist through Pyomo's Initializer handling (and not + # special case it there) + self.assertIs(type(a), ScalarCallInitializer) + else: + self.assertIs(type(a), IndexedCallInitializer) self.assertFalse(a.constant()) self.assertFalse(a.verified) self.assertFalse(a.contains_indices()) From 8b542aee6ee5558314f4b625f49b272705baaf6b Mon Sep 17 00:00:00 2001 From: John Siirola Date: Wed, 10 Jan 2024 15:43:28 -0700 Subject: [PATCH 2/4] Standardize check for PyPy --- pyomo/core/tests/unit/test_pickle.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyomo/core/tests/unit/test_pickle.py b/pyomo/core/tests/unit/test_pickle.py index 808db2e45f3..861704a2f9c 100644 --- a/pyomo/core/tests/unit/test_pickle.py +++ b/pyomo/core/tests/unit/test_pickle.py @@ -35,7 +35,7 @@ ) -using_pypy = platform.python_implementation() == "PyPy" +is_pypy = platform.python_implementation().lower().startswith("pypy") def obj_rule(model): @@ -322,7 +322,7 @@ def rule2(model, i): model.con = Constraint(rule=rule1) model.con2 = Constraint(model.a, rule=rule2) instance = model.create_instance() - if using_pypy: + if is_pypy: str_ = pickle.dumps(instance) tmp_ = pickle.loads(str_) else: From c22276fc824338991faed503f472965a15ff1749 Mon Sep 17 00:00:00 2001 From: John Siirola Date: Wed, 10 Jan 2024 16:52:09 -0700 Subject: [PATCH 3/4] Add missing import --- pyomo/core/tests/unit/test_initializer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyomo/core/tests/unit/test_initializer.py b/pyomo/core/tests/unit/test_initializer.py index a79c9c0efe6..35479612698 100644 --- a/pyomo/core/tests/unit/test_initializer.py +++ b/pyomo/core/tests/unit/test_initializer.py @@ -12,6 +12,7 @@ import functools import pickle import platform +import sys import types import pyomo.common.unittest as unittest From 131fd3f2977000ceadc903d88e9deac2c724bb81 Mon Sep 17 00:00:00 2001 From: John Siirola Date: Wed, 10 Jan 2024 19:46:08 -0700 Subject: [PATCH 4/4] fix test: ScalarCallInitializers are constant --- pyomo/core/tests/unit/test_initializer.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyomo/core/tests/unit/test_initializer.py b/pyomo/core/tests/unit/test_initializer.py index 35479612698..b334a6b857b 100644 --- a/pyomo/core/tests/unit/test_initializer.py +++ b/pyomo/core/tests/unit/test_initializer.py @@ -576,9 +576,10 @@ def test_no_argspec(self): # persist through Pyomo's Initializer handling (and not # special case it there) self.assertIs(type(a), ScalarCallInitializer) + self.assertTrue(a.constant()) else: self.assertIs(type(a), IndexedCallInitializer) - self.assertFalse(a.constant()) + self.assertFalse(a.constant()) self.assertFalse(a.verified) self.assertFalse(a.contains_indices()) # but this is not callable, as int won't accept the 'model'