Serhiy Storchaka added the comment:

I'm not sure that this is an enhancement. It makes an error message 
inconsistent with other error messages.

>>> class I(int): pass
... 
>>> I()[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'I' object does not support indexing
>>> 0[0] = 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object does not support item assignment
>>> del 0[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object does not support item deletion
>>> ''['']                                                                      
>>>                                                                             
>>>                                                                             
>>>      
Traceback (most recent call last):                                              
                                                                                
                                                                             
  File "<stdin>", line 1, in <module>                                           
                                                                                
                                                                             
TypeError: string indices must be integers, not str                             
                                                                                
                                                                             
>>> iter(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> reversed(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument to reversed() must be a sequence
>>> next(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int object is not an iterator
>>> {}[[]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

And actually this message is misleading. An attribute '__getitem__' is not 
looked up on objects.

>>> class I(int): pass
... 
>>> x = I()
>>> x.__getitem__ = lambda *args: None
>>> I()[0:]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'I' object has no attribute '__getitem__'
>>> x.__getitem__
<function <lambda> at 0x7f11b62dd648>

----------
nosy: +rhettinger, serhiy.storchaka

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue31550>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to