Skip to content

Commit

Permalink
ruff PT
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadair committed Mar 6, 2024
1 parent 631496b commit 8108ca8
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions dkist/dataset/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions dkist/dataset/tests/test_load_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ 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):
ds = load_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):
Expand Down
4 changes: 2 additions & 2 deletions dkist/dataset/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions dkist/dataset/tests/test_tiled_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
10 changes: 5 additions & 5 deletions dkist/io/tests/test_file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions dkist/net/globus/tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]}
Expand All @@ -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):
Expand Down
13 changes: 6 additions & 7 deletions dkist/net/tests/test_attr_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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],
Expand All @@ -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):
Expand Down Expand Up @@ -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'),
Expand All @@ -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()


Expand Down
4 changes: 2 additions & 2 deletions dkist/net/tests/test_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions dkist/net/tests/test_attrs_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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),
Expand Down
7 changes: 4 additions & 3 deletions dkist/net/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion dkist/net/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 10 additions & 8 deletions dkist/wcs/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ 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,)),
((np.arange(10) * u.pix,
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

Expand Down Expand Up @@ -273,15 +273,15 @@ 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,)),
((np.arange(10) * u.pix,
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))
Expand Down Expand Up @@ -608,22 +608,24 @@ 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)


@pytest.mark.parametrize("order", ["A", "B"])
@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)


Expand Down

0 comments on commit 8108ca8

Please sign in to comment.