]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
`index_property` catches IndexError as well as KeyError
authorJeong YunWon <jeong@youknowone.org>
Sun, 3 Jul 2016 12:45:15 +0000 (21:45 +0900)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 5 Jul 2016 22:24:47 +0000 (18:24 -0400)
It was raising AttributeError for key accessing in dict,
but raising IndexError for index accessing in array.

Change-Id: I58a2252a9e8d7f78cabcefcbe7223a4f3a729115

doc/build/changelog/changelog_11.rst
lib/sqlalchemy/ext/indexable.py
test/ext/test_indexable.py

index 09a63a70391a46162659bbaf7f270fd833358bb2..8ed600639c3bec879ad5d46c95a031141bba6905 100644 (file)
 .. changelog::
     :version: 1.1.0b3
 
+    .. change::
+        :tags: bug, ext
+
+        sqlalchemy.ext.indexable will intercept IndexError as well
+        as KeyError when raising as AttributeError.
+
 .. changelog::
     :version: 1.1.0b2
     :released: July 1, 2016
index 5002e9beb9257436d813c39dc4121d2e6b8cff67..7d634852416856696e1d677f0c9a84d12f54205a 100644 (file)
@@ -284,7 +284,7 @@ class index_property(hybrid_property):  # noqa
             raise AttributeError(self.attr_name)
         try:
             value = column_value[self.index]
-        except KeyError:
+        except (KeyError, IndexError):
             raise AttributeError(self.attr_name)
         else:
             return value
index c8346e4c3e0c709ea0388c06943d01e545fa3e19..56f2e1786b32c7f7726665de6248c36fc1c2df10 100644 (file)
@@ -23,9 +23,11 @@ class IndexPropertyTest(fixtures.TestBase):
             array = Column('_array', ARRAY(Integer),
                            default=[])
             first = index_property('array', 0)
+            tenth = index_property('array', 9)
 
         a = A(array=[1, 2, 3])
         eq_(a.first, 1)
+        assert_raises(AttributeError, lambda: a.tenth)
         a.first = 100
         eq_(a.first, 100)
         eq_(a.array, [100, 2, 3])
@@ -89,7 +91,7 @@ class IndexPropertyTest(fixtures.TestBase):
 
         assert_raises(AttributeError, delattr, a, "first")
 
-    def test_get_index_error(self):
+    def test_get_attribute_error(self):
         Base = declarative_base()
 
         class A(Base):
@@ -99,7 +101,7 @@ class IndexPropertyTest(fixtures.TestBase):
             first = index_property('array', 1)
 
         a = A(array=[])
-        assert_raises(IndexError, lambda: a.first)
+        assert_raises(AttributeError, lambda: a.first)
 
     def test_set_immutable(self):
         Base = declarative_base()