From 24170c29e50d636edbfe6020bf20e9656917a5c6 Mon Sep 17 00:00:00 2001 From: John Siirola Date: Mon, 21 Aug 2023 08:36:48 -0600 Subject: [PATCH] Support kwargs in partial objects passed to Initializer --- pyomo/core/base/initializer.py | 5 ++++- pyomo/core/tests/unit/test_initializer.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pyomo/core/base/initializer.py b/pyomo/core/base/initializer.py index 5863966e6f9..991feb0450d 100644 --- a/pyomo/core/base/initializer.py +++ b/pyomo/core/base/initializer.py @@ -157,7 +157,10 @@ def Initializer( # 'int'). We will just have to assume this is a "normal" # IndexedCallInitializer return IndexedCallInitializer(arg) - if len(_args.args) - len(arg.args) == 1 and _args.varargs is None: + _positional_args = set(_args.args) + for key in arg.keywords: + _positional_args.discard(key) + if len(_positional_args) - len(arg.args) == 1 and _args.varargs is None: return ScalarCallInitializer(arg) else: return IndexedCallInitializer(arg) diff --git a/pyomo/core/tests/unit/test_initializer.py b/pyomo/core/tests/unit/test_initializer.py index 655ca375af5..5767406bdf7 100644 --- a/pyomo/core/tests/unit/test_initializer.py +++ b/pyomo/core/tests/unit/test_initializer.py @@ -595,6 +595,17 @@ def fcn(k, i, j, m): self.assertFalse(a.contains_indices()) self.assertEqual(a(None, None), 572) + def fcn(m, k, i, j): + return i * 100 + j * 10 + k + + part = functools.partial(fcn, i=2, j=5, k=7) + a = Initializer(part) + self.assertIs(type(a), ScalarCallInitializer) + self.assertTrue(a.constant()) + self.assertFalse(a.verified) + self.assertFalse(a.contains_indices()) + self.assertEqual(a(None, None), 257) + @unittest.skipUnless(pandas_available, "Pandas is not installed") def test_dataframe(self): d = {'col1': [1, 2, 4]}