Skip to content

Commit

Permalink
Merge pull request #2960 from jsiirola/initializer-partial=kwargs
Browse files Browse the repository at this point in the history
Support kwargs in partial objects passed to Initializer()
  • Loading branch information
jsiirola authored Aug 21, 2023
2 parents 0584744 + 24170c2 commit c569b78
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pyomo/core/base/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions pyomo/core/tests/unit/test_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
Expand Down

0 comments on commit c569b78

Please sign in to comment.