-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve TypeError error message when trying to iterate over an object of type 'int' #118598
Comments
Hi! Thanks for the issue.
I think we cannot make this assumption blindly. In many cases
>>> list(range(0))
[]
>>> list(range(-1))
[]
I would like not to recommend Maybe start with a discussion on https://discuss.python.org ? |
I agree with @sobolevn , this is a change that might introduce a lot of false positive. I agree that may new users probably have had the issue you mentioned, but your solution is too broad. For example Also, your fix checks the exception string for such case, which would also be a bit hacky. Overall I believe this change has a too large unwanted impact, and I also agree that this should be discussed on dpo first. |
Add fix hints to exception messages, as here;
was started just a few versions ago. I agree with others that proposal/patch as is is too broad. |
Feature or enhancement
Proposal:
Currently, in situations like
or the analogous list comprehension situation, the error message of the ensuing TypeError is
'int' object is not iterable
The improved error message would look like:
'int' object is not iterable. Did you mean: 'range('int')'?
The original TypeError error message
'int' object is not iterable
results from other prohibited operations as well, such asin which case the proposed suggestion is irrelevant. However, based on searching on StackOverflow "'int' object is not iterable", the majority of issues fall into the first situation.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
It is fairly common to try to iterate over an object of type int. Here you may find some guiding links with examples:
Simple for loops:
https://www.codecademy.com/forum_questions/54f231c876b8fe4269002f2f
https://stackoverflow.com/questions/43425270/typeerror-int-object-is-not-iterable
https://blog.arrowhitech.com/typeerror-int-object-is-not-iterable-in-python-what-is-it-and-solution/
List comprehensions:
https://stackoverflow.com/questions/32312371/typeerror-int-object-is-not-iterable
Also, when I searched for "TypeError: 'int' object is not iterable" on StackOverflow, I got the impression that the majority of the issues might be solved with "range('int')", see here:
https://stackoverflow.com/search?q=TypeError%3A+%27int%27+object+is+not+iterable
Linked PRs
The text was updated successfully, but these errors were encountered: