Skip to content

Defer processing ConfigValue/ConfigList default value until first use#3394

Merged
jsiirola merged 15 commits intoPyomo:mainfrom
jsiirola:config-default
Nov 1, 2024
Merged

Defer processing ConfigValue/ConfigList default value until first use#3394
jsiirola merged 15 commits intoPyomo:mainfrom
jsiirola:config-default

Conversation

@jsiirola
Copy link
Copy Markdown
Member

Fixes #3244 .

Summary/Motivation:

NOTE: this PR builds on #3382. You can see / review the new content in this PR through this link

It is useful to have ConfigValue objects that have complex (read: heavy-weight) domains. For example, it would be convenient for a transformation to take a single configuration field for a (preconfigured) solver. Unfortunately, we would like that domain to be a SolverBase instance - and providing a default solver would force that solver to be instantiated when the ConfigValue (ConfigDict) was declared.

This PR updated the internals to defer passing the _default value through _cast until the first time the _data is accessed / needed.

In addition, this cleans up some other issues in the Config classes:

  • improve performance of serialization (avoid dicts)
  • some classes were missing __slots__ declarations
  • update the FlagType so that instances of those classes can't be constructed (attempts to call the type will return the type and not an instance)

Changes proposed in this PR:

  • (see above)

Legal Acknowledgement

By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

@jsiirola jsiirola changed the title Defer processing of COnfigValue/ConfigList default value until first use Defer processing ConfigValue/ConfigList default value until first use Oct 30, 2024
@codecov
Copy link
Copy Markdown

codecov bot commented Oct 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.59%. Comparing base (dde27f1) to head (3695d2c).
Report is 644 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3394      +/-   ##
==========================================
+ Coverage   88.54%   88.59%   +0.05%     
==========================================
  Files         883      883              
  Lines      100307   100334      +27     
==========================================
+ Hits        88813    88889      +76     
+ Misses      11494    11445      -49     
Flag Coverage Δ
linux 86.05% <100.00%> (+<0.01%) ⬆️
osx 76.04% <97.97%> (+<0.01%) ⬆️
other 86.54% <100.00%> (+<0.01%) ⬆️
win 84.38% <97.97%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

return fcn


class UninitializedMixin(object):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need (object) (we'll never stop having this argument, I know)


__slots__ = ('_declared', '_implicit_declaration', '_implicit_domain')
_all_slots = set(__slots__ + ConfigBase.__slots__)
__slots__ = ('_implicit_domain', '_declared', '_implicit_declaration')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the order matter?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. __getstate__ processes all __slots__ except for _implicit_domain in a general manner (_implicit_domain is handled separately). This processing works by skipping the first element in __slots__. See the Note on lines 2607-2608.

)

def test_display_nondata_type(self):
class NOOP(object):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not need (object)

self.assertEqual(mod_copy._visibility, 0)

def test_template_nondata(self):
class NOOP(object):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not need (object)

self.assertEqual(cfg.get('type').domain_name(), 'int')

def test_deferred_initialization(self):
class Accumulator(object):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not need (object)

@jsiirola jsiirola requested a review from mrmundt October 31, 2024 20:43
Copy link
Copy Markdown
Contributor

@emma58 emma58 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wheeeeeeeee!

Comment on lines +48 to +49
self.assertIs(NOTSET(), NOTSET)
self.assertIs(NOTSET(), NOTSET())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sick. :P

Co-authored-by: Emma Johnson <[email protected]>
@jsiirola jsiirola merged commit d90d246 into Pyomo:main Nov 1, 2024
@jsiirola jsiirola deleted the config-default branch November 1, 2024 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Defer processing ConfigDict's handling of default values until they are needed

3 participants