Repository navigation
MNT: Stop using our own catch_warnings and raises - #10504
Conversation
| if message_to_find in str(w.message): | ||
| messages_found[i] = True | ||
| assert all(messages_found) | ||
| messages_found = ["(some) times are outside of range covered by IERS " |
There was a problem hiding this comment.
@mhvk , I tried to simplify the logic here but you might want to double check.
| altaz_auto = sc.transform_to(altaz) | ||
|
|
||
| assert len(w) == 0 | ||
| altaz_auto = sc.transform_to(altaz) |
There was a problem hiding this comment.
Note: Now that we turn all warnings into errors, except for the ones we explicitly ignore, check for zero warning is no longer necessary.
| values = np.array([1.01e200, np.inf, np.inf, 0.0, -np.inf]) | ||
| with catch_warnings(AstropyWarning) as w: | ||
| # NOTE: Warning behavior varies for the parameters being passed in. | ||
| with pytest.warns(None) as w: |
There was a problem hiding this comment.
This test file is a tricky one. nullcontext doesn't work and too strict a warns catching doesn't work either; must be due to weird interactions between pyx and pure Python. 🤷
At least this way, I think I didn't change the original intent of the tests.
| assert isinstance(t2['l'].unit, u.UnrecognizedUnit) | ||
| assert str(t2['l'].unit) == 'bandpass_sol_lum' | ||
| if table_cls is QTable: | ||
| assert len(w) == 1 |
There was a problem hiding this comment.
For whatever reason, catch_warnings did not see the ResourceWarning but pytest.warns did. 🤷
This comment has been minimized.
This comment has been minimized.
9ab3457 to
d62b324
Compare
Yes, it is a risk we have to take until we're ready to issue that deprecation warning. I removed mention of it from dev docs, so hopefully that would reduce the risk but it is not foolproof. :( |
Update: I undid my damage to |
| @pytest.mark.skipif(not HAS_BZ2, reason='Python built without bz2 module') | ||
| def test_detect_bzipped(self): | ||
| """Test detection of a bzip2 file when the extension is not .bz2.""" | ||
| with ignore_warnings(): |
There was a problem hiding this comment.
Note: I don't know why warnings were ignored blindly and turns out they are not necessary anyway.
| with pytest.raises(OSError): | ||
| fits.open(filename, ignore_missing_end=True) | ||
| with pytest.raises(OSError): | ||
| fits.open(filename, ignore_missing_end=False) |
There was a problem hiding this comment.
Note: The diff is a bit misleading. All I did was un-indent after removing with catch_warnings(ResourceWarning) as ws because that catch is now unnecessary.
mhvk
left a comment
There was a problem hiding this comment.
Looks all good, especially the cases where you found ignoring warnings was not even necessary! I also think the pytest.warns(None) as w trick is actually quite nice, maybe even better than the null context (obviously, no need to adjust!), and nice to have it mentioned in the test guide. Thanks so much for doing this huge cleanup!!
Only item left would seem to get an answer to https://github.com/astropy/astropy/pull/10504/files#r446478186, plus a few really small items if you have the energy. None feel necessary, so I am approving - would definitely be good to merge sooner rather than later!
| diffobj = HeaderDiff(ha, hb) | ||
| diffobj.report(fileobj=outpath) | ||
| with catch_warnings(AstropyDeprecationWarning) as warning_lines: | ||
| with pytest.warns(AstropyDeprecationWarning, match=r'"clobber" was ' |
There was a problem hiding this comment.
Probably OK to remove now, but a separate issue!
There was a problem hiding this comment.
If you mean removing clobber from API, I cannot find an issue for it though it was a bit controversial when the deprecation happen. It was truly deprecated in #6203 (v2.0). @saimn , do you think it is safe to finally remove now? Or is this one of those we just keep around deprecated forever. This is FITS, after all.
There was a problem hiding this comment.
I would vote to keep it forever ;)
| t1 = Table() | ||
| t1.add_column(Column(name='a', data=[1, 2, 3])) | ||
| with pytest.raises(ValueError) as exc: | ||
| with pytest.warns(UserWarning, match="table path was not set via the path= argument"): |
There was a problem hiding this comment.
Yes, surely this was just a copy & paste paste error
| def test_oversize_unicode(): | ||
| config = {'verify': 'exception'} | ||
| with catch_warnings(exceptions.W46) as w: | ||
| with pytest.raises(exceptions.W47): |
There was a problem hiding this comment.
Weird, how did catch_warnings manage to pass?
|
|
||
| assert_quantity_allclose(b_fit.temperature, 2840.7438339457754 * u.K) | ||
| assert_quantity_allclose(b_fit.bolometric_flux, 6.821837075583734e-08 * u.erg / u.cm**2 / u.s) | ||
| assert_quantity_allclose(b_fit.bolometric_flux, 6.821837075583734e-08 * u.erg / u.cm**2 / u.s) # noqa |
There was a problem hiding this comment.
Line too long, but maybe I should just fix it...
| assert w[0].category is table.TableReplaceWarning | ||
| assert 'test_table' in w[0].filename | ||
| # Make sure the warning points back to the user code line | ||
| assert w[0].lineno == frameinfo.lineno + 2 |
There was a problem hiding this comment.
I think it is simply because you now inserted the with statement in between. This perhaps is one case where it makes sense to get the frame inside it, but no need unless you are editing anyway.
| with pytest.raises(ValueError) as err: | ||
| dat.ut1_utc(Time(60000, format='mjd').jd) | ||
| assert 'interpolating from IERS_Auto using predictive values' in str(err.value) | ||
| with pytest.warns(iers.IERSStaleWarning, match='IERS_Auto predictive values are older') as warns, pytest.raises(ValueError, match='interpolating from IERS_Auto using predictive values'): # noqa |
There was a problem hiding this comment.
Would be nice to break the line (if you are doing another round...)
|
@mhvk , I think I applied all the code changes you asked for and hopefully answer all your questions/concerns. Please let me know if I missed anything. Thank you so much for doing the mega-review! |
|
As for with pytest.warns(None) as w:
some_code()
assert len(w) == 0with nullcontext():
some_code()Therefore, I think |
|
Yes, I think this can (and should!) go in, once tests have passed. And the |
|
Given the approval, I'll go ahead and merge then before I need to rebase (again). Codecov failure can be ignored. Thank you so much for the review! |
…ng in WCSAxes tests to not be sensitive to unrelated warnings
Description
This pull request is to replace our own
catch_warningswith standardpyteststuff. This addresses most of #6761 but not going to deprecate anything.Also see https://docs.pytest.org/en/stable/warnings.html .
pytestwas at 5.4.1 when this PR was opened.As for deprecation warning, local testing indicates that it will bubble through and be caught by
filterwarnings = errorinsetup.cfgeven without our owncatch_warnings.Similarly, our own
raisescan be replaced withpytest.raises.TODO
astropy/coordinates/tests/test_iau_fullstack.py:180andastropy/coordinates/tests/test_regression.py:229try to access internet on CI? I thought we already setauto_download=False. Maybe problem will fix itself after Remove unnecessary remote_data in (mostly) coordinates. #10474 is merged?astropy/io/ascii/tests/test_ecsv.py:479whenpyyamlis not installed.catch_warningsusage in remaining tests:astropy/visualization/wcsaxes/tests/test_wcsapi.py(false positive)astropy/wcs/wcsapi/tests/test_fitswcs.py(false positive)astropy/wcs/wcsapi/wrappers/tests/test_sliced_wcs.py(false positive)grepagain towards the end to make sure no newcatch_warningssnuck in upstream in the meanwhile.raise RuntimeErrorinraise.__enter__andcatch_warnings.__enter__to make sure nothing besidesfitscheck.pyis using them.astropy.tests.helper.raises?p.s. Oh dear, I didn't realize I was going to touch so many lines. Initial grep-fu failed me and I underestimated the impact. If this sits too long and conflicts start to creep in, this PR might need to be broken down into smaller pieces that can be merged faster. Or someone can come do a mega-review quick and get this in sooner than later. 😬