diff --git a/CHANGES.rst b/CHANGES.rst index 1879f11851..85b096e581 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/astroquery/mast/tests/test_mast.py b/astroquery/mast/tests/test_mast.py index 8c1f07147f..e57ada5e28 100644 --- a/astroquery/mast/tests/test_mast.py +++ b/astroquery/mast/tests/test_mast.py @@ -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".'): @@ -622,10 +626,6 @@ 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) @@ -633,10 +633,6 @@ def test_resolve_object_multi(patch_post): 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) @@ -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.'): diff --git a/astroquery/mast/utils.py b/astroquery/mast/utils.py index fa3ff17f71..5a18adce46 100644 --- a/astroquery/mast/utils.py +++ b/astroquery/mast/utils.py @@ -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". @@ -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