Skip to content

Allow construction of CUID from another CUID#3464

Merged
blnicho merged 9 commits intoPyomo:mainfrom
Robbybp:cuid-from-cuid
Feb 6, 2025
Merged

Allow construction of CUID from another CUID#3464
blnicho merged 9 commits intoPyomo:mainfrom
Robbybp:cuid-from-cuid

Conversation

@Robbybp
Copy link
Copy Markdown
Contributor

@Robbybp Robbybp commented Feb 2, 2025

Fixes

ComponentUID(ComponentUID("x[1]")) raises an error.

Summary/Motivation:

When processing keys corresponding to components, e.g., in find_component or contrib.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:

  • If the argument to ComponentUID is another ComponentUID, 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:

  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.

Copy link
Copy Markdown
Contributor Author

@Robbybp Robbybp left a comment

Choose a reason for hiding this comment

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

Some implementation questions.

except (OSError, IOError):
self._cids = tuple(self._parse_cuid_v1(component))

elif isinstance(component, ComponentUID):
Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown
Member

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)

elif isinstance(component, ComponentUID):
if context is not None:
_context_err(ComponentUID)
self._cids = copy.deepcopy(component._cids)
Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member

@jsiirola jsiirola left a 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.

Comment on lines +80 to +84
def _context_err(_type):
raise ValueError(
f"Context is not allowed when initializing a ComponentUID from {_type}."
)

Copy link
Copy Markdown
Member

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__

except (OSError, IOError):
self._cids = tuple(self._parse_cuid_v1(component))

elif isinstance(component, ComponentUID):
Copy link
Copy Markdown
Member

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)

elif isinstance(component, ComponentUID):
if context is not None:
_context_err(ComponentUID)
self._cids = copy.deepcopy(component._cids)
Copy link
Copy Markdown
Member

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
Copy link
Copy Markdown

codecov bot commented Feb 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.18%. Comparing base (095a6ed) to head (682ebbf).
Report is 27 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3464      +/-   ##
==========================================
- Coverage   88.59%   88.18%   -0.41%     
==========================================
  Files         880      880              
  Lines      100555   100556       +1     
==========================================
- Hits        89083    88672     -411     
- Misses      11472    11884     +412     
Flag Coverage Δ
linux 86.17% <100.00%> (+<0.01%) ⬆️
osx 76.16% <100.00%> (+<0.01%) ⬆️
other 86.69% <100.00%> (+0.01%) ⬆️
win 84.64% <100.00%> (+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.

Copy link
Copy Markdown
Member

@jsiirola jsiirola left a comment

Choose a reason for hiding this comment

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

Approved, pending successful tests.

@blnicho blnicho merged commit 272ba63 into Pyomo:main Feb 6, 2025
29 checks passed
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.

4 participants