-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix the error- TypeError: 'float' object is not iterable #22710
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for opening your first PR into Matplotlib!
If you have not heard from us in a while, please feel free to ping @matplotlib/developers
or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.
You can also join us on gitter for real-time discussion.
For details on testing, writing docs, and our review process, please see the developer guide
We strive to be a welcoming and open project. Please follow our Code of Conduct.
Thanks for your contribution! Can you please provide a reproducible piece of code that failed before, but this fixes. Thanks! |
The second argument to set_xticks() is a list of labels. This shows a scalar float being passed in. The bug is in that package. and I am looking at main right now, and axis.py at these lines don't look like this, so I am quite confused about what is going on... |
Ah, I was taken to the specific commit rather than the PR's changes page, which is why I was confused. Yeah, this isn't a bug in matplotlib at all. The bug is in the other package's incorrect use of |
I see. Sorry, my misunderstanding. Wouldn't my changes however, also make it work in the case where someone uses set_xticks() incorrectly? Asking because I don't understand all the working parts of matplotlib underneath the hood |
No, it actually breaks things further for correct use-cases (by not working for an iterable of strings), and it wouldn't fix anything for your use-case. The code expects an iterable, and the function documentation says that it expects an iterable, and it is getting a single float value that can't serve as a list of labels. |
If there is any bug here, it is that matplotlib probably could have raise an error sooner in the stack rather than deeper down in the internals. |
Thank you! I would like to help with raising the error, where should I start? What file would I be working on? |
So, looking at the very interesting set of indirections we have (we avoid having duplicate code for x and y axis), it appears that this function actually is near the top of the stack that one could reasonably do a check. One could also do a check up in this file's So, basically, check that it is an iterable and if not, raise a TypeError. I think we have some functions for that somewhere in a utils file... |
Ok, will do |
@WeatherGod Wouldn't try:
_ = (e for e in ticklabels)
except TypeError:
print(ticklabels, 'is not iterable') be sufficient? |
So, really, the objective here is a better exception message, so we'd just capture the TypeError from the list comprehension, and raise a new TypeError like so:
The |
Maybe "sequence" is a better term, since a string is an iterable, but would be an incorrect input. |
try:
_ = (e for e in ticklabels)
except TypeError:
raise TypeError(f"{ticklabels:=} must be a sequence") from None Would this work? |
I've moved to draft until you sort out your approach. Also a reproducible self-contained example would be appreciated. You can't expect us all to download "EmotionDetection"! In lieu of this, you should add self-contained tests. Thanks.... |
@jklymak Got it but I was referring to the approach @WeatherGod suggested and adding that to set_ticklabels() function |
@Krish-sysadmin Yes, that approach looks good (but I would just wrap the current list comprehension rather than adding a new local variable for a generator). #22710 (comment) This approach of catching errors, printing, and then carrying on is something you should almost never do in a library context. We do not know enough to know that the user wanted so we can not fix it. It is then our job to tell the users something went wrong. It is then up to the application layer to catch that exception and do something about it (because at the application layer they know things might go wrong and how to fix them or know that this was an optional thing that can be ignored or ...) or pass the exception up to their caller who might know what to do and so on and so on up the callstack. In an interactive context you then hit the shell who says "right, I'll just print out the whole traceback for the human to look at" or in a script context Python say "I'm going to print the traceback, but then exit because I have nothing else I can do". Quoting the Zen of Python https://peps.python.org/pep-0020/#the-zen-of-python
I would add that I was doing some user support last week where the user had done exactly this pattern ( PS sorry, you tried to fix a bug and instead got a software design philosophy discussion ;) |
Thanks, Thomas. If I understand correctly, this is alright then. try:
[e for e in ticklabels]
except TypeError:
raise TypeError(f"{ticklabels:=} must be a sequence") from None ? |
No, we mean putting the try...except around this:
Of course, this is always a risk that |
try:
ticklabels = [t.get_text() if hasattr(t, 'get_text') else t
for t in ticklabels]
except:
raise TypeError(f"{ticklabels:=} must be a sequence") from None |
@WeatherGod Now Linux tests fail for some reason. I suspect macos will too |
Wait, now all Linux, macOS and Windows tests fail |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one linting error. The other errors are happening for reasons outside our control and is getting worked on separately.
Does anybody know if the build and test problems are fixed on the main branch? Would merging main into this branch likely fix it? |
I have no authority here, but I think you need #22766? |
This should be squash-merged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With a squash merge or a rebase+squash.
For 3.7? |
So all the tests pass. Can this be merged? |
Thanks @Krish-sysadmin, and congratulations on your first contribution to Matplotlib. We hope to see you back!
We aim for a concise commit history. Typically that means one or very few logically grouped commits. It's helpful if you could do this in future pull requests yourself. That was the comment in @tacaswell's approval, which prevented immedate merging. - I've now squash-merged all the commits into one. No worries if you need help with handling git to directly have only one commit in your PR even with updates. We can help you in a next PR if needed. |
Thanks, Tim! |
PR Summary
This commit fixes the issue "TypeError: 'float' object is not iterable" that I faced while training a machine learning model.
PR Checklist
Tests and Styling
pytest
passes).flake8-docstrings
and runflake8 --docstring-convention=all
).Documentation
doc/users/next_whats_new/
(follow instructions in README.rst there).doc/api/next_api_changes/
(follow instructions in README.rst there).