-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
BUG: fix yaml table serialization compatibility with numpy 2 #16416
BUG: fix yaml table serialization compatibility with numpy 2 #16416
Conversation
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
👋 Thank you for your draft pull request! Do you know that you can use |
08c5a4f
to
4444cf3
Compare
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.
Thanks!
Diff LGTM but I'll let subpackage maintainer(s) review and approve. Thanks! |
def represent_float(self, data): | ||
# Override to change repr(data) to str(data) since otherwise all the | ||
# numpy scalars fail in not NUMPY_LT_1_20. | ||
if data != data or (data == 0.0 and data == 1.0): |
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 to make sure I understand, why not np.isnan?
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.
I assume that's for performance reasons, though, that's a question for @mhvk
(math.isnan
should also be faster than np.isnan
for scalars)
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.
Hmm, I vaguely recall that I just copied the snipped from how pyyaml does it, except changing repr(data)
to str(data)
(I clearly should have added a note with that...). I'm not quite sure why they worked this way - perhaps there are python float-like's that do not support isnan
?
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.
Ok no problem - if we can get things to work with math.isnan
I think that would be preferable
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.
For the record here's the line you apparently copied https://github.com/yaml/pyyaml/blob/48838a3c768e3d1bcab44197d800145cfd0719d6/lib/yaml/representer.py#L172
It was written 15 years ago (circa 2009), while math.isnan
was introduced in Python 2.6, released in October 2008, so it's possible that the only reason they didn't use it was backward compatibility, and it just never got refactored.
Since the whole function is copied from pyyaml, it seems best to keep it as is, but I'll add a note so this knowledge doesn't get lost.
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.
FYI @mhvk I also updated your comment. It said that numpy scalars failed on "not NUMPY_LT_1_20" which I assume was a mistake and you really meant "not NUMPY_LT_2_0"
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.
Thanks, @neutrinoceros, that all makes sense.
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.
Beyond the two in-line comments, shouldn't we also get the next commit, for complex
? 6b7916d4e3736eaac73ca25372c341079ae38291
astropy/io/misc/tests/test_yaml.py
Outdated
|
||
|
||
def test_serialize_mixin_column(tmp_path): | ||
# see https://github.com/astropy/astropy/issues/16414 |
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.
I'm confused why our regular tests didn't catch this. We test a whole slew of different SkyCoord
for all serializations (see astropy/io/tests/mixin_columns.py
) - was the problem specific to frame="galactic"
? If so, perhaps add an extra SkyCoord
in mixin_columns
? (Though note that one then needs to add quite a bit of detail further down in the file...) Though in a way also fine just as is - after all, it is the incantation that proved there was a problem.
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.
The problem is actually not specific to frame="galactic"
, so I'll drop this argument to avoid confusion.
According to its docstring, astropy/io/tests/mixin_columns.py
is used in ascii/tests/test_ecsv.py
, fits/tests/test_connect.py
, and misc/tests/test_hdf5.py
, but not in yaml tests (yet). I'll see if there are any problems with using it here too.
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.
I've wrote a new test based on test_hdf5.py::test_hdf5_mixins_qtable_to_table
, which also fails on main and pass with this branch. I ditched the first test, let me know if I should keep both.
def represent_float(self, data): | ||
# Override to change repr(data) to str(data) since otherwise all the | ||
# numpy scalars fail in not NUMPY_LT_1_20. | ||
if data != data or (data == 0.0 and data == 1.0): |
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.
Hmm, I vaguely recall that I just copied the snipped from how pyyaml does it, except changing repr(data)
to str(data)
(I clearly should have added a note with that...). I'm not quite sure why they worked this way - perhaps there are python float-like's that do not support isnan
?
9a7c15d
to
0f855e8
Compare
It doesn't hurt and is clearly correct, but I should point out that it's also not tested. I'll push it now. |
astropy/io/misc/tests/test_hdf5.py
Outdated
@@ -707,7 +707,7 @@ def assert_objects_equal(obj1, obj2, attrs, compare_class=True): | |||
|
|||
|
|||
@pytest.mark.skipif(not HAS_H5PY, reason="requires h5py") | |||
def test_hdf5_mixins_qtable_to_table(tmp_path): | |||
def code(tmp_path): |
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.
Why this change?
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.
wow, that's a syntaxically valid mistake I must have made by trying to open vscode from another editor and typing in the wrong tab. Thanks for spotting it
astropy/io/misc/tests/test_yaml.py
Outdated
@@ -316,3 +319,57 @@ def test_yaml_load_of_object_arrays_fail(): | |||
order: C | |||
shape: !!python/tuple [3]""" | |||
) | |||
|
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.
This regression test is fine but more importantly there should be YAML-level unit tests covering all the cases in the new code. I.e. serializing a simple dict structure with all the impacted data types and confirming the output and correct round-trip.
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.
Gotcha. I'll work on it.
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.
Actually the unit tests in question already exists, I just needed to add a pytest fixture to compensate for global settings (see #15096).
They also capture the regressions properly, so maybe we should just ditch the more complex integration test, if that's okay with you.
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.
Excellent about the tests already being there! And yes, I'm good with dropping the integration test.
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.
great, I rebased and greatly simplified the branch !
astropy/io/misc/tests/test_yaml.py
Outdated
} | ||
|
||
|
||
def test_fits_yaml_mixins_qtable_to_table(tmp_path): |
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.
I think this test should live in test_mixin.py
instead of test_yaml.py
(which should be more low-level YAML).
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.
Putting it here is consistent with how the "inspiration" test lives in test_hdf5.py
. Moving that one would be out of scope here but maybe we can move both in a follow up PR ?
2d8894a
to
928098f
Compare
928098f
to
43a42ae
Compare
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.
Ah, thanks, now I understand why the tests didn't have a problem before - it was the use of legacy printing. With this, it looks all great!
…ility with numpy 2
Sorry, @taldcroft, I merged perhaps too quickly! A problem with trying to do a few things while really not at work; should just have approved and left it. Though hopefully you were OK with the latest push too... |
@mhvk @neutrinoceros - Sometimes codecov seems to give false reports, but here I don't see any coverage of the |
added to my todo list for tomorrow ! |
…416-on-v6.1.x Backport PR #16416 on branch v6.1.x (BUG: fix yaml table serialization compatibility with numpy 2)
Thanks, all! I opened a follow-up issue at #16429 |
Description
Combines the regression test from #16414 and cherry-picks 9cdd95d and 3be8af0 from #15065
This doesn't break any test that I could run locally against numpy 1.26, so it should be backportable, but I'm still keeping it as a draft until I see CI go green, just in case.
Fix #15792
Also resolves ~90% failures (340/380) from tests that currently rely on legacy print options as setup in
astropy/astropy/conftest.py
Line 28 in b2e38be