-
Notifications
You must be signed in to change notification settings - Fork 84
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
With NumPy 2.0, text/plain
+ float formatting results in SphinxWarning
#610
Comments
Also want to recognize that as of this comment, NumPy 2.0 was released approximately 5 hours ago, so I'm way ahead of the curve. Happy to help contribute a fix as needed! |
numpy has pre-releases, so it’s very possible to catch and fix issues with it before the official release 😛 |
I don't think this is something that we should try to solve. I don't think we should special-case NumPy here, instead the user should either set the print options, or store the glue object as a Python float. Glue should be given something that's serialisable. I'm not 100% firm on this personally; I can see merit in a less cumbersome UX. But, with my numeric-hat on; NumPy floats are not Python floats, and this distinction matters. |
Agreed in all the contexts where we care what types are being used! But, in this specific case of text formatting for prose, I'd argue the distinction doesn't matter; as a user, I simply want something that I'm using as a number to show up as a specific format.
I understand that special-casing NumPy here is quite fraught. Where does it stop, should we also do this for SciPy objects? etc. I personally think NumPy floats are common enough that having a special case will be a huge benefit to avoid questions - and especially inscrutable errors/warnings! That said, although I think a fix is warranted, I don't whether it belongs here or in IPython or even in NumPy. Ideally, NumPy objects would know how to format themselves; on the other hand, The only reasonable solution I can see in the short term is to do something like (this is pseudo-code):
but then that takes away options from end users as far as how they want to do the conversion. So, after all that, I've convinced myself that the status quo is the best option, with the exception that I think this behavior should be documented (I had to find it in the NumPy release notes!). I'll write up a PR for a doc improvement, probably over the weekend or early next week if no one beats me to it 😄 |
Yes, I went back and forth on this too. Ultimately, doing any kind of |
If you want to serialize things for later, you need to use a serialization format, not
The only standard Python serialization format is pickle, which would mean that you’d have to have the same packages installed in the environment that unpickles the glued values as there were when pickling them. Alternatively you could make the serializer/deserializer configurable, and provide a default that can handle basic Python types and throws an error when it can’t handle something (e.g. |
@flying-sheep it would seem to me that the intention of For now, I think the best path is what we have. Long-term, things like the |
I was just looking at the formatting code: MyST-NB/myst_nb/core/variables.py Lines 202 to 218 in 6ce30cd
I wonder if it makes sense to check whether the string
Other than the problem with special-casing NumPy, the only problems I see here are numpy not imported as |
Describe the bug
context
When I do glue a NumPy float into a page with the
{glue:text}`variable:<float format>`
, I get aSphinxWarning
:expectation
I expected no warning to occur.
problem
This is a problem for people doing formatting floating point NumPy objects because if they turn warnings to errors, their pages won't build.
The cause is described here: https://numpy.org/devdocs/release/2.0.0-notes.html#representation-of-numpy-scalars-changed
Basically, the repr of NumPy types changed to include the NumPy type. As such, the IPython display formatter outputs the literal
'numpy.float64(1.2345)'
which obviously can't be formatted as a float. I'm not sure if this should be filed here or with IPython... I landed on here because it affects theglue
functionality in particular and because it seems like the display formatter for NumPy types in IPython is probably doing the right thing.The immediate workaround is as suggested in the NumPy release notes, setting
np.set_printoptions(legacy="1.25")
fixes the warning.Reproduce the bug
-W
is on.List your environment
and most importantly, NumPy >=2.0
The text was updated successfully, but these errors were encountered: