Skip to content

Commit

Permalink
Merge pull request #8949 from dhomeier/pytest-5-excinfo-fixes
Browse files Browse the repository at this point in the history
Resolve str AssertionErrors on ExceptionInfo objects in pytest 5
  • Loading branch information
bsipocz committed Jul 7, 2019
1 parent ded92fd commit 6bbd284
Show file tree
Hide file tree
Showing 30 changed files with 116 additions and 116 deletions.
8 changes: 4 additions & 4 deletions astropy/coordinates/tests/test_angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,13 +650,13 @@ def test_latitude():
with pytest.raises(TypeError) as excinfo:
lon = Longitude(10, 'deg')
lat = Latitude(lon)
assert "A Latitude angle cannot be created from a Longitude angle" in str(excinfo)
assert "A Latitude angle cannot be created from a Longitude angle" in str(excinfo.value)

with pytest.raises(TypeError) as excinfo:
lon = Longitude(10, 'deg')
lat = Latitude([20], 'deg')
lat[0] = lon
assert "A Longitude angle cannot be assigned to a Latitude angle" in str(excinfo)
assert "A Longitude angle cannot be assigned to a Latitude angle" in str(excinfo.value)

# Check we can work around the Lat vs Long checks by casting explicitly to Angle.
lon = Longitude(10, 'deg')
Expand Down Expand Up @@ -738,13 +738,13 @@ def test_longitude():
with pytest.raises(TypeError) as excinfo:
lat = Latitude(10, 'deg')
lon = Longitude(lat)
assert "A Longitude angle cannot be created from a Latitude angle" in str(excinfo)
assert "A Longitude angle cannot be created from a Latitude angle" in str(excinfo.value)

with pytest.raises(TypeError) as excinfo:
lat = Latitude(10, 'deg')
lon = Longitude([20], 'deg')
lon[0] = lat
assert "A Latitude angle cannot be assigned to a Longitude angle" in str(excinfo)
assert "A Latitude angle cannot be assigned to a Longitude angle" in str(excinfo.value)

# Check we can work around the Lat vs Long checks by casting explicitly to Angle.
lat = Latitude(10, 'deg')
Expand Down
6 changes: 3 additions & 3 deletions astropy/coordinates/tests/test_earth.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ def test_of_address(google_api_key):
loc = EarthLocation.of_address("New York, NY")
except NameResolveError as e:
# API limit might surface even here in Travis CI.
if 'unknown failure with' not in str(e):
pytest.xfail(str(e))
if 'unknown failure with' not in str(e.value):
pytest.xfail(str(e.value))
else:
assert quantity_allclose(loc.lat, NYC_lat, atol=NYC_tol)
assert quantity_allclose(loc.lon, NYC_lon, atol=NYC_tol)
Expand All @@ -323,7 +323,7 @@ def test_of_address(google_api_key):
# Buffer above sometimes insufficient to get around API limit but
# we also do not want to drag things out with time.sleep(0.195),
# where 0.195 was empirically determined on some physical machine.
pytest.xfail(str(e))
pytest.xfail(str(e.value))
else:
assert quantity_allclose(loc.lat, NYC_lat, atol=NYC_tol)
assert quantity_allclose(loc.lon, NYC_lon, atol=NYC_tol)
Expand Down
26 changes: 13 additions & 13 deletions astropy/coordinates/tests/test_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class TestAttributes(metaclass=OrderedDescriptorContainer):
# Make sure setting values via public attribute fails
with pytest.raises(AttributeError) as err:
t.attr_none = 5
assert 'Cannot set frame attribute' in str(err)
assert 'Cannot set frame attribute' in str(err.value)


def test_frame_subclass_attribute_descriptor():
Expand Down Expand Up @@ -171,7 +171,7 @@ def test_no_data_nonscalar_frames():
with pytest.raises(ValueError) as exc:
AltAz(obstime=Time('2012-01-01') + np.arange(10.) * u.day,
temperature=np.ones((3,)) * u.deg_C)
assert 'inconsistent shapes' in str(exc)
assert 'inconsistent shapes' in str(exc.value)


def test_frame_repr():
Expand Down Expand Up @@ -517,18 +517,18 @@ def test_time_inputs():

with pytest.raises(ValueError) as err:
c = FK4(1 * u.deg, 2 * u.deg, equinox=1.5)
assert 'Invalid time input' in str(err)
assert 'Invalid time input' in str(err.value)

with pytest.raises(ValueError) as err:
c = FK4(1 * u.deg, 2 * u.deg, obstime='hello')
assert 'Invalid time input' in str(err)
assert 'Invalid time input' in str(err.value)

# A vector time should work if the shapes match, but we don't automatically
# broadcast the basic data (just like time).
FK4([1, 2] * u.deg, [2, 3] * u.deg, obstime=['J2000', 'J2001'])
with pytest.raises(ValueError) as err:
FK4(1 * u.deg, 2 * u.deg, obstime=['J2000', 'J2001'])
assert 'shape' in str(err)
assert 'shape' in str(err.value)


def test_is_frame_attr_default():
Expand Down Expand Up @@ -598,7 +598,7 @@ def test_representation():
for attr in ('ra', 'dec', 'distance'):
with pytest.raises(AttributeError) as err:
getattr(icrs, attr)
assert 'object has no attribute' in str(err)
assert 'object has no attribute' in str(err.value)

# Testing when `_representation` set to `CylindricalRepresentation`.
icrs.representation_type = r.CylindricalRepresentation
Expand All @@ -619,7 +619,7 @@ def test_representation():
for attr in ('x', 'y', 'z'):
with pytest.raises(AttributeError) as err:
getattr(icrs, attr)
assert 'object has no attribute' in str(err)
assert 'object has no attribute' in str(err.value)

# Testing setter input using text argument for cylindrical.
icrs.representation_type = 'cylindrical'
Expand All @@ -629,11 +629,11 @@ def test_representation():

with pytest.raises(ValueError) as err:
icrs.representation_type = 'WRONG'
assert 'but must be a BaseRepresentation class' in str(err)
assert 'but must be a BaseRepresentation class' in str(err.value)

with pytest.raises(ValueError) as err:
icrs.representation_type = ICRS
assert 'but must be a BaseRepresentation class' in str(err)
assert 'but must be a BaseRepresentation class' in str(err.value)


def test_represent_as():
Expand Down Expand Up @@ -704,11 +704,11 @@ def test_dynamic_attrs():

with pytest.raises(AttributeError) as err:
c.blahblah
assert "object has no attribute 'blahblah'" in str(err)
assert "object has no attribute 'blahblah'" in str(err.value)

with pytest.raises(AttributeError) as err:
c.ra = 1
assert "Cannot set any frame attribute" in str(err)
assert "Cannot set any frame attribute" in str(err.value)

c.blahblah = 1
assert c.blahblah == 1
Expand Down Expand Up @@ -1011,12 +1011,12 @@ def test_missing_component_error_names():

with pytest.raises(TypeError) as e:
ICRS(ra=150 * u.deg)
assert "missing 1 required positional argument: 'dec'" in str(e)
assert "missing 1 required positional argument: 'dec'" in str(e.value)

with pytest.raises(TypeError) as e:
ICRS(ra=150*u.deg, dec=-11*u.deg,
pm_ra=100*u.mas/u.yr, pm_dec=10*u.mas/u.yr)
assert "pm_ra_cosdec" in str(e)
assert "pm_ra_cosdec" in str(e.value)


def test_non_spherical_representation_unit_creation(unitphysics):
Expand Down
2 changes: 1 addition & 1 deletion astropy/coordinates/tests/test_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ def test_init_xyz_but_more_than_one_array_fail(self):
with pytest.raises(ValueError) as exc:
CartesianRepresentation(x=[1, 2, 3] * u.pc, y=[2, 3, 4] * u.pc,
z=[3, 4, 5] * u.pc, xyz_axis=0)
assert 'xyz_axis should only be set' in str(exc)
assert 'xyz_axis should only be set' in str(exc.value)

def test_init_one_array_yz_fail(self):
with pytest.raises(ValueError) as exc:
Expand Down
24 changes: 12 additions & 12 deletions astropy/coordinates/tests/test_sky_coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_coord_init_string():

with pytest.raises(ValueError) as err:
SkyCoord('1d 2d 3d')
assert "Cannot parse first argument data" in str(err)
assert "Cannot parse first argument data" in str(err.value)

sc1 = SkyCoord('8 00 00 +5 00 00.0', unit=(u.hour, u.deg), frame='icrs')
assert isinstance(sc1, SkyCoord)
Expand Down Expand Up @@ -232,7 +232,7 @@ def test_coord_init_unit():
for unit in ('deg,deg,deg,deg', [u.deg, u.deg, u.deg, u.deg], None):
with pytest.raises(ValueError) as err:
SkyCoord(1, 2, unit=unit)
assert 'Unit keyword must have one to three unit values' in str(err)
assert 'Unit keyword must have one to three unit values' in str(err.value)

for unit in ('m', (u.m, u.deg), ''):
with pytest.raises(u.UnitsError) as err:
Expand All @@ -253,19 +253,19 @@ def test_coord_init_list():

with pytest.raises(ValueError) as err:
SkyCoord(['1d 2d 3d'])
assert "Cannot parse first argument data" in str(err)
assert "Cannot parse first argument data" in str(err.value)

with pytest.raises(ValueError) as err:
SkyCoord([('1d', '2d', '3d')])
assert "Cannot parse first argument data" in str(err)
assert "Cannot parse first argument data" in str(err.value)

sc = SkyCoord([1 * u.deg, 1 * u.deg], [2 * u.deg, 2 * u.deg])
assert allclose(sc.ra, Angle('1d'))
assert allclose(sc.dec, Angle('2d'))

with pytest.raises(ValueError) as err:
SkyCoord([1 * u.deg, 2 * u.deg]) # this list is taken as RA w/ missing dec
assert "One or more elements of input sequence does not have a length" in str(err)
assert "One or more elements of input sequence does not have a length" in str(err.value)


def test_coord_init_array():
Expand Down Expand Up @@ -296,7 +296,7 @@ def test_coord_init_representation():

with pytest.raises(ValueError) as err:
SkyCoord(coord, frame='icrs', ra='1d')
assert "conflicts with keyword argument 'ra'" in str(err)
assert "conflicts with keyword argument 'ra'" in str(err.value)

coord = CartesianRepresentation(1 * u.one, 2 * u.one, 3 * u.one)
sc = SkyCoord(coord, frame='icrs')
Expand Down Expand Up @@ -328,7 +328,7 @@ def test_frame_init():

with pytest.raises(ValueError) as err:
SkyCoord(C_ICRS, frame='galactic')
assert 'Cannot override frame=' in str(err)
assert 'Cannot override frame=' in str(err.value)


def test_attr_inheritance():
Expand Down Expand Up @@ -383,7 +383,7 @@ def test_attr_conflicts():
# Not OK if attrs don't match
with pytest.raises(ValueError) as err:
SkyCoord(sc, equinox='J1999', obstime='J2002')
assert "Coordinate attribute 'obstime'=" in str(err)
assert "Coordinate attribute 'obstime'=" in str(err.value)

# Same game but with fk4 which has equinox and obstime frame attrs
sc = SkyCoord(1, 2, frame='fk4', unit='deg', equinox='J1999', obstime='J2001')
Expand All @@ -394,12 +394,12 @@ def test_attr_conflicts():
# Not OK if SkyCoord attrs don't match
with pytest.raises(ValueError) as err:
SkyCoord(sc, equinox='J1999', obstime='J2002')
assert "Frame attribute 'obstime' has conflicting" in str(err)
assert "Frame attribute 'obstime' has conflicting" in str(err.value)

# Not OK because sc.frame has different attrs
with pytest.raises(ValueError) as err:
SkyCoord(sc.frame, equinox='J1999', obstime='J2002')
assert "Frame attribute 'obstime' has conflicting" in str(err)
assert "Frame attribute 'obstime' has conflicting" in str(err.value)


def test_frame_attr_getattr():
Expand Down Expand Up @@ -877,7 +877,7 @@ def test_units():

with pytest.raises(u.UnitsError) as err:
SkyCoord(1, 2, 3, unit=(u.m, u.m), representation_type='cartesian')
assert 'should have matching physical types' in str(err)
assert 'should have matching physical types' in str(err.value)

SkyCoord(1, 2, 3, unit=(u.m, u.km, u.pc), representation_type='cartesian')
assert_quantities_allclose(sc, (1*u.m, 2*u.km, 3*u.pc), ('x', 'y', 'z'))
Expand Down Expand Up @@ -1053,7 +1053,7 @@ def test_init_with_frame_instance_keyword():
# Check duplicate arguments
with pytest.raises(ValueError) as err:
c = SkyCoord(3 * u.deg, 4 * u.deg, frame=FK5(equinox='J2010'), equinox='J2001')
assert "Cannot specify frame attribute 'equinox'" in str(err)
assert "Cannot specify frame attribute 'equinox'" in str(err.value)


def test_guess_from_table():
Expand Down
24 changes: 12 additions & 12 deletions astropy/io/ascii/tests/test_c_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ def test_too_many_cols1():
"""
with pytest.raises(InconsistentTableError) as e:
table = FastBasic().read(text)
assert 'InconsistentTableError: Number of header columns (3) ' \
'inconsistent with data columns in data line 2' in str(e)
assert 'Number of header columns (3) ' \
'inconsistent with data columns in data line 2' in str(e.value)


def test_too_many_cols2():
Expand All @@ -423,8 +423,8 @@ def test_too_many_cols2():
"""
with pytest.raises(InconsistentTableError) as e:
table = FastCsv().read(text)
assert 'InconsistentTableError: Number of header columns (2) ' \
'inconsistent with data columns in data line 0' in str(e)
assert 'Number of header columns (2) ' \
'inconsistent with data columns in data line 0' in str(e.value)


def test_too_many_cols3():
Expand All @@ -435,8 +435,8 @@ def test_too_many_cols3():
"""
with pytest.raises(InconsistentTableError) as e:
table = FastCsv().read(text)
assert 'InconsistentTableError: Number of header columns (2) ' \
'inconsistent with data columns in data line 0' in str(e)
assert 'Number of header columns (2) ' \
'inconsistent with data columns in data line 0' in str(e.value)


@pytest.mark.parametrize("parallel", [True, False])
Expand Down Expand Up @@ -750,17 +750,17 @@ def test_rdb(parallel, read_rdb):
with pytest.raises(ValueError) as e:
text = 'A\tB\tC\nN\tS\tN\n4\tb\ta' # C column contains non-numeric data
read_rdb(text, parallel=parallel)
assert 'Column C failed to convert' in str(e)
assert 'Column C failed to convert' in str(e.value)

with pytest.raises(ValueError) as e:
text = 'A\tB\tC\nN\tN\n1\t2\t3' # not enough types specified
read_rdb(text, parallel=parallel)
assert 'mismatch between number of column names and column types' in str(e)
assert 'mismatch between number of column names and column types' in str(e.value)

with pytest.raises(ValueError) as e:
text = 'A\tB\tC\nN\tN\t5\n1\t2\t3' # invalid type for column C
read_rdb(text, parallel=parallel)
assert 'type definitions do not all match [num](N|S)' in str(e)
assert 'type definitions do not all match [num](N|S)' in str(e.value)


@pytest.mark.parametrize("parallel", [True, False])
Expand Down Expand Up @@ -794,7 +794,7 @@ def test_data_start(parallel, read_basic):
# tries to begin in the middle of quoted field
read_basic(text, data_start=4, parallel=parallel)
assert 'header columns (3) inconsistent with data columns in data line 0' \
in str(e)
in str(e.value)

table = read_basic(text, data_start=5, parallel=parallel)
# ignore commented line
Expand Down Expand Up @@ -864,7 +864,7 @@ def test_strip_line_trailing_whitespace(parallel, read_basic):
with pytest.raises(InconsistentTableError) as e:
ascii.read(StringIO(text), format='fast_basic', guess=False)
assert 'header columns (3) inconsistent with data columns in data line 0' \
in str(e)
in str(e.value)

text = 'a b c\n 1 2 3 \t \n 4 5 6 '
table = read_basic(text, parallel=parallel)
Expand Down Expand Up @@ -1148,7 +1148,7 @@ def test_fortran_reader(parallel, guess):
ascii.read(text.format(*(6*('D'))), format='basic', guess=guess,
fast_reader={'use_fast_converter': False,
'parallel': parallel, 'exponent_style': 'D'})
assert 'fast_reader: exponent_style requires use_fast_converter' in str(e)
assert 'fast_reader: exponent_style requires use_fast_converter' in str(e.value)

# Enable multiprocessing and the fast converter iterate over
# all style-exponent combinations, with auto-detection
Expand Down
4 changes: 2 additions & 2 deletions astropy/io/ascii/tests/test_ecsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def test_csv_ecsv_colnames_mismatch():
lines[header_index] = 'a b d'
with pytest.raises(ValueError) as err:
ascii.read(lines, format='ecsv')
assert "column names from ECSV header ['a', 'b', 'c']" in str(err)
assert "column names from ECSV header ['a', 'b', 'c']" in str(err.value)


@pytest.mark.skipif('not HAS_YAML')
Expand Down Expand Up @@ -435,7 +435,7 @@ def test_ecsv_but_no_yaml_warning():

with pytest.raises(ascii.InconsistentTableError) as exc:
ascii.read(SIMPLE_LINES, format='ecsv')
assert "PyYAML package is required" in str(exc)
assert "PyYAML package is required" in str(exc.value)


@pytest.mark.skipif('not HAS_YAML')
Expand Down
4 changes: 2 additions & 2 deletions astropy/io/ascii/tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ def test_identify_table_fail():
with pytest.raises(core.InconsistentTableError) as err:
Table.read(table_in, format='ascii.html', htmldict={'table_id': 'bad_id'},
guess=False)
assert str(err).endswith("ERROR: HTML table id 'bad_id' not found")
assert err.match("ERROR: HTML table id 'bad_id' not found$")

with pytest.raises(core.InconsistentTableError) as err:
Table.read(table_in, format='ascii.html', htmldict={'table_id': 3},
guess=False)
assert str(err).endswith("ERROR: HTML table number 3 not found")
assert err.match("ERROR: HTML table number 3 not found$")


@pytest.mark.skipif('not HAS_BEAUTIFUL_SOUP')
Expand Down
2 changes: 1 addition & 1 deletion astropy/io/ascii/tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ def test_read_chunks_chunk_size_too_small():
with pytest.raises(ValueError) as err:
ascii.read(fpath, header_start=1, data_start=3,
fast_reader={'chunk_size': 10})
assert 'no newline found in chunk (chunk_size too small?)' in str(err)
assert 'no newline found in chunk (chunk_size too small?)' in str(err.value)


def test_read_chunks_table_changes():
Expand Down

0 comments on commit 6bbd284

Please sign in to comment.