summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiana Clarke <diana.joan.clarke@gmail.com>2012-11-17 15:28:50 -0500
committerDiana Clarke <diana.joan.clarke@gmail.com>2012-11-17 15:28:50 -0500
commit40071dbda4c2467f10a1ef217ce1d6e64058fba3 (patch)
tree02126593dc602dff8617b8b78846d4d6ce1a6d32
parentdbdd0fdd48bc0baf13a614fd193ad4fd6f697840 (diff)
downloadsqlalchemy-40071dbda4c2467f10a1ef217ce1d6e64058fba3.tar.gz
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
-rw-r--r--lib/sqlalchemy/util/_collections.py1
-rw-r--r--test/base/test_utils.py33
2 files changed, 13 insertions, 21 deletions
diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py
index cefbb6d7f..0ed13f0d5 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 fe46377a2..af881af17 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'])