Skip to content

Commit

Permalink
MAINT: cleanup astropy <4.1 code (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsipocz authored Sep 21, 2022
1 parent 8f535b4 commit acced64
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 45 deletions.
12 changes: 2 additions & 10 deletions pyvo/dal/adhoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from astropy import units as u
from astropy.units import Quantity, Unit
from astropy.units import spectral as spectral_equivalencies
from pyvo.utils.compat import ASTROPY_LT_4_1

from astropy.io.votable.tree import Resource, Group
from astropy.utils.collections import HomogeneousList
Expand Down Expand Up @@ -132,9 +131,7 @@ def get_adhocservice_by_ivoid(self, ivo_id):
Resource
The resource element describing the service.
"""
if ASTROPY_LT_4_1 and isinstance(ivo_id, str):
ivo_id = ivo_id.encode('utf-8')
if not ASTROPY_LT_4_1 and isinstance(ivo_id, bytes):
if isinstance(ivo_id, bytes):
ivo_id = ivo_id.decode('utf-8')
for adhocservice in self.iter_adhocservices():
if any(
Expand Down Expand Up @@ -592,12 +589,7 @@ def clone_byid(self, id):
for index, row in enumerate(rows):
votable.array[index] = row
# now remove unreferenced services from resources
if ASTROPY_LT_4_1:
referenced_serviced = \
[x.decode('utf-8') for x in votable.array['service_def'] if x]
else:
referenced_serviced = \
[x for x in votable.array['service_def'] if x]
referenced_serviced = [x for x in votable.array['service_def'] if x]
# remove customized that are not referenced by the current results
for x in copy_tb.resources:
if x.ID and x.ID not in referenced_serviced:
Expand Down
25 changes: 7 additions & 18 deletions pyvo/dal/tests/test_datalink.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,6 @@ def test_datalink_batch():
assert len([_ for _ in results.iter_datalinks()]) == 3


# temporary hack while astropy <4.1 is still around; you can remove
# this and its calls once that's no longer true.
def _debytify(v):
if isinstance(v, bytes):
return v.decode("utf-8")
return v


@pytest.mark.usefixtures('proc', 'datalink_vocabulary')
@pytest.mark.filterwarnings("ignore::astropy.io.votable.exceptions.W27")
@pytest.mark.filterwarnings("ignore::astropy.io.votable.exceptions.W06")
Expand All @@ -151,22 +143,21 @@ def _debytify(v):
class TestSemanticsRetrieval:
def test_access_with_string(self):
datalink = DatalinkResults.from_result_url('http://example.com/proc')
res = [_debytify(r["access_url"])
for r in datalink.bysemantics("#this")]
res = [r["access_url"] for r in datalink.bysemantics("#this")]
assert len(res) == 1
assert res[0].endswith("eq010000ms/20100927.comb_avg.0001.fits.fz")

def test_access_with_list(self):
datalink = DatalinkResults.from_result_url('http://example.com/proc')
res = [_debytify(r["access_url"])
res = [r["access_url"]
for r in datalink.bysemantics(["#this", "#preview-image"])]
assert len(res) == 2
assert res[0].endswith("eq010000ms/20100927.comb_avg.0001.fits.fz")
assert res[1].endswith("20100927.comb_avg.0001.fits.fz?preview=True")

def test_access_with_expansion(self):
datalink = DatalinkResults.from_result_url('http://example.com/proc')
res = [_debytify(r["access_url"])
res = [r["access_url"]
for r in datalink.bysemantics(["#this", "#preview"])]
assert len(res) == 3
assert res[0].endswith("eq010000ms/20100927.comb_avg.0001.fits.fz")
Expand All @@ -175,24 +166,22 @@ def test_access_with_expansion(self):

def test_access_without_expansion(self):
datalink = DatalinkResults.from_result_url('http://example.com/proc')
res = [_debytify(r["access_url"])
for r in datalink.bysemantics(
["#this", "#preview"],
include_narrower=False)]
res = [r["access_url"] for r in datalink.bysemantics(
["#this", "#preview"], include_narrower=False)]
assert len(res) == 2
assert res[0].endswith("eq010000ms/20100927.comb_avg.0001.fits.fz")
assert res[1].endswith("http://dc.zah.uni-heidelberg.de/wider.dat")

def test_with_full_url(self):
datalink = DatalinkResults.from_result_url('http://example.com/proc')
res = [_debytify(r["access_url"])
res = [r["access_url"]
for r in datalink.bysemantics("urn:example:rdf/dlext#oracle")]
assert len(res) == 1
assert res[0].endswith("when-will-it-be-back")

def test_all_mixed(self):
datalink = DatalinkResults.from_result_url('http://example.com/proc')
res = [_debytify(r["access_url"])
res = [r["access_url"]
for r in datalink.bysemantics([
"urn:example:rdf/dlext#oracle",
'http://www.ivoa.net/rdf/datalink/core#preview',
Expand Down
21 changes: 9 additions & 12 deletions pyvo/dal/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from pyvo.dal.exceptions import DALServiceError, DALQueryError,\
DALFormatError, DALOverflowWarning
from pyvo.version import version
from pyvo.utils.compat import ASTROPY_LT_4_1

from astropy.table import Table
from astropy.io.votable.tree import VOTableFile, Table as VOTable
Expand Down Expand Up @@ -131,12 +130,11 @@ def _test_results(results):
assert results['1', 1] == 42
assert results['1', 2] == 1337

truth = b'Illuminatus' if ASTROPY_LT_4_1 else 'Illuminatus'
truth = 'Illuminatus'
assert results['2', 0] == truth
truth = b"Don't panic, and always carry a towel" \
if ASTROPY_LT_4_1 else "Don't panic, and always carry a towel"
truth = "Don't panic, and always carry a towel"
assert results['2', 1] == truth
truth = b'Elite' if ASTROPY_LT_4_1 else 'Elite'
truth = 'Elite'
assert results['2', 2] == truth


Expand All @@ -148,16 +146,15 @@ def _test_records(records):

assert records[0]['1'] == 23

truth = b'Illuminatus' if ASTROPY_LT_4_1 else 'Illuminatus'
truth = 'Illuminatus'
assert records[0]['2'] == truth

assert records[1]['1'] == 42
truth = b"Don't panic, and always carry a towel" \
if ASTROPY_LT_4_1 else "Don't panic, and always carry a towel"
truth = "Don't panic, and always carry a towel"
assert records[1]['2'] == truth

assert records[2]['1'] == 1337
truth = b'Elite' if ASTROPY_LT_4_1 else 'Elite'
truth = 'Elite'
assert records[2]['2'] == truth


Expand Down Expand Up @@ -407,7 +404,7 @@ def test_itemaccess(self):
'http://example.com/query/basic')[0]

assert record['1'] == 23
truth = b'Illuminatus' if ASTROPY_LT_4_1 else 'Illuminatus'
truth = 'Illuminatus'
assert record['2'] == truth

assert record['_1'] == 23
Expand Down Expand Up @@ -438,7 +435,7 @@ def test_len(self):
def test_repr(self):
record = DALResults.from_result_url(
'http://example.com/query/basic')[0]
truth = b'Illuminatus' if ASTROPY_LT_4_1 else 'Illuminatus'
truth = 'Illuminatus'
assert repr(record) == repr((23, truth))

def test_get(self):
Expand All @@ -454,7 +451,7 @@ def test_columnaliases(self):
assert record.getbyucd('foo') == 23
assert record.getbyucd('bar') == 23

truth = b'Illuminatus' if ASTROPY_LT_4_1 else 'Illuminatus'
truth = 'Illuminatus'
assert record.getbyutype('foobar') == truth

record.getbyucd('baz') is None
Expand Down
6 changes: 1 addition & 5 deletions pyvo/utils/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@
"""
Placeholder for compatibility constructs
"""
from packaging.version import Version
import astropy

__all__ = ['ASTROPY_LT_4_1']

ASTROPY_LT_4_1 = Version(astropy.__version__) < Version('4.1')
__all__ = []

0 comments on commit acced64

Please sign in to comment.