Collection classes require a len implementation

Issue #530 resolved
jek created an issue

InstumentedList is (accidentally?) requiring collection classes to implement {{{len}}}. Attached is a patch to use {{{len}}} if available on the collection class implementation, otherwise transparently emulate it through counting {{{iter}}}.

Comments (5)

  1. Mike Bayer repo owner

    just curious, how is __len__ actually getting called ? ( i could look myself but if you have a stack trace that would be handy).

  2. Former user Account Deleted

    i'll see if i can dig one up. i've been implementing len since the i got the first exception, so my memory is a littly hazy. it may have been in my unit tests, not in sa core. -jek

  3. Former user Account Deleted
    Traceback (most recent call last):
      File "test/orm/relationships.py", line 785, in testobj
        sess.flush()
      File "/home/jek/work/vtwo/trunk/src/ext/sqlalchemy/lib/sqlalchemy/orm/session.py", line 302, in flush
        self.uow.flush(self, objects)
      File "/home/jek/work/vtwo/trunk/src/ext/sqlalchemy/lib/sqlalchemy/orm/unitofwork.py", line 214, in flush
        flush_context.execute()
      File "/home/jek/work/vtwo/trunk/src/ext/sqlalchemy/lib/sqlalchemy/orm/unitofwork.py", line 363, in execute
        if up.preexecute(self):
      File "/home/jek/work/vtwo/trunk/src/ext/sqlalchemy/lib/sqlalchemy/orm/unitofwork.py", line 920, in preexecute
        self.processor.preprocess_dependencies(self.targettask, elements, trans, delete=False)
      File "/home/jek/work/vtwo/trunk/src/ext/sqlalchemy/lib/sqlalchemy/orm/dependency.py", line 237, in preprocess_dependencies
        childlist = self.get_object_dependencies(obj, uowcommit, passive=True)
      File "/home/jek/work/vtwo/trunk/src/ext/sqlalchemy/lib/sqlalchemy/orm/dependency.py", line 141, in get_object_dependencies
        return sessionlib.attribute_manager.get_history(obj, self.key, passive = passive)
      File "./lib/sqlalchemy/orm/attributes.py", line 868, in get_history
        return getattr(obj.__class__, key).get_history(obj, **kwargs)
      File "./lib/sqlalchemy/orm/attributes.py", line 104, in get_history
        return AttributeHistory(self, obj, current, passive=passive)
      File "./lib/sqlalchemy/orm/attributes.py", line 723, in __init__
        if current:
      File "./lib/sqlalchemy/orm/attributes.py", line 569, in __len__
        def __len__(self): return len(self.data)
    TypeError: object of type 'MyCollection' has no len()
    
  4. Log in to comment