Repository navigation
remove original unique_name on rename - #344
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
|
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Right, idk, for some reason I though we passed the name to the add method. Nvm. then.
| # Note: This check is necessary because the registry may have been | ||
| # updated by another object with the same old name. |
There was a problem hiding this comment.
Huh? There shouldn't be any other object with the same old name. It is unique, after all. Is this check really necessary?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I can comment it out leaving for potential uncomment when we encounter those pesky timing issues.
There was a problem hiding this comment.
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 | ||
|
|
There was a problem hiding this comment.
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.
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.