Skip to content

Multiple bugs in ObjectList #1935

Closed

Description

@apdavison

Describe the bug

I started looking into #1927 (the O(n²) cost of ObjectList.append), I found a number of correctness bugs in ObjectList (neo/core/objectlist.py), and one in Container. They're separate from the performance problem, so I'm collecting them here to be fixed first.

To Reproduce / Expected behavior

The examples below use:

import neo, quantities as pq

def sig():
    return neo.AnalogSignal([[1], [2]] * pq.mV, sampling_rate=1 * pq.kHz)

seg = neo.Segment()

1. extend() and += don't detect duplicates within the iterable being added (as noted by @drodarie in #1927).

a = sig()
seg.analogsignals.extend([a, a])
len(seg.analogsignals)  # 2

2. extend() and += with a generator add nothing. The validation loop uses up the iterator, so _items.extend() gets an empty one. The objects still get their parent set.

a = sig()
seg.analogsignals.extend(x for x in [a])
len(seg.analogsignals)  # 0
a.segment is seg        # True

3. in, index(), count() and remove() compare by equality, not identity. For multi-sample signals this raises an exception whenever the object isn't the first item. It can also match a different object with equal data. The duplicate check in append() already uses identity.

seg.analogsignals.append(sig())
sig() in seg.analogsignals
# ValueError: The truth value of an array with more than one element is ambiguous.

4. reverse() uses raise instead of return.

seg.analogsignals.reverse()
# TypeError: exceptions must derive from BaseException

5. extend() isn't atomic. If an object fails validation, the objects before it have already had their parent set, but none of them are added.

a = sig()
seg.analogsignals.extend([a, neo.Event()])  # TypeError, as expected
a.segment is seg                            # True, but a is not in seg.analogsignals

6. __setitem__ skips the type, duplicate and parent checks.

seg.analogsignals.append(sig())
seg.analogsignals[0] = neo.Event()  # accepted

7. Assigning anything other than a list or ObjectList to a container's child list is silently ignored. Container._set_object_list (neo/core/container.py) builds a TypeError but never raises it.

seg.analogsignals = (sig(),)
len(seg.analogsignals)  # 0, no error

SpikeTrainList overrides append, extend, __setitem__ and += with copies of the same logic, so it has bugs 1, 2, 5 and 6 as well.

Environment:

  • OS: macOS
  • Python version: 3.13.14
  • Neo version: current master [442658a]
  • NumPy version: 2.5.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions