From: Gilles Dartiguelongue Date: Tue, 9 Dec 2014 11:08:12 +0000 (+0100) Subject: Fix slice addressing of _AssociationList with python3 X-Git-Tag: rel_1_0_0b1~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9fee9cb87e0d13db4426664f5758c1ddad0533e3;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fix slice addressing of _AssociationList with python3 --- diff --git a/lib/sqlalchemy/ext/associationproxy.py b/lib/sqlalchemy/ext/associationproxy.py index bb08ce9ba6..c0e71068bf 100644 --- a/lib/sqlalchemy/ext/associationproxy.py +++ b/lib/sqlalchemy/ext/associationproxy.py @@ -527,7 +527,10 @@ class _AssociationList(_AssociationCollection): return self.setter(object, value) def __getitem__(self, index): - return self._get(self.col[index]) + if not isinstance(index, slice): + return self._get(self.col[index]) + else: + return [self._get(member) for member in self.col[index]] def __setitem__(self, index, value): if not isinstance(index, slice): diff --git a/test/ext/test_associationproxy.py b/test/ext/test_associationproxy.py index 67e4747053..9e328a35fd 100644 --- a/test/ext/test_associationproxy.py +++ b/test/ext/test_associationproxy.py @@ -912,6 +912,22 @@ class LazyLoadTest(fixtures.TestBase): self.assert_('_children' in p.__dict__) self.assert_(len(p._children) == 3) + def test_slicing_list(self): + Parent, Child = self.Parent, self.Child + + mapper(Parent, self.table, properties={ + '_children': relationship(Child, lazy='select', + collection_class=list)}) + + p = Parent('p') + p.children = ['a', 'b', 'c'] + + p = self.roundtrip(p) + + self.assert_(len(p._children) == 3) + eq_('b', p.children[1]) + eq_(['b', 'c'], p.children[-2:]) + def test_lazy_scalar(self): Parent, Child = self.Parent, self.Child