Skip to content

Commit

Permalink
Merge pull request astropy#17675 from neutrinoceros/tst/dep/require_p…
Browse files Browse the repository at this point in the history
…ytest_8

TST: require pytest 8.0 or newer
  • Loading branch information
pllim authored Jan 27, 2025
2 parents a1679d1 + f31ef43 commit f1b0809
Show file tree
Hide file tree
Showing 20 changed files with 133 additions and 274 deletions.
27 changes: 2 additions & 25 deletions astropy/coordinates/tests/test_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
CartesianDifferential,
)
from astropy.coordinates.tests.helper import skycoord_equal
from astropy.tests.helper import PYTEST_LT_8_0
from astropy.tests.helper import assert_quantity_allclose as assert_allclose
from astropy.time import Time
from astropy.units import allclose
Expand Down Expand Up @@ -254,23 +253,12 @@ def test_no_data_nonscalar_frames():
assert a1.temperature.shape == (3, 10)
assert a1.shape == (3, 10)

match = r".*inconsistent shapes.*"
if PYTEST_LT_8_0:
# Exception.__notes__ are ignored in matching,
# so we'll match manually and post-mortem instead
direct_match = None
else:
direct_match = match

with pytest.raises(ValueError, match=direct_match) as exc:
with pytest.raises(ValueError, match=r".*inconsistent shapes.*"):
AltAz(
obstime=Time("2012-01-01") + np.arange(10.0) * u.day,
temperature=np.ones((3,)) * u.deg_C,
)

if direct_match is None:
assert re.match(match, "\n".join(exc.value.__notes__))


def test_frame_repr():
i = ICRS()
Expand Down Expand Up @@ -746,20 +734,9 @@ def test_time_inputs():
assert c.shape == (2,)

# If the shapes are not broadcastable, then we should raise an exception.
match = r".*inconsistent shapes.*"
if PYTEST_LT_8_0:
# Exception.__notes__ are ignored in matching,
# so we'll match manually and post-mortem instead
direct_match = None
else:
direct_match = match

with pytest.raises(ValueError, match=direct_match) as exc:
with pytest.raises(ValueError, match=r".*inconsistent shapes.*"):
FK4([1, 2, 3] * u.deg, [4, 5, 6] * u.deg, obstime=["J2000", "J2001"])

if direct_match is None:
assert re.match(match, "\n".join(exc.value.__notes__))


def test_is_frame_attr_default():
"""
Expand Down
8 changes: 1 addition & 7 deletions astropy/coordinates/tests/test_iau_fullstack.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

import warnings
from contextlib import nullcontext

import erfa
import numpy as np
Expand All @@ -12,7 +11,6 @@
from astropy.coordinates import EarthLocation, SkyCoord, golden_spiral_grid
from astropy.coordinates.builtin_frames import ICRS, AltAz
from astropy.coordinates.builtin_frames.utils import get_jd12
from astropy.tests.helper import PYTEST_LT_8_0
from astropy.time import Time
from astropy.utils import iers

Expand Down Expand Up @@ -198,12 +196,8 @@ def test_future_altaz():
t = Time("J2161")

# check that these message(s) appear among any other warnings
if PYTEST_LT_8_0:
ctx = nullcontext()
else:
ctx = pytest.warns(erfa.core.ErfaWarning)
with (
ctx,
pytest.warns(erfa.core.ErfaWarning),
pytest.warns(
AstropyWarning,
match="Tried to get polar motions for times after IERS data is valid.*",
Expand Down
15 changes: 6 additions & 9 deletions astropy/coordinates/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
)
from astropy.coordinates.sites import get_builtin_sites
from astropy.table import Table
from astropy.tests.helper import PYTEST_LT_8_0, assert_quantity_allclose
from astropy.tests.helper import assert_quantity_allclose
from astropy.time import Time
from astropy.units import allclose as quantity_allclose
from astropy.utils import iers
Expand Down Expand Up @@ -252,14 +252,11 @@ def test_regression_futuretimes_4302():
else:
ctx1 = nullcontext()

ctx2 = pytest.warns(ErfaWarning, match=".*dubious year.*")

if PYTEST_LT_8_0:
ctx3 = nullcontext()
else:
ctx3 = pytest.warns(AstropyWarning, match=".*times after IERS data is valid.*")

with ctx1, ctx2, ctx3:
with (
ctx1,
pytest.warns(ErfaWarning, match=".*dubious year.*"),
pytest.warns(AstropyWarning, match=".*times after IERS data is valid.*"),
):
future_time = Time("2511-5-1")
c = CIRS(1 * u.deg, 2 * u.deg, obstime=future_time)
with iers.conf.set_temp("auto_max_age", None):
Expand Down
40 changes: 13 additions & 27 deletions astropy/coordinates/tests/test_spectral_coordinate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@
_apply_relativistic_doppler_shift,
)
from astropy.table import Table
from astropy.tests.helper import (
PYTEST_LT_8_0,
assert_quantity_allclose,
quantity_allclose,
)
from astropy.tests.helper import assert_quantity_allclose, quantity_allclose
from astropy.utils import iers
from astropy.utils.data import get_pkg_data_filename
from astropy.utils.exceptions import AstropyUserWarning, AstropyWarning
Expand Down Expand Up @@ -660,14 +656,12 @@ def test_los_shift_radial_velocity():
sc6 = sc4.with_radial_velocity_shift(-3 * u.km / u.s)
assert_quantity_allclose(sc6.radial_velocity, -2 * u.km / u.s)

if PYTEST_LT_8_0:
ctx = nullcontext()
else:
ctx = pytest.warns(
with (
pytest.warns(AstropyUserWarning, match="No velocity defined on frame"),
pytest.warns(
NoDistanceWarning, match="Distance on coordinate object is dimensionless"
)

with pytest.warns(AstropyUserWarning, match="No velocity defined on frame"), ctx:
),
):
sc7 = SpectralCoord(
500 * u.nm,
radial_velocity=1 * u.km / u.s,
Expand Down Expand Up @@ -1039,15 +1033,11 @@ def test_spectral_coord_from_sky_coord_without_distance():
with pytest.warns(AstropyUserWarning, match="No velocity defined on frame"):
coord = SpectralCoord([1, 2, 3] * u.micron, observer=obs)
# coord.target = SkyCoord.from_name('m31') # <- original issue, but below is the same but requires no remote data access
if PYTEST_LT_8_0:
ctx = nullcontext()
else:
ctx = pytest.warns(NoVelocityWarning, match="No velocity defined on frame")
with (
pytest.warns(
AstropyUserWarning, match="Distance on coordinate object is dimensionless"
),
ctx,
pytest.warns(NoVelocityWarning, match="No velocity defined on frame"),
):
coord.target = SkyCoord(ra=10.68470833 * u.deg, dec=41.26875 * u.deg)

Expand Down Expand Up @@ -1077,23 +1067,19 @@ def test_spectralcoord_accuracy(specsys):

rest = 550 * u.nm

if PYTEST_LT_8_0:
ctx = nullcontext()
else:
ctx = pytest.warns(
NoVelocityWarning,
match=(
r"^No velocity defined on frame, assuming \(0\., 0\., 0\.\) km / s\.$"
),
)
with iers.conf.set_temp("auto_download", False):
for row in reference_table:
observer = EarthLocation.from_geodetic(
-row["obslon"], row["obslat"]
).get_itrs(obstime=row["obstime"])

with (
ctx,
pytest.warns(
NoVelocityWarning,
match=(
r"^No velocity defined on frame, assuming \(0\., 0\., 0\.\) km / s\.$"
),
),
pytest.warns(
NoDistanceWarning,
match=(
Expand Down
16 changes: 6 additions & 10 deletions astropy/coordinates/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from contextlib import nullcontext

import erfa
import pytest

Expand All @@ -8,7 +6,7 @@
get_polar_motion,
)
from astropy.coordinates.solar_system import get_body_barycentric_posvel
from astropy.tests.helper import PYTEST_LT_8_0, assert_quantity_allclose
from astropy.tests.helper import assert_quantity_allclose
from astropy.time import Time
from astropy.utils import iers
from astropy.utils.exceptions import AstropyWarning
Expand All @@ -17,17 +15,15 @@
def test_polar_motion_unsupported_dates():
msg = r"Tried to get polar motions for times {} IERS.*"

if PYTEST_LT_8_0:
ctx = nullcontext()
else:
ctx = pytest.warns(erfa.core.ErfaWarning, match=".*dubious year.*")

with pytest.warns(AstropyWarning, match=msg.format("before")), ctx:
with (
pytest.warns(AstropyWarning, match=msg.format("before")),
pytest.warns(erfa.core.ErfaWarning, match=".*dubious year.*"),
):
get_polar_motion(Time("1900-01-01"))

with (
pytest.warns(AstropyWarning, match=msg.format("after")),
ctx,
pytest.warns(erfa.core.ErfaWarning, match=".*dubious year.*"),
iers.conf.set_temp("auto_max_age", None),
):
get_polar_motion(Time("2100-01-01"))
Expand Down
13 changes: 5 additions & 8 deletions astropy/cosmology/_src/tests/funcs/test_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
z_at_value,
)
from astropy.cosmology._src.funcs.optimize import _z_at_scalar_value
from astropy.tests.helper import PYTEST_LT_8_0
from astropy.units import allclose
from astropy.utils.compat.optional_deps import HAS_SCIPY
from astropy.utils.exceptions import AstropyUserWarning
Expand Down Expand Up @@ -209,11 +208,6 @@ def test_z_at_value_bracketed(method):
"""
cosmo = Planck13

if PYTEST_LT_8_0:
ctx_fval = nullcontext()
else:
ctx_fval = pytest.warns(AstropyUserWarning, match="fval is not bracketed")

if method == "Bounded":
with pytest.warns(AstropyUserWarning, match="fval is not bracketed"):
z = z_at_value(cosmo.angular_diameter_distance, 1500 * u.Mpc, method=method)
Expand All @@ -223,7 +217,10 @@ def test_z_at_value_bracketed(method):
else:
z = 0.6812777
bracket = (1.6, 2.0)
with pytest.warns(UserWarning, match="Option 'bracket' is ignored"), ctx_fval:
with (
pytest.warns(UserWarning, match="Option 'bracket' is ignored"),
pytest.warns(AstropyUserWarning, match="fval is not bracketed"),
):
assert allclose(
z_at_value(
cosmo.angular_diameter_distance,
Expand Down Expand Up @@ -319,7 +316,7 @@ def test_z_at_value_bracketed(method):
rtol=1e-6,
)

if not PYTEST_LT_8_0 and method == "Bounded":
if method == "Bounded":
ctx_bracket = pytest.warns(
UserWarning, match="Option 'bracket' is ignored by method Bounded"
)
Expand Down
9 changes: 1 addition & 8 deletions astropy/io/votable/tests/test_tree.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import filecmp
import io
from contextlib import nullcontext

import numpy as np
import pytest
Expand All @@ -10,7 +9,6 @@
from astropy.io.votable.exceptions import E26, W07, W08, W21, W41
from astropy.io.votable.table import parse
from astropy.io.votable.tree import MivotBlock, Resource, VOTableFile
from astropy.tests.helper import PYTEST_LT_8_0
from astropy.utils.data import get_pkg_data_filename


Expand Down Expand Up @@ -263,14 +261,9 @@ def test_version():
parse(io.BytesIO(begin + b"1.4" + middle + b"1.3" + end), verify="exception")
parse(io.BytesIO(begin + b"1.5" + middle + b"1.3" + end), verify="exception")

if PYTEST_LT_8_0:
ctx = nullcontext()
else:
ctx = pytest.warns(W41)

# Invalid versions
for bversion in (b"1.0", b"2.0"):
with pytest.warns(W21), ctx:
with pytest.warns(W21), pytest.warns(W41):
parse(
io.BytesIO(begin + bversion + middle + bversion + end),
verify="exception",
Expand Down
19 changes: 5 additions & 14 deletions astropy/modeling/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)
from astropy.modeling.parameters import InputParameterError, Parameter
from astropy.modeling.separable import separability_matrix
from astropy.tests.helper import PYTEST_LT_8_0, assert_quantity_allclose
from astropy.tests.helper import assert_quantity_allclose
from astropy.utils.compat.optional_deps import HAS_SCIPY


Expand Down Expand Up @@ -990,25 +990,16 @@ def test__validate_input_shapes():
assert (2, 2) == model._validate_input_shapes(inputs, model.inputs, 1)

# Fail check_broadcast
match = r".*All inputs must have identical shapes or must be scalars.*"

# Fails because the input shape of the second input has one more axis which
# for which the first input can be broadcasted to
inputs = [np.array([[1, 2], [3, 4]]), np.array([[5, 6], [7, 8], [9, 10]])]

if PYTEST_LT_8_0:
# Exception.__notes__ are ignored in matching,
# so we'll match manually and post-mortem instead
direct_match = None
else:
direct_match = match

with pytest.raises(ValueError, match=direct_match) as exc:
with pytest.raises(
ValueError,
match=r".*All inputs must have identical shapes or must be scalars.*",
):
model._validate_input_shapes(inputs, model.inputs, 1)

if direct_match is None:
assert re.match(match, "\n".join(exc.value.__notes__))


def test__remove_axes_from_shape():
model = models.Gaussian1D()
Expand Down
12 changes: 4 additions & 8 deletions astropy/nddata/tests/test_ccddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import os
import textwrap
from contextlib import nullcontext

import numpy as np
import pytest
Expand All @@ -19,7 +18,6 @@
VarianceUncertainty,
)
from astropy.table import Table
from astropy.tests.helper import PYTEST_LT_8_0
from astropy.utils import NumpyRNGContext
from astropy.utils.data import (
get_pkg_data_contents,
Expand Down Expand Up @@ -715,12 +713,10 @@ def test_wcs_keywords_removed_from_header():
data_file1 = get_pkg_data_filename(
"data/o4sp040b0_raw.fits", package="astropy.io.fits.tests"
)
if PYTEST_LT_8_0:
ctx = nullcontext()
else:
ctx = pytest.warns(FITSFixedWarning, match="'datfix' made the change")

with pytest.warns(FITSFixedWarning, match="'unitfix' made the change"), ctx:
with (
pytest.warns(FITSFixedWarning, match="'unitfix' made the change"),
pytest.warns(FITSFixedWarning, match="'datfix' made the change"),
):
ccd = CCDData.read(data_file1, unit="count")


Expand Down
Loading

0 comments on commit f1b0809

Please sign in to comment.