From 05e9f4cf6ff50152d9ecbe389acdb12f2c1fbd86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brigitta=20Sipo=CC=8Bcz?= Date: Sun, 11 Sep 2022 00:04:11 -0700 Subject: [PATCH] Fixing more warnings and removing them from the config --- astroquery/alfalfa/tests/test_alfalfa.py | 4 ++- astroquery/besancon/tests/test_besancon.py | 6 ++-- astroquery/cadc/tests/test_cadctap.py | 4 ++- astroquery/casda/tests/test_casda.py | 13 ++++--- .../exoplanet_orbit_database.py | 13 ++++--- .../heasarc/tests/test_heasarc_remote_isdc.py | 7 ++-- astroquery/ipac/irsa/ibe/core.py | 7 ++-- astroquery/ipac/irsa/sha/tests/test_sha.py | 4 +-- astroquery/ipac/ned/tests/test_ned.py | 8 +++-- astroquery/mast/tests/test_mast.py | 2 +- astroquery/nvas/tests/test_nvas.py | 11 +++--- astroquery/ogle/tests/test_ogle.py | 6 +++- astroquery/sdss/core.py | 2 +- astroquery/sdss/tests/test_sdss.py | 4 +-- astroquery/simbad/tests/test_simbad.py | 32 ++++++++--------- astroquery/ukidss/tests/test_ukidss.py | 10 +++--- astroquery/vamdc/core.py | 2 +- astroquery/vizier/tests/test_vizier.py | 3 +- setup.cfg | 34 +++++++++---------- 19 files changed, 99 insertions(+), 73 deletions(-) diff --git a/astroquery/alfalfa/tests/test_alfalfa.py b/astroquery/alfalfa/tests/test_alfalfa.py index df767d1823..12de35411f 100644 --- a/astroquery/alfalfa/tests/test_alfalfa.py +++ b/astroquery/alfalfa/tests/test_alfalfa.py @@ -38,7 +38,9 @@ def patch_get_readable_fileobj(request): @contextmanager def get_readable_fileobj_mockreturn(filename, **kwargs): file_obj = data_path(DATA_FILES['spectrum']) # TODO: add images option - yield open(file_obj, 'rb') # read as bytes, assuming FITS + # read as bytes, assuming FITS + with open(file_obj, 'rb') as inputfile: + yield inputfile mp = request.getfixturevalue("monkeypatch") diff --git a/astroquery/besancon/tests/test_besancon.py b/astroquery/besancon/tests/test_besancon.py index bf031f5977..06ef8379af 100644 --- a/astroquery/besancon/tests/test_besancon.py +++ b/astroquery/besancon/tests/test_besancon.py @@ -65,11 +65,11 @@ def get_readable_fileobj_mockreturn(filename, **kwargs): if isinstance(filename, str): if '1376235131.430670' in filename: is_binary = kwargs.get('encoding', None) == 'binary' - file_obj = open(data_path('1376235131.430670.resu'), - "r" + ('b' if is_binary else '')) + with open(data_path('1376235131.430670.resu'), "r" + ('b' if is_binary else '')) as file_obj: + yield file_obj else: file_obj = filename - yield file_obj + yield file_obj mp = request.getfixturevalue("monkeypatch") diff --git a/astroquery/cadc/tests/test_cadctap.py b/astroquery/cadc/tests/test_cadctap.py index 8497b3792c..c31ae6c5bf 100644 --- a/astroquery/cadc/tests/test_cadctap.py +++ b/astroquery/cadc/tests/test_cadctap.py @@ -177,7 +177,9 @@ def raise_for_status(self): class CapabilitiesResponse: def __init__(self): caps_file = data_path('tap_caps.xml') - self.text = open(caps_file, 'r').read() + with open(caps_file, 'r') as infile: + text = infile.read() + self.text = text def raise_for_status(self): pass diff --git a/astroquery/casda/tests/test_casda.py b/astroquery/casda/tests/test_casda.py index 6d0cdd629c..2beb3dc737 100644 --- a/astroquery/casda/tests/test_casda.py +++ b/astroquery/casda/tests/test_casda.py @@ -11,6 +11,7 @@ import astropy.units as u from astropy.table import Table, Column from astropy.io.votable import parse +from astropy.io.votable.exceptions import W03, W50 from astroquery import log import numpy as np @@ -269,7 +270,8 @@ def test_query_region_async_box(patch_get): def test_filter_out_unreleased(): - all_records = parse(data_path('partial_unreleased.xml'), verify='warn').get_first_table().to_table() + with pytest.warns(W03): + all_records = parse(data_path('partial_unreleased.xml'), verify='warn').get_first_table().to_table() assert all_records[0]['obs_release_date'] == '2017-08-02T03:51:19.728Z' assert all_records[1]['obs_release_date'] == '2218-01-02T16:51:00.728Z' assert all_records[2]['obs_release_date'] == '' @@ -331,7 +333,8 @@ def test_stage_data(patch_get): casda = Casda() fake_login(casda, USERNAME, PASSWORD) casda.POLL_INTERVAL = 1 - urls = casda.stage_data(table, verbose=True) + with pytest.warns(W50, match="Invalid unit string 'pixels'"): + urls = casda.stage_data(table, verbose=True) assert urls == ['http://casda.csiro.au/download/web/111-000-111-000/askap_img.fits.checksum', 'http://casda.csiro.au/download/web/111-000-111-000/askap_img.fits'] @@ -348,7 +351,8 @@ def test_cutout(patch_get): casda = Casda() fake_login(casda, USERNAME, PASSWORD) casda.POLL_INTERVAL = 1 - urls = casda.cutout(table, coordinates=centre, radius=radius, verbose=True) + with pytest.warns(W50, match="Invalid unit string 'pixels'"): + urls = casda.cutout(table, coordinates=centre, radius=radius, verbose=True) assert urls == ['http://casda.csiro.au/download/web/111-000-111-000/cutout.fits.checksum', 'http://casda.csiro.au/download/web/111-000-111-000/cutout.fits'] @@ -367,7 +371,8 @@ def test_cutout_no_args(patch_get): casda.POLL_INTERVAL = 1 with pytest.raises(ValueError, match=r"Please provide cutout parameters such as coordinates, band or channel\.") as excinfo: - casda.cutout(table) + with pytest.warns(W50, match="Invalid unit string 'pixels'"): + casda.cutout(table) def test_cutout_unauthorised(patch_get): diff --git a/astroquery/exoplanet_orbit_database/exoplanet_orbit_database.py b/astroquery/exoplanet_orbit_database/exoplanet_orbit_database.py index 2ea35bcb42..0ec703cbb5 100644 --- a/astroquery/exoplanet_orbit_database/exoplanet_orbit_database.py +++ b/astroquery/exoplanet_orbit_database/exoplanet_orbit_database.py @@ -1,6 +1,7 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst import json import os +import warnings from astropy.utils.data import download_file from astropy.io import ascii @@ -30,9 +31,9 @@ def __init__(self): def param_units(self): if self._param_units is None: module_dir = os.path.dirname(os.path.abspath(__file__)) - units_file = open(os.path.join(module_dir, 'data', - 'exoplanet_orbit_database_units.json')) - self._param_units = json.load(units_file) + filename = os.path.join(module_dir, 'data', 'exoplanet_orbit_database_units.json') + with open(filename) as units_file: + self._param_units = json.load(units_file) return self._param_units @@ -82,7 +83,11 @@ def get_table(self, cache=True, show_progress=True, table_path=None): except ValueError: print(f"WARNING: Unit {self.param_units[col]} not recognised") - self._table = QTable(exoplanets_table) + # Masked quantities are not supported in older astropy, warnings are raised for = 4 # shown as '..'; ignore @@ -362,7 +361,7 @@ def list_tables(self, mission=None, dataset=None, cache=True): response = self._request('GET', url, timeout=self.TIMEOUT, cache=cache) - root = BeautifulSoup(response.text) + root = BeautifulSoup(response.text, 'html5lib') return [tr.find('td').string for tr in root.findAll('tr')[1:]] # Unfortunately, the URL construction for each data set is different, and diff --git a/astroquery/ipac/irsa/sha/tests/test_sha.py b/astroquery/ipac/irsa/sha/tests/test_sha.py index 367891eb82..d162c4e002 100644 --- a/astroquery/ipac/irsa/sha/tests/test_sha.py +++ b/astroquery/ipac/irsa/sha/tests/test_sha.py @@ -22,8 +22,8 @@ def data_path(filename): def get_mockreturn(url, params=None, stream=False, timeout=10, **kwargs): if stream: filename = data_path(DATA_FILES['img']) - return MockResponse(open(filename, 'rb').read(), - content_type='image/fits', **kwargs) + with open(filename, 'rb') as infile: + return MockResponse(infile.read(), content_type='image/fits', **kwargs) elif params['RA'] == 163.6136: filename = data_path(DATA_FILES['pos_t']) elif params['NAIFID'] == 2003226: diff --git a/astroquery/ipac/ned/tests/test_ned.py b/astroquery/ipac/ned/tests/test_ned.py index 71fefaa8c0..d16fbe33a7 100644 --- a/astroquery/ipac/ned/tests/test_ned.py +++ b/astroquery/ipac/ned/tests/test_ned.py @@ -1,6 +1,7 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst import os +from contextlib import contextmanager from numpy import testing as npt import pytest @@ -47,12 +48,14 @@ def patch_get(request): @pytest.fixture def patch_get_readable_fileobj(request): + @contextmanager def get_readable_fileobj_mockreturn(filename, cache=True, encoding=None, show_progress=True): # Need to read FITS files with binary encoding: should raise error # otherwise assert encoding == 'binary' - return open(data_path(DATA_FILES['image']), 'rb') + with open(data_path(DATA_FILES['image']), 'rb') as infile: + yield infile mp = request.getfixturevalue("monkeypatch") @@ -138,7 +141,8 @@ def test_photometry(patch_get): def test_extract_image_urls(): - html_in = open(data_path(DATA_FILES['extract_urls']), 'r').read() + with open(data_path(DATA_FILES['extract_urls']), 'r') as infile: + html_in = infile.read() url_list = ned.core.Ned._extract_image_urls(html_in) assert len(url_list) == 5 for url in url_list: diff --git a/astroquery/mast/tests/test_mast.py b/astroquery/mast/tests/test_mast.py index 842e20d96f..2ebe5985a9 100644 --- a/astroquery/mast/tests/test_mast.py +++ b/astroquery/mast/tests/test_mast.py @@ -614,7 +614,7 @@ def test_catalogs_query_criteria(patch_post): assert isinstance(result, Table) with pytest.raises(InvalidQueryError) as invalid_query: - mast.Catalogs.query_criteria(catalog="Tic", objectName="M10") + mast.Catalogs.query_criteria(catalog="Tic", objectname="M10") assert "non-positional" in str(invalid_query.value) diff --git a/astroquery/nvas/tests/test_nvas.py b/astroquery/nvas/tests/test_nvas.py index 1606c3ce18..01ada69b64 100644 --- a/astroquery/nvas/tests/test_nvas.py +++ b/astroquery/nvas/tests/test_nvas.py @@ -60,11 +60,11 @@ def patch_get_readable_fileobj(request): def get_readable_fileobj_mockreturn(filename, **kwargs): encoding = kwargs.get('encoding', None) if encoding == 'binary': - file_obj = open(data_path(DATA_FILES["image"]), 'rb') + with open(data_path(DATA_FILES["image"]), 'rb') as file_obj: + yield file_obj else: - file_obj = open(data_path(DATA_FILES["image"]), - "r", encoding=encoding) - yield file_obj + with open(data_path(DATA_FILES["image"]), "r", encoding=encoding) as file_obj: + yield file_obj mp = request.getfixturevalue("monkeypatch") @@ -95,7 +95,8 @@ def test_parse_coordinates(coordinates): def test_extract_image_urls(): - html_in = open(data_path(DATA_FILES['image_search']), 'r').read() + with open(data_path(DATA_FILES['image_search']), 'r') as infile: + html_in = infile.read() image_list = nvas.core.Nvas.extract_image_urls(html_in) assert len(image_list) == 2 diff --git a/astroquery/ogle/tests/test_ogle.py b/astroquery/ogle/tests/test_ogle.py index 895650a092..aebe3e62b6 100644 --- a/astroquery/ogle/tests/test_ogle.py +++ b/astroquery/ogle/tests/test_ogle.py @@ -5,8 +5,11 @@ import pytest from astropy.coordinates import SkyCoord from astropy import units as u +from astropy.utils.exceptions import AstropyDeprecationWarning + from astroquery.utils.mocks import MockResponse + DATA_FILES = {'gal_0_3': 'gal_0_3.txt'} @@ -56,4 +59,5 @@ def test_ogle_list_values(patch_post): coordinates """ co_list = [[0, 0, 0], [3, 3, 3]] - ogle.core.Ogle.query_region(coord=co_list) + with pytest.warns(AstropyDeprecationWarning): + ogle.core.Ogle.query_region(coord=co_list) diff --git a/astroquery/sdss/core.py b/astroquery/sdss/core.py index d27a98f047..e5b5c0739f 100644 --- a/astroquery/sdss/core.py +++ b/astroquery/sdss/core.py @@ -1002,7 +1002,7 @@ def _args_to_payload(self, coordinates=None, else: if field_help is not True: warnings.warn(f"{field_help} isn't a valid 'photobj_field' or " - "'specobj_field' field, valid fields are" + "'specobj_field' field, valid fields are " "returned.") return {'photoobj_all': photoobj_all, 'specobj_all': specobj_all} diff --git a/astroquery/sdss/tests/test_sdss.py b/astroquery/sdss/tests/test_sdss.py index b486f0da7d..780ae62704 100644 --- a/astroquery/sdss/tests/test_sdss.py +++ b/astroquery/sdss/tests/test_sdss.py @@ -504,8 +504,8 @@ def test_field_help_region(patch_request): existing_s_field = sdss.SDSS.query_region(coords, field_help='spectroSynFlux_r') - non_existing_field = sdss.SDSS.query_region(coords, - field_help='nonexist') + with pytest.warns(UserWarning, match="nonexist isn't a valid 'photobj_field' or 'specobj_field'"): + non_existing_field = sdss.SDSS.query_region(coords, field_help='nonexist') assert existing_p_field is None assert existing_s_field is None diff --git a/astroquery/simbad/tests/test_simbad.py b/astroquery/simbad/tests/test_simbad.py index 3fb3b032c7..da0770542f 100644 --- a/astroquery/simbad/tests/test_simbad.py +++ b/astroquery/simbad/tests/test_simbad.py @@ -125,15 +125,12 @@ def test_parse_result(): result1 = sb._parse_result( MockResponseSimbad('query id '), simbad.core.SimbadVOTableResult) assert isinstance(result1, Table) - with pytest.raises(TableParseError) as ex: + expected_exception = 'Failed to parse SIMBAD result! The raw response can be found in self.last_response, ' + + with pytest.raises(TableParseError, match=expected_exception): sb._parse_result(MockResponseSimbad('query error '), simbad.core.SimbadVOTableResult) - assert str(ex.value) == ('Failed to parse SIMBAD result! The raw response ' - 'can be found in self.last_response, and the ' - 'error in self.last_table_parse_error. ' - 'The attempted parsed result is in ' - 'self.last_parsed_result.\n Exception: 7:115: ' - 'no element found') + assert isinstance(sb.last_response.text, str) assert isinstance(sb.last_response.content, bytes) @@ -361,15 +358,18 @@ def test_votable_fields(): set(['main_id', 'coordinates', 'rot', 'z_value', 'velocity'])) sb.remove_votable_fields('rot', 'main_id', 'coordinates') assert set(sb.get_votable_fields()) == set(['z_value', 'velocity']) - sb.remove_votable_fields('rot', 'main_id', 'coordinates') + # Warning is expected as we removed the 'coordinates' field above: + with pytest.warns(UserWarning, match="coordinates: this field is not set"): + sb.remove_votable_fields('rot', 'main_id', 'coordinates') assert set(sb.get_votable_fields()) == set(['z_value', 'velocity']) - sb.remove_votable_fields('z_value', 'velocity') - assert (set(sb.get_votable_fields()) == - set(['main_id', 'coordinates'])) + with pytest.warns(UserWarning, match="All fields have been removed. Resetting"): + sb.remove_votable_fields('z_value', 'velocity') + assert set(sb.get_votable_fields()) == set(['main_id', 'coordinates']) sb.add_votable_fields('rot', 'z_value', 'velocity') - sb.reset_votable_fields() assert (set(sb.get_votable_fields()) == - set(['main_id', 'coordinates'])) + set(['main_id', 'coordinates', 'rot', 'z_value', 'velocity'])) + sb.reset_votable_fields() + assert set(sb.get_votable_fields()) == set(['main_id', 'coordinates']) def test_query_criteria1(patch_post): @@ -394,9 +394,9 @@ def test_simbad_settings1(): sb = simbad.core.Simbad() assert sb.get_votable_fields() == ['main_id', 'coordinates'] sb.add_votable_fields('ra', 'dec(5)') - sb.remove_votable_fields('ra', 'dec') - assert (sb.get_votable_fields() == - ['main_id', 'coordinates', 'dec(5)']) + with pytest.warns(UserWarning, match="dec: this field is not set"): + sb.remove_votable_fields('ra', 'dec') + assert sb.get_votable_fields() == ['main_id', 'coordinates', 'dec(5)'] sb.reset_votable_fields() diff --git a/astroquery/ukidss/tests/test_ukidss.py b/astroquery/ukidss/tests/test_ukidss.py index e2fcb8645a..50636c8392 100644 --- a/astroquery/ukidss/tests/test_ukidss.py +++ b/astroquery/ukidss/tests/test_ukidss.py @@ -41,10 +41,11 @@ def get_readable_fileobj_mockreturn(filename, **kwargs): is_binary = kwargs.get('encoding', None) == 'binary' mode = 'rb' if is_binary else 'r' if "fits" in filename: - file_obj = open(data_path(DATA_FILES["image"]), mode) + with open(data_path(DATA_FILES["image"]), mode) as file_obj: + yield file_obj else: - file_obj = open(data_path(DATA_FILES["votable"]), mode) - yield file_obj + with open(data_path(DATA_FILES["votable"]), mode) as file_obj: + yield file_obj mp = request.getfixturevalue("monkeypatch") @@ -135,7 +136,8 @@ def test_get_image_list(patch_get, patch_get_readable_fileobj): def test_extract_urls(): - html_in = open(data_path(DATA_FILES["image_results"]), 'r').read() + with open(data_path(DATA_FILES["image_results"]), 'r') as infile: + html_in = infile.read() urls = ukidss.core.Ukidss.extract_urls(html_in) assert len(urls) == 1 diff --git a/astroquery/vamdc/core.py b/astroquery/vamdc/core.py index cb90d28ca2..f3fd573f48 100644 --- a/astroquery/vamdc/core.py +++ b/astroquery/vamdc/core.py @@ -13,7 +13,7 @@ __doctest_skip__ = ['VamdcClass.*'] -@deprecated('0.4.2', 'the module relies on an unmaintained library and is' +@deprecated('0.4.2', 'the vamdc astroquery module relies on an unmaintained library and is' 'considered deprecated until completely refactored or upstream' 'is stablised.') @async_to_sync diff --git a/astroquery/vizier/tests/test_vizier.py b/astroquery/vizier/tests/test_vizier.py index 4f5bd76d65..4672de449a 100644 --- a/astroquery/vizier/tests/test_vizier.py +++ b/astroquery/vizier/tests/test_vizier.py @@ -229,7 +229,8 @@ def test_keywords(self): v = vizier.core.Vizier(keywords=['optical', 'chandra', 'ans']) assert str(v.keywords) == ('-kw.Mission=ANS\n-kw.Mission=' 'Chandra\n-kw.Wavelength=optical') - v = vizier.core.Vizier(keywords=['xy', 'optical']) + with pytest.warns(UserWarning, match="xy : No such keyword"): + v = vizier.core.Vizier(keywords=['xy', 'optical']) assert str(v.keywords) == '-kw.Wavelength=optical' v.keywords = ['optical', 'cobe'] assert str(v.keywords) == '-kw.Mission=COBE\n-kw.Wavelength=optical' diff --git a/setup.cfg b/setup.cfg index 45a8d376a1..6aadce237c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -35,29 +35,29 @@ remote_data_strict = true addopts = --doctest-rst filterwarnings = error - ignore: Experimental:UserWarning: -# This is a temporary measure, all of these should be fixed: +## These are temporary measures, all of these should be fixed: ignore:distutils Version classes are deprecated:DeprecationWarning - ignore::pytest.PytestUnraisableExceptionWarning ignore::numpy.VisibleDeprecationWarning ignore:unclosed =4.1