mirror of
https://github.com/sqlalchemy/sqlalchemy.git
synced 2026-05-27 02:52:53 -04:00
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:
Vendored
+6
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user