[issue32259] Misleading "not iterable" Error Message when generator return a "simple" type, and a tuple is expected

Serhiy Storchaka report at bugs.python.org
Sun Dec 10 17:54:01 EST 2017


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

Camion, the problem is not in the error message. The problem is in the complex expression that produces an error. If you have a complex expression you always have a change to misidentify the source of error. In your case the expression contains two implicit iter() invocations.

I have looked at the code. It is possible to make the error message in this case more specific. There are precedences of this when pass wrong type as var-positional or var-keyword arguments:

>>> print(*1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: print() argument after * must be an iterable, not int
>>> print(**1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: print() argument after ** must be a mapping, not int

In the following cases the error message is general. It is possible to make it more specific.

>>> a, b = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> [*1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> {*1}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> {**1}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not a mapping

But PyObject_GetIter() is called in 88 places in CPython core and extensions. In all these cases we can make error messages more specific. But I have large doubts that it is worth to do. And I'm not sure even about the above examples.

If somebody provide a patch we could discuss it (I'm not sure it will be accepted at end).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32259>
_______________________________________


More information about the Python-bugs-list mailing list