Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support kwargs in partial objects passed to Initializer() #2960

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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