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

Allow construction of CUID from another CUID #3464

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Robbybp
Copy link
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
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.

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

elif isinstance(component, ComponentUID):
Copy link
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
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
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
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
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
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__

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

elif isinstance(component, ComponentUID):
Copy link
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
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

codecov bot commented Feb 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.57%. Comparing base (095a6ed) to head (2357c03).

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     
Flag Coverage Δ
linux 86.17% <100.00%> (+<0.01%) ⬆️
osx 76.16% <100.00%> (+<0.01%) ⬆️
other 86.67% <100.00%> (+<0.01%) ⬆️
win 84.63% <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.

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.

3 participants