From 5a2d2f47d6681ce3085f4d4e4a8d384eb442c96f Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 3 Jul 2016 21:45:15 +0900 Subject: [PATCH] `index_property` catches IndexError as well as KeyError 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 | 6 ++++++ lib/sqlalchemy/ext/indexable.py | 2 +- test/ext/test_indexable.py | 6 ++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index 09a63a7039..8ed600639c 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -21,6 +21,12 @@ .. 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 diff --git a/lib/sqlalchemy/ext/indexable.py b/lib/sqlalchemy/ext/indexable.py index 5002e9beb9..7d63485241 100644 --- a/lib/sqlalchemy/ext/indexable.py +++ b/lib/sqlalchemy/ext/indexable.py @@ -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 diff --git a/test/ext/test_indexable.py b/test/ext/test_indexable.py index c8346e4c3e..56f2e1786b 100644 --- a/test/ext/test_indexable.py +++ b/test/ext/test_indexable.py @@ -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() -- 2.47.2