Skip to content

Commit 9aa767b

Browse files
authored
Merge pull request #2475 from at88mph/err-unused-args
BUG: raise for unsupported arguments for alma
2 parents 902c758 + 59ff73e commit 9aa767b

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ alma
2424
^^^^
2525

2626
- Fixed a regression to handle arrays of string input for the ``query`` methods. [#2094]
27+
- Throws an error when an unsupported ``kwargs`` (or argument) is passed in to a function. [#2475]
2728

2829

2930
astrometry.net

astroquery/alma/core.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
def _gen_sql(payload):
163163
sql = 'select * from ivoa.obscore'
164164
where = ''
165+
unused_payload = payload.copy()
165166
if payload:
166167
for constraint in payload:
167168
for attrib_category in ALMA_FORM_KEYS.values():
@@ -181,6 +182,18 @@ def _gen_sql(payload):
181182
else:
182183
where = ' WHERE '
183184
where += attrib_where
185+
186+
# Delete this key to see what's left over afterward
187+
#
188+
# Use pop to avoid the slight possibility of trying to remove
189+
# an already removed key
190+
unused_payload.pop(constraint)
191+
192+
if unused_payload:
193+
# Left over (unused) constraints passed. Let the user know.
194+
remaining = [f'{p} -> {unused_payload[p]}' for p in unused_payload]
195+
raise TypeError(f'Unsupported arguments were passed:\n{remaining}')
196+
184197
return sql + where
185198

186199

@@ -296,7 +309,8 @@ def query_region_async(self, coordinate, radius, *, public=True,
296309
payload=payload, **kwargs)
297310

298311
def query_async(self, payload, *, public=True, science=True,
299-
legacy_columns=False, get_query_payload=None, **kwargs):
312+
legacy_columns=False, get_query_payload=None,
313+
maxrec=None, **kwargs):
300314
"""
301315
Perform a generic query with user-specified payload
302316
@@ -313,6 +327,10 @@ def query_async(self, payload, *, public=True, science=True,
313327
legacy_columns : bool
314328
True to return the columns from the obsolete ALMA advanced query,
315329
otherwise return the current columns based on ObsCore model.
330+
get_query_payload : bool
331+
Flag to indicate whether to simply return the payload.
332+
maxrec : integer
333+
Cap on the amount of records returned. Default is no limit.
316334
317335
Returns
318336
-------
@@ -340,7 +358,7 @@ def query_async(self, payload, *, public=True, science=True,
340358
return payload
341359

342360
query = _gen_sql(payload)
343-
result = self.query_tap(query, maxrec=payload.get('maxrec', None))
361+
result = self.query_tap(query, maxrec=maxrec)
344362
if result is not None:
345363
result = result.to_table()
346364
else:
@@ -588,7 +606,7 @@ def is_proprietary(self, uid):
588606
proprietary or not.
589607
"""
590608
query = "select distinct data_rights from ivoa.obscore where " \
591-
"obs_id='{}'".format(uid)
609+
"member_ous_uid='{}'".format(uid)
592610
result = self.query_tap(query)
593611
if result:
594612
tableresult = result.to_table()

astroquery/alma/tests/test_alma.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,18 @@ def test_pol_sql():
243243
common_select + " WHERE (pol_states='/XX/' OR pol_states='/XX/YY/')"
244244

245245

246+
def test_unused_args():
247+
alma = Alma()
248+
alma._get_dataarchive_url = Mock()
249+
# with patch('astroquery.alma.tapsql.coord.SkyCoord.from_name') as name_mock, pytest.raises(TypeError) as typeError:
250+
with patch('astroquery.alma.tapsql.coord.SkyCoord.from_name') as name_mock:
251+
with pytest.raises(TypeError) as typeError:
252+
name_mock.return_value = SkyCoord(1, 2, unit='deg')
253+
alma.query_object('M13', public=False, bogus=True, nope=False, band_list=[3])
254+
255+
assert "['bogus -> True', 'nope -> False']" in str(typeError.value)
256+
257+
246258
def test_query():
247259
# Tests the query and return values
248260
tap_mock = Mock()

astroquery/alma/tests/test_alma_remote.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def test_freq(self, alma):
8787

8888
def test_bands(self, alma):
8989
payload = {'band_list': ['5', '7']}
90-
result = alma.query(payload)
90+
# Added maxrec here as downloading and reading the results take too long.
91+
result = alma.query(payload, maxrec=1000)
9192
assert len(result) > 0
9293
for row in result:
9394
assert ('5' in row['band_list']) or ('7' in row['band_list'])
@@ -136,7 +137,7 @@ def test_data_proprietary(self, alma):
136137
assert not alma.is_proprietary('uid://A001/X12a3/Xe9')
137138
IVOA_DATE_FORMAT = "%Y-%m-%dT%H:%M:%S.%f"
138139
now = datetime.utcnow().strftime(IVOA_DATE_FORMAT)[:-3]
139-
query = "select top 1 obs_id from ivoa.obscore where " \
140+
query = "select top 1 member_ous_uid from ivoa.obscore where " \
140141
"obs_release_date > '{}'".format(now)
141142
result = alma.query_tap(query)
142143
assert len(result.table) == 1
@@ -146,6 +147,7 @@ def test_data_proprietary(self, alma):
146147
with pytest.raises(AttributeError):
147148
alma.is_proprietary('uid://NON/EXI/STING')
148149

150+
@pytest.mark.xfail(reason="Depends on PR 2438 (https://github.com/astropy/astroquery/pull/2438)")
149151
def test_data_info(self, temp_dir, alma):
150152
alma.cache_location = temp_dir
151153

@@ -257,6 +259,7 @@ def test_doc_example(self, temp_dir, alma):
257259
gc_data = alma.query_region(galactic_center, 1 * u.deg)
258260
# assert len(gc_data) >= 425 # Feb 8, 2016
259261
assert len(gc_data) >= 50 # Nov 16, 2016
262+
content_length_column_name = 'content_length'
260263

261264
uids = np.unique(m83_data['Member ous id'])
262265
if ASTROPY_LT_4_1:
@@ -271,11 +274,11 @@ def test_doc_example(self, temp_dir, alma):
271274
assert X30.sum() == 4 # Jul 13, 2020
272275
assert X31.sum() == 4 # Jul 13, 2020
273276
mous1 = alma.get_data_info('uid://A001/X11f/X30')
274-
totalsize_mous1 = mous1['size'].sum() * u.Unit(mous1['size'].unit)
277+
totalsize_mous1 = mous1[content_length_column_name].sum() * u.Unit(mous1[content_length_column_name].unit)
275278
assert (totalsize_mous1.to(u.B) > 1.9*u.GB)
276279

277280
mous = alma.get_data_info('uid://A002/X3216af/X31')
278-
totalsize_mous = mous['size'].sum() * u.Unit(mous['size'].unit)
281+
totalsize_mous = mous[content_length_column_name].sum() * u.Unit(mous[content_length_column_name].unit)
279282
# More recent ALMA request responses do not include any information
280283
# about file size, so we have to allow for the possibility that all
281284
# file sizes are replaced with -1
@@ -313,11 +316,13 @@ def test_misc(self, alma):
313316
result = alma.query(payload={'pi_name': '*Bally*'}, public=False,
314317
maxrec=10)
315318
assert result
316-
result.write('/tmp/alma-onerow.txt', format='ascii')
319+
# Add overwrite=True in case the test previously died unexpectedly
320+
# and left the temp file.
321+
result.write('/tmp/alma-onerow.txt', format='ascii', overwrite=True)
317322
for row in result:
318323
assert 'Bally' in row['obs_creator_name']
319324
result = alma.query(payload=dict(project_code='2016.1.00165.S'),
320-
public=False, cache=False)
325+
public=False)
321326
assert result
322327
for row in result:
323328
assert '2016.1.00165.S' == row['proposal_id']
@@ -336,8 +341,7 @@ def test_misc(self, alma):
336341

337342
result = alma.query_region(
338343
coordinates.SkyCoord('5:35:14.461 -5:21:54.41', frame='fk5',
339-
unit=(u.hour, u.deg)), radius=0.034 * u.deg,
340-
payload={'energy.frequency-asu': '215 .. 220'})
344+
unit=(u.hour, u.deg)), radius=0.034 * u.deg)
341345

342346
result = alma.query(payload=dict(project_code='2012.*',
343347
public_data=True))

0 commit comments

Comments
 (0)