Skip to content

Commit aa9c920

Browse files
authored
Merge pull request #3435 from snbianco/resolve-int
2 parents c0a1a84 + 7f99790 commit aa9c920

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ esa.hubble
2020
- Update ``get_datalabs_path`` method so an alternative path is checked if the
2121
file is not in Datalabs yet [#3437]
2222

23+
mast
24+
^^^^
25+
26+
- Raise an error if non-string values are passed to ``utils.resolve_object``. [#3435]
27+
28+
2329

2430
Infrastructure, Utility and Other Changes and Additions
2531
-------------------------------------------------------

astroquery/mast/tests/test_mast.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,10 @@ def test_resolve_object_single(patch_post):
601601
with pytest.raises(ResolverError, match='Could not resolve "nonexisting" to a sky position.'):
602602
mast.Mast.resolve_object("nonexisting")
603603

604+
# Error if object is not a string
605+
with pytest.raises(InvalidQueryError, match='All object names must be strings.'):
606+
mast.Mast.resolve_object(1)
607+
604608
# Error if single object cannot be resolved with given resolver
605609
with pytest.raises(ResolverError, match='Could not resolve "Barnard\'s Star" to a sky position using '
606610
'resolver "NED".'):
@@ -622,21 +626,13 @@ def test_resolve_object_multi(patch_post):
622626
assert obj in coord_dict
623627
assert isinstance(coord_dict[obj], SkyCoord)
624628

625-
# Warn if one of the objects cannot be resolved
626-
with pytest.warns(InputWarning, match='Could not resolve "nonexisting" to a sky position.'):
627-
coord_dict = mast.Mast.resolve_object(["M1", "nonexisting"])
628-
629629
# Resolver specified
630630
coord_dict = mast.Mast.resolve_object(objects, resolver="SIMBAD")
631631
assert isinstance(coord_dict, dict)
632632
for obj in objects:
633633
assert obj in coord_dict
634634
assert isinstance(coord_dict[obj], SkyCoord)
635635

636-
# Warn if one of the objects can't be resolved with given resolver
637-
with pytest.warns(InputWarning, match='Could not resolve "TIC 307210830" to a sky position using resolver "NED"'):
638-
mast.Mast.resolve_object(objects[:2], resolver="NED")
639-
640636
# Resolve all
641637
coord_dict = mast.Mast.resolve_object(objects, resolve_all=True)
642638
assert isinstance(coord_dict, dict)
@@ -646,6 +642,14 @@ def test_resolve_object_multi(patch_post):
646642
assert isinstance(obj_dict, dict)
647643
assert isinstance(obj_dict["SIMBAD"], SkyCoord)
648644

645+
# Warn if one of the objects cannot be resolved
646+
with pytest.warns(InputWarning, match='Could not resolve "nonexisting" to a sky position.'):
647+
coord_dict = mast.Mast.resolve_object(["M1", "nonexisting"])
648+
649+
# Warn if one of the objects can't be resolved with given resolver
650+
with pytest.warns(InputWarning, match='Could not resolve "TIC 307210830" to a sky position using resolver "NED"'):
651+
mast.Mast.resolve_object(objects[:2], resolver="NED")
652+
649653
# Error if none of the objects can be resolved
650654
warnings.simplefilter("ignore", category=InputWarning) # ignore warnings
651655
with pytest.raises(ResolverError, match='Could not resolve any of the given object names to sky positions.'):

astroquery/mast/utils.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def resolve_object(objectname, *, resolver=None, resolve_all=False):
154154
155155
Parameters
156156
----------
157-
objectname : str
157+
objectname : str, or iterable of str
158158
Name(s) of astronomical object(s) to resolve.
159159
resolver : str, optional
160160
The resolver to use when resolving a named target into coordinates. Valid options are "SIMBAD" and "NED".
@@ -179,7 +179,18 @@ def resolve_object(objectname, *, resolver=None, resolve_all=False):
179179
`~astropy.coordinates.SkyCoord` objects with the resolved coordinates.
180180
"""
181181
# Normalize input
182-
object_names = [objectname] if isinstance(objectname, str) else list(objectname)
182+
try:
183+
# Strings are iterable, so check explicitly
184+
if isinstance(objectname, str):
185+
raise TypeError
186+
object_names = list(objectname)
187+
except TypeError:
188+
object_names = [objectname]
189+
190+
# If any items are not strings, raise an error
191+
if not all(isinstance(name, str) for name in object_names):
192+
raise InvalidQueryError('All object names must be strings.')
193+
183194
single = len(object_names) == 1
184195

185196
is_catalog = False # Flag to check if object name belongs to a MAST catalog

0 commit comments

Comments
 (0)