Skip to content

Commit d347559

Browse files
authored
Merge pull request #3084 from snbianco/ASB-28248-invalid-args
Check that parameters are valid when running criteria queries
2 parents 1c1588e + 8b122a2 commit d347559

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ mast
183183
- Increased the speed of ``mast.Observations.get_cloud_uris`` by obtaining multiple
184184
URIs from MAST at once. [#3064]
185185

186+
- Present users with an error rather than a warning when nonexistent query criteria are used in ``mast.Observations.query_criteria``
187+
and ``mast.Catalogs.query_criteria``. [#3084]
188+
186189

187190
0.4.7 (2024-03-08)
188191
==================

astroquery/mast/discovery_portal.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
This module contains various methods for querying the MAST Discovery Portal API.
77
"""
88

9+
import difflib
910
import warnings
1011
import uuid
1112
import json
@@ -20,7 +21,7 @@
2021
from ..query import BaseQuery
2122
from ..utils import async_to_sync
2223
from ..utils.class_or_instance import class_or_instance
23-
from ..exceptions import InputWarning, NoResultsWarning, RemoteServiceError
24+
from ..exceptions import InputWarning, InvalidQueryError, NoResultsWarning, RemoteServiceError
2425

2526
from . import conf, utils
2627

@@ -406,8 +407,10 @@ def build_filter_set(self, column_config_name, service_name=None, **filters):
406407
# Get the column type and separator
407408
col_info = caom_col_config.get(colname)
408409
if not col_info:
409-
warnings.warn("Filter {} does not exist. This filter will be skipped.".format(colname), InputWarning)
410-
continue
410+
closest_match = difflib.get_close_matches(colname, caom_col_config.keys(), n=1)
411+
error_msg = f"Filter '{colname}' does not exist. Did you mean '{closest_match[0]}'?" if closest_match \
412+
else f"Filter '{colname}' does not exist."
413+
raise InvalidQueryError(error_msg)
411414

412415
colType = "discrete"
413416
if (col_info.get("vot.datatype", col_info.get("type")) in ("double", "float", "numeric")) \

astroquery/mast/tests/test_mast_remote.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,17 @@ def test_observations_query_criteria(self):
274274
intentType="calibration")
275275
assert (result["intentType"] == "calibration").all()
276276

277+
def test_observations_query_criteria_invalid_keyword(self):
278+
# attempt to make a criteria query with invalid keyword
279+
with pytest.raises(InvalidQueryError) as err_no_alt:
280+
Observations.query_criteria_count(not_a_keyword='TESS')
281+
assert "Filter 'not_a_keyword' does not exist." in str(err_no_alt.value)
282+
283+
# keyword is close enough for difflib to offer alternative
284+
with pytest.raises(InvalidQueryError) as err_with_alt:
285+
Observations.query_criteria_count(oops_collection='TESS')
286+
assert 'obs_collection' in str(err_with_alt.value)
287+
277288
# count functions
278289
def test_observations_query_region_count(self):
279290
maxRes = Observations.query_criteria_count()
@@ -881,6 +892,17 @@ def check_result(result, exp_vals):
881892
assert isinstance(result, Table)
882893
assert result['distance'][0] <= result['distance'][1]
883894

895+
def test_catalogs_query_criteria_invalid_keyword(self):
896+
# attempt to make a criteria query with invalid keyword
897+
with pytest.raises(InvalidQueryError) as err_no_alt:
898+
Catalogs.query_criteria(catalog='tic', not_a_keyword='TESS')
899+
assert "Filter 'not_a_keyword' does not exist." in str(err_no_alt.value)
900+
901+
# keyword is close enough for difflib to offer alternative
902+
with pytest.raises(InvalidQueryError) as err_with_alt:
903+
Catalogs.query_criteria(catalog='ctl', objectType="STAR")
904+
assert 'objType' in str(err_with_alt.value)
905+
884906
def test_catalogs_query_hsc_matchid_async(self):
885907
catalogData = Catalogs.query_object("M10",
886908
radius=.001,

0 commit comments

Comments
 (0)