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
This commit is contained in:
Jeong YunWon
2016-07-03 21:45:15 +09:00
committed by Mike Bayer
parent 7c8c124dbe
commit 5a2d2f47d6
3 changed files with 11 additions and 3 deletions
+6
View File
@@ -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
+1 -1
View 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
+4 -2
View 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()