-
Notifications
You must be signed in to change notification settings - Fork 249
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
Prevent the same object being added more than once to a given list #1414
Conversation
by using the name `_items` for the elements of the list.
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.
Just one tiny question/suggestion for the error, but otherwise this looks good to me. I want to test this locally this afternoon and then I'll sign off. But I agree that biologically it really doesn't make sense to allow someone to add the exact same object multiple times.
neo/core/objectlist.py
Outdated
@@ -32,6 +32,10 @@ def _handle_append(self, obj): | |||
) | |||
): | |||
raise TypeError(f"Object is a {type(obj)}. It should be one of {self.allowed_contents}.") | |||
|
|||
if self._contains(obj): | |||
raise ValueError("Cannot add the same object twice") |
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.
Is there a way to be more descriptive here. You can add the same type of object, but not the same object. I'm just a little worried someone might get confused.
raise ValueError("Cannot add the same object twice") | |
raise ValueError(f"Cannot add {obj} because it is already contained within {self}") |
Not sure if that works or if you need something else.
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.
The string representation of data objects can be very long, so I don't think it's a good idea to put it in exception messages. How about "Cannot add this object because it is already contained within the list"?
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.
yeah that sounds good!
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.
done
This relates to #1405 - in the scenario in that issue, a ValueError will be raised.
(note: the change of attribute name from
contents
to_items
is because_handle_append
is also used by SpikeTrainList, which uses the name_items
).