Skip to content

Commit cbfbcb6

Browse files
committed
FEAT: better error message for AxisCollection.index(name)
this is also Python3.14-proof, while we used to rely on the list.index builtin message, which changed (for the worse IMO) in Python 3.14
1 parent e8d4dfa commit cbfbcb6

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

larray/core/axis.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2302,7 +2302,10 @@ def index(self, axis, compatible=False) -> int:
23022302
name = axis
23032303
if name is None:
23042304
raise ValueError(f"{axis!r} is not in collection")
2305-
return self.names.index(name)
2305+
try:
2306+
return self.names.index(name)
2307+
except ValueError:
2308+
raise ValueError(f"axis {name!r} is not in collection")
23062309

23072310
# XXX: we might want to return a new AxisCollection (same question for other inplace operations:
23082311
# append, extend, pop, __delitem__, __setitem__)

larray/tests/test_axiscollection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ def test_contains(col):
221221

222222
def test_index(col):
223223
assert col.index('lipro') == 0
224-
with must_raise(ValueError, msg="'nonexisting' is not in list"):
224+
with must_raise(ValueError, msg="axis 'nonexisting' is not in collection"):
225225
col.index('nonexisting')
226-
assert col.index(0) == 0
226+
assert col.index(0) == 0
227227
assert col.index(1) == 1
228228
assert col.index(2) == 2
229229
assert col.index(-1) == -1
@@ -237,9 +237,9 @@ def test_index(col):
237237
assert col.index(sex) == 1
238238
assert col.index(age) == 2
239239
assert col.index(sex2) == 1
240-
with must_raise(ValueError, msg="'geo' is not in list"):
240+
with must_raise(ValueError, msg="axis 'geo' is not in collection"):
241241
col.index(geo)
242-
with must_raise(ValueError, msg="'value' is not in list"):
242+
with must_raise(ValueError, msg="axis 'value' is not in collection"):
243243
col.index(value)
244244

245245
# test anonymous axes

0 commit comments

Comments
 (0)