-
Notifications
You must be signed in to change notification settings - Fork 526
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
Allow construction of CUID from another CUID #3464
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some implementation questions.
pyomo/core/base/componentuid.py
Outdated
try: | ||
self._cids = tuple(self._parse_cuid_v2(component)) | ||
except (OSError, IOError): | ||
self._cids = tuple(self._parse_cuid_v1(component)) | ||
|
||
elif isinstance(component, ComponentUID): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In find_component
, we used type is ComponentUID
instead of isinstance
, presumably for performance. Should we do the same here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, yes. (If for no other reason than consistency - a derived class would work here, but not in find_component
)
pyomo/core/base/componentuid.py
Outdated
elif isinstance(component, ComponentUID): | ||
if context is not None: | ||
_context_err(ComponentUID) | ||
self._cids = copy.deepcopy(component._cids) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, _cids
shouldn't contain anything mutable, so we may not need the copy here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that is correct, and would be in favor of the change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple super minor changes that would be nice to include and this should be good to go.
pyomo/core/base/componentuid.py
Outdated
def _context_err(_type): | ||
raise ValueError( | ||
f"Context is not allowed when initializing a ComponentUID from {_type}." | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably move this up to the module scope so we don't take the hit of re-creating the function at every call ti __init__
pyomo/core/base/componentuid.py
Outdated
try: | ||
self._cids = tuple(self._parse_cuid_v2(component)) | ||
except (OSError, IOError): | ||
self._cids = tuple(self._parse_cuid_v1(component)) | ||
|
||
elif isinstance(component, ComponentUID): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, yes. (If for no other reason than consistency - a derived class would work here, but not in find_component
)
pyomo/core/base/componentuid.py
Outdated
elif isinstance(component, ComponentUID): | ||
if context is not None: | ||
_context_err(ComponentUID) | ||
self._cids = copy.deepcopy(component._cids) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that is correct, and would be in favor of the change.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3464 +/- ##
==========================================
- Coverage 88.59% 88.57% -0.02%
==========================================
Files 880 880
Lines 100555 100557 +2
==========================================
- Hits 89083 89073 -10
- Misses 11472 11484 +12
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Fixes
ComponentUID(ComponentUID("x[1]"))
raises an error.Summary/Motivation:
When processing keys corresponding to components, e.g., in
find_component
orcontrib.mpc.get_indexed_cuid
, we sometimes want to convert a key into a CUID so it can be handled more consistently. However, we have to handle separately the case where the key is already a CUID. This PR proposes to allow construction of a CUID from another CUID.Changes proposed in this PR:
ComponentUID
is anotherComponentUID
, we just copy the_cids
attribute onto the new instance.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: