Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ API changes
Service fixes and enhancements
------------------------------

mast
^^^^

- Raise an error if non-string values are passed to ``utils.resolve_object``. [#3435]



Infrastructure, Utility and Other Changes and Additions
Expand Down
20 changes: 12 additions & 8 deletions astroquery/mast/tests/test_mast.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,10 @@ def test_resolve_object_single(patch_post):
with pytest.raises(ResolverError, match='Could not resolve "nonexisting" to a sky position.'):
mast.Mast.resolve_object("nonexisting")

# Error if object is not a string
with pytest.raises(InvalidQueryError, match='All object names must be strings.'):
mast.Mast.resolve_object(1)

# Error if single object cannot be resolved with given resolver
with pytest.raises(ResolverError, match='Could not resolve "Barnard\'s Star" to a sky position using '
'resolver "NED".'):
Expand All @@ -622,21 +626,13 @@ def test_resolve_object_multi(patch_post):
assert obj in coord_dict
assert isinstance(coord_dict[obj], SkyCoord)

# Warn if one of the objects cannot be resolved
with pytest.warns(InputWarning, match='Could not resolve "nonexisting" to a sky position.'):
coord_dict = mast.Mast.resolve_object(["M1", "nonexisting"])

# Resolver specified
coord_dict = mast.Mast.resolve_object(objects, resolver="SIMBAD")
assert isinstance(coord_dict, dict)
for obj in objects:
assert obj in coord_dict
assert isinstance(coord_dict[obj], SkyCoord)

# Warn if one of the objects can't be resolved with given resolver
with pytest.warns(InputWarning, match='Could not resolve "TIC 307210830" to a sky position using resolver "NED"'):
mast.Mast.resolve_object(objects[:2], resolver="NED")

# Resolve all
coord_dict = mast.Mast.resolve_object(objects, resolve_all=True)
assert isinstance(coord_dict, dict)
Expand All @@ -646,6 +642,14 @@ def test_resolve_object_multi(patch_post):
assert isinstance(obj_dict, dict)
assert isinstance(obj_dict["SIMBAD"], SkyCoord)

# Warn if one of the objects cannot be resolved
with pytest.warns(InputWarning, match='Could not resolve "nonexisting" to a sky position.'):
coord_dict = mast.Mast.resolve_object(["M1", "nonexisting"])

# Warn if one of the objects can't be resolved with given resolver
with pytest.warns(InputWarning, match='Could not resolve "TIC 307210830" to a sky position using resolver "NED"'):
mast.Mast.resolve_object(objects[:2], resolver="NED")

# Error if none of the objects can be resolved
warnings.simplefilter("ignore", category=InputWarning) # ignore warnings
with pytest.raises(ResolverError, match='Could not resolve any of the given object names to sky positions.'):
Expand Down
15 changes: 13 additions & 2 deletions astroquery/mast/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def resolve_object(objectname, *, resolver=None, resolve_all=False):

Parameters
----------
objectname : str
objectname : str, or iterable of str
Name(s) of astronomical object(s) to resolve.
resolver : str, optional
The resolver to use when resolving a named target into coordinates. Valid options are "SIMBAD" and "NED".
Expand All @@ -179,7 +179,18 @@ def resolve_object(objectname, *, resolver=None, resolve_all=False):
`~astropy.coordinates.SkyCoord` objects with the resolved coordinates.
"""
# Normalize input
object_names = [objectname] if isinstance(objectname, str) else list(objectname)
try:
# Strings are iterable, so check explicitly
if isinstance(objectname, str):
raise TypeError
object_names = list(objectname)
except TypeError:
object_names = [objectname]

# If any items are not strings, raise an error
if not all(isinstance(name, str) for name in object_names):
raise InvalidQueryError('All object names must be strings.')

single = len(object_names) == 1

is_catalog = False # Flag to check if object name belongs to a MAST catalog
Expand Down
Loading