From: Diana Clarke Date: Sat, 17 Nov 2012 20:28:50 +0000 (-0500) Subject: initializing _labels to an empty list so that the other methods don't throw exception... X-Git-Tag: rel_0_8_0b2~50 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=40071dbda4c2467f10a1ef217ce1d6e64058fba3;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git initializing _labels to an empty list so that the other methods don't throw exceptions in the None labels case, but rather return (), [], or {}. this is not backwards compatible, but doubt anyone is relying on those exceptions #2601 --- diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py index cefbb6d7f7..0ed13f0d58 100644 --- a/lib/sqlalchemy/util/_collections.py +++ b/lib/sqlalchemy/util/_collections.py @@ -33,6 +33,7 @@ class KeyedTuple(tuple): def __new__(cls, vals, labels=None): t = tuple.__new__(cls, vals) + t._labels = [] if labels: t.__dict__.update(zip(labels, vals)) t._labels = labels diff --git a/test/base/test_utils.py b/test/base/test_utils.py index fe46377a27..af881af179 100644 --- a/test/base/test_utils.py +++ b/test/base/test_utils.py @@ -13,35 +13,26 @@ class KeyedTupleTest(): keyed_tuple = util.KeyedTuple([]) eq_(type(keyed_tuple), util.KeyedTuple) eq_(str(keyed_tuple), '()') - eq_(keyed_tuple.__dict__, {}) + eq_(len(keyed_tuple), 0) - # TODO: consider returning an empty [] rather than raising - assert_raises(AttributeError, keyed_tuple.keys) - - # TODO: consider returning an empty {} rather than raising - assert_raises(AttributeError, keyed_tuple._asdict) - - # TODO: consider returning an empty () rather than raising - def should_raise(): - keyed_tuple._fields - assert_raises(AttributeError, should_raise) + eq_(keyed_tuple.__dict__, {'_labels': []}) + eq_(keyed_tuple.keys(), []) + eq_(keyed_tuple._fields, ()) + eq_(keyed_tuple._asdict(), {}) def test_values_but_no_labels(self): keyed_tuple = util.KeyedTuple([1, 2]) eq_(type(keyed_tuple), util.KeyedTuple) eq_(str(keyed_tuple), '(1, 2)') - eq_(keyed_tuple.__dict__, {}) - - # TODO: consider returning an empty [] rather than raising - assert_raises(AttributeError, keyed_tuple.keys) + eq_(len(keyed_tuple), 2) - # TODO: consider returning an empty {} rather than raising - assert_raises(AttributeError, keyed_tuple._asdict) + eq_(keyed_tuple.__dict__, {'_labels': []}) + eq_(keyed_tuple.keys(), []) + eq_(keyed_tuple._fields, ()) + eq_(keyed_tuple._asdict(), {}) - # TODO: consider returning an empty () rather than raising - def should_raise(): - keyed_tuple._fields - assert_raises(AttributeError, should_raise) + eq_(keyed_tuple[0], 1) + eq_(keyed_tuple[1], 2) def test_basic_creation(self): keyed_tuple = util.KeyedTuple([1, 2], ['a', 'b'])