diff --git a/.ruff.toml b/.ruff.toml index 8cca398d1..6ac25a524 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -5,7 +5,7 @@ exclude = [ "__pycache__", "build", ] -select = ["F", "E", "W", "UP", "I", "C4"] +select = ["F", "E", "W", "UP", "I", "C4", "PT"] extend-ignore = [ # pycodestyle (E, W) "E501", # LineTooLong # TODO! fix diff --git a/dkist/dataset/tests/test_dataset.py b/dkist/dataset/tests/test_dataset.py index 061cfb868..93560936f 100644 --- a/dkist/dataset/tests/test_dataset.py +++ b/dkist/dataset/tests/test_dataset.py @@ -80,15 +80,13 @@ def test_load_from_directory(): def test_from_directory_no_asdf(tmp_path): - with pytest.raises(ValueError) as e: + with pytest.raises(ValueError, match="No asdf file found"): load_dataset(tmp_path) - assert "No asdf file found" in str(e) def test_from_not_directory(): - with pytest.raises(ValueError) as e: + with pytest.raises(ValueError, match="directory argument"): load_dataset(rootdir / "notadirectory") - assert "directory argument" in str(e) def test_load_tiled_dataset(): @@ -114,15 +112,13 @@ def test_load_with_old_methods(): def test_from_directory_not_dir(): - with pytest.raises(ValueError) as e: + with pytest.raises(ValueError, match="must be a directory"): load_dataset(rootdir / 'EIT' / 'eit_2004-03-01T00_00_10.515000.asdf') - assert "must be a directory" in str(e) def test_load_with_invalid_input(): - with pytest.raises(TypeError) as e: + with pytest.raises(TypeError, match="Input type note recognised."): load_dataset(42) - assert "Input type not recognised." in str(e) def test_crop_few_slices(dataset_4d): diff --git a/dkist/dataset/tests/test_load_dataset.py b/dkist/dataset/tests/test_load_dataset.py index b8392d55b..bc24a1475 100644 --- a/dkist/dataset/tests/test_load_dataset.py +++ b/dkist/dataset/tests/test_load_dataset.py @@ -49,12 +49,12 @@ def fixture_finder(request): return request.getfixturevalue(request.param) -@pytest.mark.parametrize("fixture_finder", ( +@pytest.mark.parametrize("fixture_finder", [ "asdf_path", "asdf_str", "single_asdf_in_folder", "single_asdf_in_folder_str", - ), + ], indirect=True ) def test_load_single_dataset(fixture_finder): @@ -62,10 +62,10 @@ def test_load_single_dataset(fixture_finder): assert isinstance(ds, Dataset) -@pytest.mark.parametrize("fixture_finder", ( +@pytest.mark.parametrize("fixture_finder", [ ["asdf_path", "asdf_str", "single_asdf_in_folder", "single_asdf_in_folder_str"], ("asdf_path", "asdf_str", "single_asdf_in_folder", "single_asdf_in_folder_str"), - ), + ], indirect=True ) def test_load_multiple(fixture_finder): diff --git a/dkist/dataset/tests/test_plotting.py b/dkist/dataset/tests/test_plotting.py index bc6e65b54..2965db14a 100644 --- a/dkist/dataset/tests/test_plotting.py +++ b/dkist/dataset/tests/test_plotting.py @@ -6,7 +6,7 @@ @pytest.mark.mpl_image_compare -@pytest.mark.parametrize("aslice", (np.s_[0, :, :], np.s_[:, 0, :], np.s_[:, :, 0])) +@pytest.mark.parametrize("aslice", [np.s_[0, :, :], np.s_[:, 0, :], np.s_[:, :, 0]]) def test_dataset_projection(dataset_3d, aslice): pytest.importorskip("ndcube", "2.0.2") # https://github.com/sunpy/ndcube/pull/509 ds = dataset_3d[aslice] @@ -17,7 +17,7 @@ def test_dataset_projection(dataset_3d, aslice): @pytest.mark.mpl_image_compare -@pytest.mark.parametrize("aslice", (np.s_[0, :, :], np.s_[:, 0, :], np.s_[:, :, 0])) +@pytest.mark.parametrize("aslice", [np.s_[0, :, :], np.s_[:, 0, :], np.s_[:, :, 0]]) def test_2d_plot(dataset_3d, aslice): fig = plt.figure() dataset_3d[aslice].plot() diff --git a/dkist/dataset/tests/test_tiled_dataset.py b/dkist/dataset/tests/test_tiled_dataset.py index 2d08da926..c63f36a23 100644 --- a/dkist/dataset/tests/test_tiled_dataset.py +++ b/dkist/dataset/tests/test_tiled_dataset.py @@ -16,12 +16,12 @@ def test_tiled_dataset(simple_tiled_dataset, dataset): assert simple_tiled_dataset.shape == (2, 2) -@pytest.mark.parametrize("aslice", (np.s_[0,0], +@pytest.mark.parametrize("aslice", [np.s_[0,0], np.s_[0], np.s_[...,0], np.s_[:,1], np.s_[1,1], - np.s_[0:2, :])) + np.s_[0:2, :]]) def test_tiled_dataset_slice(simple_tiled_dataset, aslice): assert np.all(simple_tiled_dataset[aslice] == simple_tiled_dataset._data[aslice]) diff --git a/dkist/io/tests/test_file_manager.py b/dkist/io/tests/test_file_manager.py index 22b8ab0f5..f1e459ee8 100644 --- a/dkist/io/tests/test_file_manager.py +++ b/dkist/io/tests/test_file_manager.py @@ -162,7 +162,7 @@ def test_reprs(file_manager): @pytest.fixture def orchestrate_transfer_mock(mocker): - yield mocker.patch("dkist.net.helpers._orchestrate_transfer_task", + return mocker.patch("dkist.net.helpers._orchestrate_transfer_task", autospec=True) @@ -254,10 +254,10 @@ def test_length_one_first_array_axis(small_visp_dataset): assert len(small_visp_dataset[:, 5, 5].files.filenames) == 3 -@pytest.mark.parametrize("kwargs", ( +@pytest.mark.parametrize("kwargs", [ {}, {"path": "~/", "overwrite": True} -)) +]) def test_download_quality(mocker, small_visp_dataset, kwargs): simple_download = mocker.patch("dkist.io.file_manager.Downloader.simple_download") from dkist.net import conf @@ -276,10 +276,10 @@ def test_download_quality(mocker, small_visp_dataset, kwargs): ) -@pytest.mark.parametrize("kwargs", ( +@pytest.mark.parametrize("kwargs", [ {}, {"path": "~/", "overwrite": True} -)) +]) def test_download_quality_movie(mocker, small_visp_dataset, kwargs): simple_download = mocker.patch("dkist.io.file_manager.Downloader.simple_download") from dkist.net import conf diff --git a/dkist/net/globus/tests/test_endpoints.py b/dkist/net/globus/tests/test_endpoints.py index 9ea8e1d22..71968d79a 100644 --- a/dkist/net/globus/tests/test_endpoints.py +++ b/dkist/net/globus/tests/test_endpoints.py @@ -52,7 +52,7 @@ def test_get_transfer_client(mocker, transfer_client): assert isinstance(transfer_client, globus_sdk.TransferClient) -@pytest.mark.parametrize("endpoint_id", ("12345", None)) +@pytest.mark.parametrize("endpoint_id", ["12345", None]) def test_get_local_endpoint_id(mocker, endpoint_id): lgcp_mock = mocker.patch("globus_sdk.LocalGlobusConnectPersonal.endpoint_id", new_callable=mocker.PropertyMock) @@ -76,9 +76,8 @@ def test_get_endpoint_id_search(mocker, mock_search, endpoint_search, transfer_c assert endpoint_id == "dd1ee92a-6d04-11e5-ba46-22000b92c6ec" # Test multiple match fail - with pytest.raises(ValueError) as exc: + with pytest.raises(ValueError, match="Multiple"): get_endpoint_id(" ", transfer_client) - assert "Multiple" in str(exc.value) # Test just one result mock_search.return_value = {"DATA": endpoint_search["DATA"][1:2]} @@ -87,9 +86,8 @@ def test_get_endpoint_id_search(mocker, mock_search, endpoint_search, transfer_c # Test no results mock_search.return_value = {"DATA": []} - with pytest.raises(ValueError) as e_info: + with pytest.raises(ValueError, match="No matches"): get_endpoint_id(" ", transfer_client) - assert "No matches" in str(e_info.value) def test_get_endpoint_id_uuid(mocker, transfer_client, endpoint_search): diff --git a/dkist/net/tests/test_attr_walker.py b/dkist/net/tests/test_attr_walker.py index 1a4796706..d8d1d998f 100644 --- a/dkist/net/tests/test_attr_walker.py +++ b/dkist/net/tests/test_attr_walker.py @@ -23,7 +23,7 @@ def query_or_instrument(): """ return (a.Instrument("VBI") | a.Instrument("VISP")) & a.Time("2020/06/01", "2020/06/02") -@pytest.fixture(scope="function") +@pytest.fixture() def boundingbox_params(): """ Create possible bounding box input coordinates and args @@ -42,7 +42,7 @@ def boundingbox_params(): width = 3.4 * u.deg height = 1.2 * u.deg - yield { + return { # bottom_left, top_right, width, height "bottom left vector icrs": [bottom_left_vector_icrs, None, None, None], "bottom left top right icrs": [bottom_left_icrs, top_right_icrs, None, None], @@ -51,13 +51,12 @@ def boundingbox_params(): } -@pytest.fixture(scope="function", - params=["bottom left vector icrs", +@pytest.fixture(params=["bottom left vector icrs", "bottom left top right icrs", "bottom left top right", "bottom left width height",],) def boundingbox_param(request, boundingbox_params): - yield boundingbox_params[request.param] + return boundingbox_params[request.param] def test_walker_single(all_attrs_classes, api_param_names): @@ -111,7 +110,7 @@ def test_walker_single(all_attrs_classes, api_param_names): assert not set(api_param_names[all_attrs_classes]).difference(params[0].keys()) -@pytest.mark.parametrize("search,search_type", +@pytest.mark.parametrize(("search", "search_type"), [ ('containing', 'rectangleContainingBoundingBox'), ('contained', 'rectangleContainedByBoundingBox'), @@ -136,7 +135,7 @@ def test_boundingbox(search, search_type, boundingbox_param): assert coordinate_regex.search(value) def test_args_browsemovie(): - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Either movieurl or movieobjectkey must be specified"): da.BrowseMovie() diff --git a/dkist/net/tests/test_attrs.py b/dkist/net/tests/test_attrs.py index 6ae464659..74252a5b7 100644 --- a/dkist/net/tests/test_attrs.py +++ b/dkist/net/tests/test_attrs.py @@ -11,8 +11,8 @@ def test_embargoed_inputs(): assert not da.Embargoed.false.value assert da.Embargoed.true.value - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="is_embargoed must be either True or False"): da.Embargoed("neither up nor down") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="is_embargoed must be either True or False"): da.Embargoed(42) diff --git a/dkist/net/tests/test_attrs_values.py b/dkist/net/tests/test_attrs_values.py index 6dce033ee..fa169f6e0 100644 --- a/dkist/net/tests/test_attrs_values.py +++ b/dkist/net/tests/test_attrs_values.py @@ -130,7 +130,7 @@ def test_attempt_local_update_error_download(mocker, caplog_dkist, tmp_homedir, ("dkist", logging.ERROR, "Failed to download new attrs values."), ] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="This is a value error"): success = attempt_local_update(silence_errors=False) @@ -155,7 +155,7 @@ def test_attempt_local_update_fail_invalid_download(mocker, tmp_path, caplog_dki success = attempt_local_update(user_file=json_file, silence_errors=False) -@pytest.mark.parametrize("user_file, update_needed, allow_update, should_update", [ +@pytest.mark.parametrize(("user_file", "update_needed", "allow_update", "should_update"), [ ("user_file", False, True, False), ("user_file", True, True, True), ("user_file", True, False, False), diff --git a/dkist/net/tests/test_client.py b/dkist/net/tests/test_client.py index 9070c58b0..e3b302c3e 100644 --- a/dkist/net/tests/test_client.py +++ b/dkist/net/tests/test_client.py @@ -39,7 +39,8 @@ def test_search_by_time(client, time): res = client.search(time, a.Instrument("VBI")) assert len(res) == 1 assert res[0]["Primary Proposal ID"] == "pid_1_50" - assert res[0]["Start Time"].value == '2022-12-27T19:27:42.338' and res[0]["End Time"].value == '2022-12-27T20:00:09.005' + assert res[0]["Start Time"].value == '2022-12-27T19:27:42.338' + assert res[0]["End Time"].value == '2022-12-27T20:00:09.005' @pytest.fixture def empty_query_response(): @@ -202,14 +203,14 @@ def test_can_handle_query(client, query): assert client._can_handle_query(query) -@pytest.mark.parametrize("query", ( +@pytest.mark.parametrize("query", [ a.Instrument("bob"), a.Physobs("who's got the button"), a.Level(2), (a.Instrument("VBI"), a.Level(0)), (a.Instrument("VBI"), a.Detector("test")), (), -)) +]) def test_cant_handle_query(client, query): """Some examples of invalid queries.""" assert not client._can_handle_query(query) diff --git a/dkist/net/tests/test_helpers.py b/dkist/net/tests/test_helpers.py index 11be07dd2..4554d8120 100644 --- a/dkist/net/tests/test_helpers.py +++ b/dkist/net/tests/test_helpers.py @@ -11,7 +11,7 @@ @pytest.fixture def orchestrate_transfer_mock(mocker): - yield mocker.patch("dkist.net.helpers._orchestrate_transfer_task", autospec=True) + return mocker.patch("dkist.net.helpers._orchestrate_transfer_task", autospec=True) @pytest.mark.parametrize( diff --git a/dkist/wcs/tests/test_models.py b/dkist/wcs/tests/test_models.py index 45468a3e3..b0fe982ca 100755 --- a/dkist/wcs/tests/test_models.py +++ b/dkist/wcs/tests/test_models.py @@ -99,7 +99,7 @@ def test_varying_transform_pc(): assert u.allclose(vct.inverse(*world, 5*u.pix), pixel[:2], atol=0.01*u.pix) -@pytest.mark.parametrize(("pixel", "lon_shape"), ( +@pytest.mark.parametrize(("pixel", "lon_shape"), [ ((*np.mgrid[0:10, 0:10] * u.pix, np.arange(10) * u.pix), (10, 10)), (np.mgrid[0:10, 0:10, 0:5] * u.pix, (10, 10, 5)), ((2 * u.pix, 2 * u.pix, np.arange(10) * u.pix), (10,)), @@ -107,7 +107,7 @@ def test_varying_transform_pc(): np.arange(10) * u.pix, np.arange(10)[..., None] * u.pix), (10, 10)), (np.mgrid[0:1024, 0:1000, 0:2] * u.pix, (1024, 1000, 2)), -)) +]) def test_varying_transform_pc_shapes(pixel, lon_shape): varying_matrix_lt = [rotation_matrix(a)[:2, :2] for a in np.linspace(0, 90, 10)] * u.pix @@ -273,7 +273,7 @@ def test_varying_transform_4d_pc_unitless(): assert np.isnan(vct(0, 0, -10, 0)).all() -@pytest.mark.parametrize(("pixel", "lon_shape"), ( +@pytest.mark.parametrize(("pixel", "lon_shape"), [ ((*np.mgrid[0:5, 0:5] * u.pix, np.arange(5) * u.pix, 0 * u.pix), (5, 5)), (np.mgrid[0:10, 0:10, 0:5, 0:3] * u.pix, (10, 10, 5, 3)), ((2 * u.pix, 2 * u.pix, 0*u.pix, np.arange(3) * u.pix), (3,)), @@ -281,7 +281,7 @@ def test_varying_transform_4d_pc_unitless(): np.arange(10) * u.pix, np.arange(5)[..., None] * u.pix, np.arange(3)[..., None, None]), (3, 5, 10)), -)) +]) def test_varying_transform_4d_pc_shapes(pixel, lon_shape): varying_matrix_lt = [rotation_matrix(a)[:2, :2] for a in np.linspace(0, 90, 15)] * u.pix varying_matrix_lt = varying_matrix_lt.reshape((5, 3, 2, 2)) @@ -608,14 +608,16 @@ def test_ravel_repr(ndim, order): array_shape = tuple(rng.integers(1, 21, ndim)) ravel = Ravel(array_shape, order=order) unravel = ravel.inverse - assert str(array_shape) in repr(ravel) and order in repr(ravel) - assert str(array_shape) in repr(unravel) and order in repr(unravel) + assert str(array_shape) in repr(ravel) + assert order in repr(ravel) + assert str(array_shape) in repr(unravel) + assert order in repr(unravel) @pytest.mark.parametrize("array_shape", [(0, 1), (1, 0), (1,)]) @pytest.mark.parametrize("ravel", [Ravel, Unravel]) def test_ravel_bad_array_shape(array_shape, ravel): - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="array_shape must be at least 2D and have values >= 1"): ravel(array_shape) @@ -623,7 +625,7 @@ def test_ravel_bad_array_shape(array_shape, ravel): @pytest.mark.parametrize("ravel", [Ravel, Unravel]) def test_ravel_bad_order(order, ravel): array_shape=(2, 2, 2) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="order kwarg must be one of 'C' or 'F'"): ravel(array_shape, order)