Skip to content

remove original unique_name on rename - #344

Merged
rozyczko merged 2 commits into
release-3.0from
drop-old-name-on-rename
Oct 23, 2026
Merged

rozyczko merged 2 commits into
release-3.0from
drop-old-name-on-rename

Conversation

@rozyczko

Copy link
Copy Markdown
Member

Changing object's unique name now removes the old name from the registry.
Attempting to set a unique name that is already registered to another object now raises a ValueError, and the object's name remains unchanged if this happens.

@rozyczko rozyczko added [scope] maintenance Code/tooling cleanup, no feature or bugfix (major.minor.PATCH) [priority] high Should be prioritized soon [area] base classes Changes to or creation of new base classes labels Oct 23, 2026
@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.75%. Comparing base (9fc6a61) to head (7200b07).
⚠️ Report is 11 commits behind head on release-3.0.

Additional details and impacted files

Impacted file tree graph

@@               Coverage Diff               @@
##           release-3.0     #344      +/-   ##
===============================================
+ Coverage        92.70%   92.75%   +0.04%     
===============================================
  Files               47       47              
  Lines             3400     3408       +8     
===============================================
+ Hits              3152     3161       +9     
+ Misses             248      247       -1     
Flag Coverage Δ
unittests 92.75% <100.00%> (+0.04%) ⬆️

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

Files with missing lines Coverage Δ
src/easyscience/base_classes/easy_base.py 98.59% <100.00%> (+0.20%) ⬆️
src/easyscience/global_object/object_registry.py 100.00% <ø> (ø)

... and 2 files with indirect coverage changes

@rozyczko
rozyczko marked this pull request as ready for review October 23, 2026 11:37
@rozyczko
rozyczko requested a review from damskii9992 October 23, 2026 11:37
Comment on lines +70 to +78
old_unique_name = self._unique_name
if new_unique_name != old_unique_name:
self._unique_name = new_unique_name
try:
registry.add(self)
except ValueError:
# Keep the old name if the new one is taken
self._unique_name = old_unique_name
raise

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.

You could just wait with setting self._unique_name until the end of the method, so if we raise when trying to add to the registry, then it never runs anyways. Saves you the "except" branch of this code.

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.

Errrr.... not really. ObjectRegistry.add(obj) gets its key by reading obj.unique_name. If the name is set after add, the object gets registered under its old name and add raises "already exists" every time.

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.

Right, idk, for some reason I though we passed the name to the add method. Nvm. then.

Comment on lines +81 to +82
# Note: This check is necessary because the registry may have been
# updated by another object with the same old name.

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.

Huh? There shouldn't be any other object with the same old name. It is unique, after all. Is this check really necessary?

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.

Indeed. In normal use it isn't needed. But it protects against a stale name: ModelBase.from_dict calls registry.discard() on a parameter which is still alive (and registry.clear() does the same thing).
That name can then go to a new object. If the stale object is renamed later, unchecked discard would remove the new object's entry.
It is very unlikely, but we know how persistent and nasty those timing issues are, so I'd keep the check.
I've reworded the comment to explain this. If you'd rather keep it simple, I'm fine dropping the check and using a plain discard.

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.

Hmm, I guess better safe than sorry, unless we discover it is a performance bottleneck (I doubt that).
Although it really shouldn't ever be necessary.

The clear method is only meant for testing, so that it breaks stuff if used in live code is only reasonable. And the ModelBase from_dict only calls discard on its temporary parameters, which should be garbage collected soon after.

So I don't really see a scenario where this check is needed, other than us/users intentionally trying to break stuff. But its a cheap safeguard, I guess.

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.

I can comment it out leaving for potential uncomment when we encounter those pesky timing issues.

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.

Nah, just merge it. The check is cheap. If it was an expensive check it'd have been a different story :)

# Then
assert global_object.registry['EasyBase_0'] is base_object
assert len(global_object.registry) == 1

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.

Might wanna add a test where you make an object with a unique_name, then rename it, then make another object with the first objects unique_name, to check that it is indeed available again.

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.

good idea. added

Base automatically changed from new-base-rename to release-3.0 October 23, 2026 12:45
@rozyczko
rozyczko merged commit b02eecd into release-3.0 Oct 23, 2026
28 checks passed
@rozyczko
rozyczko deleted the drop-old-name-on-rename branch October 23, 2026 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[area] base classes Changes to or creation of new base classes [priority] high Should be prioritized soon [scope] maintenance Code/tooling cleanup, no feature or bugfix (major.minor.PATCH)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants