larray-project / larray

N-dimensional labelled arrays in Python
https://larray.readthedocs.io/
GNU General Public License v3.0
8 stars 6 forks source link

unsupported types in getitem must raise either TypeError or ValueError, not AssertionError #1095

Open gdementen opened 7 months ago

gdementen commented 7 months ago

Unsure which though.

>>> arr = ndtest((2, 3))
>>> arr[object()]
AssertionError: <object object at 0x000001F2737241B0> (<class 'object'>) is not scalar
>>> arr[None]
AssertionError: None (<class 'NoneType'>) is not scalar
>>> class A:
...     def __eq__(self):
...         return False
>>> a = A()
>>> arr[a]
AssertionError: <larray_editor.api.A object at 0x000001F2746AF190> (<class 'larray_editor.api.A'>) is not scalar

Compare this with dict:

>>> d = {}
>>> d[object()]
KeyError: <object object at 0x000001F26E653520>
>>> d[None]
KeyError: None
>>> d[a]
TypeError: unhashable type: 'A'

I also wonder why we raise ValueError when a key is not found instead of KeyError or at least some kind of LookupError? I remember looking at StackOverflow for this, but I don't remember the reasoning anymore.

>>> arr['b4']
ValueError: 'b4' is not a valid label for any axis:
 a [2]: 'a0' 'a1'
 b [3]: 'b0' 'b1' 'b2'