Skip to content

Commit 7f99790

Browse files
snbiancobsipocz
authored andcommitted
Raise error if non-string object names
Remove extra changes from tests Fix docstring
1 parent 65f35d8 commit 7f99790

File tree

6 files changed

+53
-46
lines changed

6 files changed

+53
-46
lines changed

CHANGES.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ esa.hubble
2323
mast
2424
^^^^
2525

26-
- Bugfix so that the ``utils.resolve_object`` function accepts integer values as object names. [#3435]
26+
- Raise an error if non-string values are passed to ``utils.resolve_object``. [#3435]
2727

2828

2929

astroquery/mast/tests/data/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ To generate `~astroquery.mast.tests.data.resolver.json`, use the following:
5757
>>> import json
5858
>>> from astroquery.mast import utils
5959
...
60-
>>> objects = ["TIC 307210830", "Barnard's Star", "M1", "M101", "M103", "M8", "M10", 251813740]
60+
>>> objects = ["TIC 307210830", "Barnard's Star", "M1", "M101", "M103", "M8", "M10"]
6161
>>> resp = utils._simple_request('http://mastresolver.stsci.edu/Santa-war/query',
6262
... {'name': objects, 'outputFormat': 'json', 'resolveAll': 'true'})
6363
>>> with open('resolver.json', 'w') as file:

astroquery/mast/tests/data/resolver.json

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"searchString": "tic 307210830",
55
"resolver": "TIC",
66
"cached": false,
7-
"resolverTime": 6,
7+
"resolverTime": 2,
88
"searchRadius": 0.000333,
99
"canonicalName": "TIC 307210830",
1010
"ra": 124.531756290083,
@@ -14,19 +14,31 @@
1414
"searchString": "tic 307210830",
1515
"resolver": "SIMBAD",
1616
"cached": true,
17-
"resolverTime": 282,
17+
"resolverTime": 294,
1818
"cacheDate": "Apr 17, 2025, 3:47:59 PM",
1919
"searchRadius": -1.0,
2020
"canonicalName": "L 98-59",
2121
"ra": 124.5317560026638,
2222
"decl": -68.3130014904408,
2323
"objectType": "HighPM*"
2424
},
25+
{
26+
"searchString": "tic 307210830",
27+
"resolver": "SIMBADCFA",
28+
"cached": true,
29+
"resolverTime": 296,
30+
"cacheDate": "Mar 19, 2025, 4:36:27 PM",
31+
"searchRadius": -1.0,
32+
"canonicalName": "L 98-59",
33+
"ra": 124.5317560026638,
34+
"decl": -68.3130014904408,
35+
"objectType": "HighPM*"
36+
},
2537
{
2638
"searchString": "barnard's star",
2739
"resolver": "SIMBAD",
2840
"cached": true,
29-
"resolverTime": 284,
41+
"resolverTime": 302,
3042
"cacheDate": "May 7, 2025, 2:29:06 PM",
3143
"searchRadius": -1.0,
3244
"canonicalName": "NAME Barnard's star",
@@ -38,7 +50,7 @@
3850
"searchString": "m1",
3951
"resolver": "SIMBAD",
4052
"cached": true,
41-
"resolverTime": 332,
53+
"resolverTime": 292,
4254
"cacheDate": "Apr 29, 2025, 1:57:27 PM",
4355
"searchRadius": -1.0,
4456
"canonicalName": "M 1",
@@ -53,7 +65,7 @@
5365
"searchString": "m1",
5466
"resolver": "NED",
5567
"cached": false,
56-
"resolverTime": 915,
68+
"resolverTime": 1417,
5769
"searchRadius": -1.0,
5870
"canonicalName": "MESSIER 001",
5971
"ra": 83.63311,
@@ -64,8 +76,8 @@
6476
"searchString": "m101",
6577
"resolver": "SIMBAD",
6678
"cached": true,
67-
"resolverTime": 376,
68-
"cacheDate": "Sep 30, 2025, 8:16:12 PM",
79+
"resolverTime": 290,
80+
"cacheDate": "Sep 2, 2025, 8:13:51 PM",
6981
"searchRadius": -1.0,
7082
"canonicalName": "M 101",
7183
"ra": 210.802429,
@@ -79,7 +91,7 @@
7991
"searchString": "m101",
8092
"resolver": "SIMBADCFA",
8193
"cached": true,
82-
"resolverTime": 340,
94+
"resolverTime": 289,
8395
"cacheDate": "May 14, 2025, 8:04:05 PM",
8496
"searchRadius": -1.0,
8597
"canonicalName": "M 101",
@@ -94,7 +106,7 @@
94106
"searchString": "m101",
95107
"resolver": "NED",
96108
"cached": false,
97-
"resolverTime": 917,
109+
"resolverTime": 1181,
98110
"searchRadius": -1.0,
99111
"canonicalName": "MESSIER 101",
100112
"ra": 210.80227,
@@ -106,7 +118,7 @@
106118
"searchString": "m103",
107119
"resolver": "SIMBAD",
108120
"cached": true,
109-
"resolverTime": 286,
121+
"resolverTime": 294,
110122
"cacheDate": "May 2, 2025, 4:33:25 AM",
111123
"searchRadius": -1.0,
112124
"canonicalName": "M 103",
@@ -122,7 +134,7 @@
122134
"searchString": "m103",
123135
"resolver": "NED",
124136
"cached": false,
125-
"resolverTime": 821,
137+
"resolverTime": 1094,
126138
"searchRadius": -1.0,
127139
"canonicalName": "MESSIER 103",
128140
"ra": 23.34086,
@@ -133,7 +145,7 @@
133145
"searchString": "m8",
134146
"resolver": "SIMBAD",
135147
"cached": true,
136-
"resolverTime": 313,
148+
"resolverTime": 294,
137149
"cacheDate": "May 1, 2025, 1:11:47 PM",
138150
"searchRadius": -1.0,
139151
"canonicalName": "M 8",
@@ -145,7 +157,7 @@
145157
"searchString": "m8",
146158
"resolver": "NED",
147159
"cached": false,
148-
"resolverTime": 826,
160+
"resolverTime": 1165,
149161
"searchRadius": -1.0,
150162
"canonicalName": "MESSIER 008",
151163
"ra": 270.92194,
@@ -156,7 +168,7 @@
156168
"searchString": "m10",
157169
"resolver": "SIMBAD",
158170
"cached": true,
159-
"resolverTime": 278,
171+
"resolverTime": 290,
160172
"cacheDate": "Apr 29, 2025, 2:00:16 PM",
161173
"searchRadius": -1.0,
162174
"canonicalName": "M 10",
@@ -172,22 +184,12 @@
172184
"searchString": "m10",
173185
"resolver": "NED",
174186
"cached": false,
175-
"resolverTime": 838,
187+
"resolverTime": 998,
176188
"searchRadius": -1.0,
177189
"canonicalName": "MESSIER 010",
178190
"ra": 254.28771,
179191
"decl": -4.10031,
180192
"objectType": "*Cl"
181-
},
182-
{
183-
"searchString": "251813740",
184-
"resolver": "K2",
185-
"cached": false,
186-
"resolverTime": 3,
187-
"searchRadius": 0.000333,
188-
"canonicalName": "EPIC 251813740",
189-
"ra": 343.48308,
190-
"decl": -0.98468872
191193
}
192194
],
193195
"status": ""

astroquery/mast/tests/test_mast.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,6 @@ def test_resolve_object_single(patch_post):
577577
assert isinstance(obj_loc, SkyCoord)
578578
assert round(obj_loc.separation(tic_coord).value, 10) == 0
579579

580-
# Resolve with integer input
581-
obj_loc = mast.Mast.resolve_object(251813740)
582-
assert isinstance(obj_loc, SkyCoord)
583-
584580
# Resolve using a specific resolver and an object that belongs to a MAST catalog
585581
obj_loc_simbad = mast.Mast.resolve_object(obj, resolver="SIMBAD")
586582
assert round(obj_loc_simbad.separation(simbad_coord).value, 10) == 0
@@ -605,6 +601,10 @@ def test_resolve_object_single(patch_post):
605601
with pytest.raises(ResolverError, match='Could not resolve "nonexisting" to a sky position.'):
606602
mast.Mast.resolve_object("nonexisting")
607603

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+
608608
# Error if single object cannot be resolved with given resolver
609609
with pytest.raises(ResolverError, match='Could not resolve "Barnard\'s Star" to a sky position using '
610610
'resolver "NED".'):
@@ -617,27 +617,26 @@ def test_resolve_object_single(patch_post):
617617

618618

619619
def test_resolve_object_multi(patch_post):
620-
objects = ["TIC 307210830", "M1", "Barnard's Star", 251813740]
620+
objects = ["TIC 307210830", "M1", "Barnard's Star"]
621621

622622
# No resolver specified
623623
coord_dict = mast.Mast.resolve_object(objects)
624624
assert isinstance(coord_dict, dict)
625625
for obj in objects:
626-
obj_str = str(obj)
627-
assert obj_str in coord_dict
628-
assert isinstance(coord_dict[obj_str], SkyCoord)
626+
assert obj in coord_dict
627+
assert isinstance(coord_dict[obj], SkyCoord)
629628

630629
# Resolver specified
631-
coord_dict = mast.Mast.resolve_object(objects[:3], resolver="SIMBAD")
630+
coord_dict = mast.Mast.resolve_object(objects, resolver="SIMBAD")
632631
assert isinstance(coord_dict, dict)
633-
for obj in objects[:3]:
632+
for obj in objects:
634633
assert obj in coord_dict
635634
assert isinstance(coord_dict[obj], SkyCoord)
636635

637636
# Resolve all
638-
coord_dict = mast.Mast.resolve_object(objects[:3], resolve_all=True)
637+
coord_dict = mast.Mast.resolve_object(objects, resolve_all=True)
639638
assert isinstance(coord_dict, dict)
640-
for obj in objects[:3]:
639+
for obj in objects:
641640
assert obj in coord_dict
642641
obj_dict = coord_dict[obj]
643642
assert isinstance(obj_dict, dict)

astroquery/mast/tests/test_mast_remote.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ def test_resolve_object(self):
5252
ticobj_loc = utils.resolve_object("TIC 141914082")
5353
assert round(ticobj_loc.separation(SkyCoord("94.6175354 -72.04484622", unit='deg')).value, 4) == 0
5454

55-
# Integer input
56-
int_loc = utils.resolve_object(251813740)
57-
assert round(int_loc.separation(SkyCoord("343.48308 -0.98468872", unit='deg')).value, 4) == 0
58-
5955
# Try the same object with different resolvers
6056
# The position of objects can change with different resolvers
6157
ned_loc = utils.resolve_object("jw100", resolver="NED")

astroquery/mast/utils.py

Lines changed: 13 additions & 3 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, int, or iterable of {str, int}
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,8 +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, int)) else list(objectname)
183-
object_names = [str(name) for name in object_names] # Convert all names to strings
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+
184194
single = len(object_names) == 1
185195

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

0 commit comments

Comments
 (0)