Skip to content

Commit a08a6cf

Browse files
authored
Enable ruff's flake8-pytest-style (PT) rules (#2899)
1 parent 90b43d9 commit a08a6cf

File tree

9 files changed

+55
-36
lines changed

9 files changed

+55
-36
lines changed

pygmt/tests/test_clib.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,12 @@ def test_call_module_error_message():
167167
Check is the GMT error message was captured.
168168
"""
169169
with clib.Session() as lib:
170-
try:
170+
with pytest.raises(GMTCLibError) as exc_info:
171171
lib.call_module("info", "bogus-data.bla")
172-
except GMTCLibError as error:
173-
assert "Module 'info' failed with status code" in str(error)
174-
assert "gmtinfo [ERROR]: Cannot find file bogus-data.bla" in str(error)
172+
assert "Module 'info' failed with status code" in exc_info.value.args[0]
173+
assert (
174+
"gmtinfo [ERROR]: Cannot find file bogus-data.bla" in exc_info.value.args[0]
175+
)
175176

176177

177178
def test_method_no_session():
@@ -316,9 +317,9 @@ def test_create_data_fails():
316317
)
317318

318319
# If the data pointer returned is None (NULL pointer)
319-
with pytest.raises(GMTCLibError):
320-
with clib.Session() as lib:
321-
with mock(lib, "GMT_Create_Data", returns=None):
320+
with clib.Session() as lib:
321+
with mock(lib, "GMT_Create_Data", returns=None):
322+
with pytest.raises(GMTCLibError):
322323
lib.create_data(
323324
family="GMT_IS_DATASET",
324325
geometry="GMT_IS_SURFACE",

pygmt/tests/test_clib_loading.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,15 @@ def _mock_ctypes_cdll_return(self, libname):
130130
# libname is a loaded GMT library
131131
return self.loaded_libgmt
132132

133-
@pytest.fixture
134-
def mock_ctypes(self, monkeypatch):
133+
@pytest.fixture()
134+
def _mock_ctypes(self, monkeypatch):
135135
"""
136136
Patch the ctypes.CDLL function.
137137
"""
138138
monkeypatch.setattr(ctypes, "CDLL", self._mock_ctypes_cdll_return)
139139

140-
def test_two_broken_libraries(self, mock_ctypes):
140+
@pytest.mark.usefixtures("_mock_ctypes")
141+
def test_two_broken_libraries(self):
141142
"""
142143
Case 1: two broken libraries.
143144
@@ -154,7 +155,8 @@ def test_two_broken_libraries(self, mock_ctypes):
154155
with pytest.raises(GMTCLibNotFoundError, match=msg_regex):
155156
load_libgmt(lib_fullnames=lib_fullnames)
156157

157-
def test_load_brokenlib_invalidpath(self, mock_ctypes):
158+
@pytest.mark.usefixtures("_mock_ctypes")
159+
def test_load_brokenlib_invalidpath(self):
158160
"""
159161
Case 2: broken library + invalid path.
160162
@@ -171,28 +173,32 @@ def test_load_brokenlib_invalidpath(self, mock_ctypes):
171173
with pytest.raises(GMTCLibNotFoundError, match=msg_regex):
172174
load_libgmt(lib_fullnames=lib_fullnames)
173175

174-
def test_brokenlib_invalidpath_workinglib(self, mock_ctypes):
176+
@pytest.mark.usefixtures("_mock_ctypes")
177+
def test_brokenlib_invalidpath_workinglib(self):
175178
"""
176179
Case 3: broken library + invalid path + working library.
177180
"""
178181
lib_fullnames = [self.faked_libgmt1, self.invalid_path, self.loaded_libgmt]
179182
assert check_libgmt(load_libgmt(lib_fullnames=lib_fullnames)) is None
180183

181-
def test_invalidpath_brokenlib_workinglib(self, mock_ctypes):
184+
@pytest.mark.usefixtures("_mock_ctypes")
185+
def test_invalidpath_brokenlib_workinglib(self):
182186
"""
183187
Case 4: invalid path + broken library + working library.
184188
"""
185189
lib_fullnames = [self.invalid_path, self.faked_libgmt1, self.loaded_libgmt]
186190
assert check_libgmt(load_libgmt(lib_fullnames=lib_fullnames)) is None
187191

188-
def test_workinglib_brokenlib_invalidpath(self, mock_ctypes):
192+
@pytest.mark.usefixtures("_mock_ctypes")
193+
def test_workinglib_brokenlib_invalidpath(self):
189194
"""
190195
Case 5: working library + broken library + invalid path.
191196
"""
192197
lib_fullnames = [self.loaded_libgmt, self.faked_libgmt1, self.invalid_path]
193198
assert check_libgmt(load_libgmt(lib_fullnames=lib_fullnames)) is None
194199

195-
def test_brokenlib_brokenlib_workinglib(self, mock_ctypes):
200+
@pytest.mark.usefixtures("_mock_ctypes")
201+
def test_brokenlib_brokenlib_workinglib(self):
196202
"""
197203
Case 6: repeating broken libraries + working library.
198204
"""

pygmt/tests/test_clib_virtualfiles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_virtual_file_bad_direction():
110110

111111

112112
@pytest.mark.parametrize(
113-
"array_func,kind",
113+
("array_func", "kind"),
114114
[(np.array, "matrix"), (pd.DataFrame, "vector"), (xr.Dataset, "vector")],
115115
)
116116
def test_virtualfile_from_data_required_z_matrix(array_func, kind):

pygmt/tests/test_geopandas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_geopandas_info_geodataframe(gdf):
7171

7272

7373
@pytest.mark.parametrize(
74-
"geomtype,desired",
74+
("geomtype", "desired"),
7575
[
7676
("multipolygon", [0.0, 35.0, 0.0, 20.0]),
7777
("polygon", [20.0, 23.0, 10.0, 14.0]),

pygmt/tests/test_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_load_static_earth_relief():
3232

3333

3434
@pytest.mark.parametrize(
35-
"data,x,y",
35+
("data", "x", "y"),
3636
[
3737
(None, None, None),
3838
("data.txt", np.array([1, 2]), np.array([4, 5])),

pygmt/tests/test_meca.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ def test_meca_spec_ndarray_no_convention():
305305
"""
306306
Raise an exception if convention is not given for an ndarray input.
307307
"""
308+
fig = Figure()
309+
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
308310
with pytest.raises(GMTInvalidInput):
309-
fig = Figure()
310-
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
311311
fig.meca(spec=np.array([[-124, 48, 12.0, 330, 30, 90, 3]]), scale="1c")
312312

313313

@@ -316,16 +316,16 @@ def test_meca_spec_ndarray_mismatched_columns():
316316
Raise an exception if the ndarray input doesn't have the expected number of
317317
columns.
318318
"""
319+
fig = Figure()
320+
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
319321
with pytest.raises(GMTInvalidInput):
320-
fig = Figure()
321-
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
322322
fig.meca(
323323
spec=np.array([[-124, 48, 12.0, 330, 30, 90]]), convention="aki", scale="1c"
324324
)
325325

326+
fig = Figure()
327+
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
326328
with pytest.raises(GMTInvalidInput):
327-
fig = Figure()
328-
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
329329
fig.meca(
330330
spec=np.array([[-124, 48, 12.0, 330, 30, 90, 3, -124.5, 47.5, 30.0, 50.0]]),
331331
convention="aki",

pygmt/tests/test_x2sys_cross.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
@pytest.fixture(name="mock_x2sys_home")
19-
def fixture_mock_x2sys_home(monkeypatch):
19+
def _fixture_mock_x2sys_home(monkeypatch):
2020
"""
2121
Set the X2SYS_HOME environment variable to the current working directory
2222
for the test session.
@@ -34,7 +34,8 @@ def fixture_tracks():
3434
return [dataframe.query(expr="z > -20")] # reduce size of dataset
3535

3636

37-
def test_x2sys_cross_input_file_output_file(mock_x2sys_home):
37+
@pytest.mark.usefixtures("mock_x2sys_home")
38+
def test_x2sys_cross_input_file_output_file():
3839
"""
3940
Run x2sys_cross by passing in a filename, and output internal crossovers to
4041
an ASCII txt file.
@@ -52,7 +53,8 @@ def test_x2sys_cross_input_file_output_file(mock_x2sys_home):
5253
_ = pd.read_csv(outfile, sep="\t", header=2) # ensure ASCII text file loads ok
5354

5455

55-
def test_x2sys_cross_input_file_output_dataframe(mock_x2sys_home):
56+
@pytest.mark.usefixtures("mock_x2sys_home")
57+
def test_x2sys_cross_input_file_output_dataframe():
5658
"""
5759
Run x2sys_cross by passing in a filename, and output internal crossovers to
5860
a pandas.DataFrame.
@@ -69,7 +71,8 @@ def test_x2sys_cross_input_file_output_dataframe(mock_x2sys_home):
6971
assert columns[6:] == ["head_1", "head_2", "vel_1", "vel_2", "z_X", "z_M"]
7072

7173

72-
def test_x2sys_cross_input_dataframe_output_dataframe(mock_x2sys_home, tracks):
74+
@pytest.mark.usefixtures("mock_x2sys_home")
75+
def test_x2sys_cross_input_dataframe_output_dataframe(tracks):
7376
"""
7477
Run x2sys_cross by passing in one dataframe, and output internal crossovers
7578
to a pandas.DataFrame.
@@ -89,7 +92,8 @@ def test_x2sys_cross_input_dataframe_output_dataframe(mock_x2sys_home, tracks):
8992
assert output.dtypes["i_2"].type == np.object_
9093

9194

92-
def test_x2sys_cross_input_two_dataframes(mock_x2sys_home):
95+
@pytest.mark.usefixtures("mock_x2sys_home")
96+
def test_x2sys_cross_input_two_dataframes():
9397
"""
9498
Run x2sys_cross by passing in two pandas.DataFrame tables with a time
9599
column, and output external crossovers to a pandas.DataFrame.
@@ -125,7 +129,8 @@ def test_x2sys_cross_input_two_dataframes(mock_x2sys_home):
125129
assert output.dtypes["t_2"].type == np.datetime64
126130

127131

128-
def test_x2sys_cross_input_dataframe_with_nan(mock_x2sys_home, tracks):
132+
@pytest.mark.usefixtures("mock_x2sys_home")
133+
def test_x2sys_cross_input_dataframe_with_nan(tracks):
129134
"""
130135
Run x2sys_cross by passing in one dataframe with NaN values, and output
131136
internal crossovers to a pandas.DataFrame.
@@ -148,7 +153,8 @@ def test_x2sys_cross_input_dataframe_with_nan(mock_x2sys_home, tracks):
148153
assert output.dtypes["i_2"].type == np.object_
149154

150155

151-
def test_x2sys_cross_input_two_filenames(mock_x2sys_home):
156+
@pytest.mark.usefixtures("mock_x2sys_home")
157+
def test_x2sys_cross_input_two_filenames():
152158
"""
153159
Run x2sys_cross by passing in two filenames, and output external crossovers
154160
to a pandas.DataFrame.
@@ -186,7 +192,8 @@ def test_x2sys_cross_invalid_tracks_input_type(tracks):
186192
x2sys_cross(tracks=[invalid_tracks])
187193

188194

189-
def test_x2sys_cross_region_interpolation_numpoints(mock_x2sys_home):
195+
@pytest.mark.usefixtures("mock_x2sys_home")
196+
def test_x2sys_cross_region_interpolation_numpoints():
190197
"""
191198
Test that x2sys_cross's region (R), interpolation (l) and numpoints (W)
192199
arguments work.
@@ -210,7 +217,8 @@ def test_x2sys_cross_region_interpolation_numpoints(mock_x2sys_home):
210217
npt.assert_allclose(output.z_M.mean(), -2890.465813)
211218

212219

213-
def test_x2sys_cross_trackvalues(mock_x2sys_home):
220+
@pytest.mark.usefixtures("mock_x2sys_home")
221+
def test_x2sys_cross_trackvalues():
214222
"""
215223
Test that x2sys_cross's trackvalues (Z) argument work.
216224
"""

pygmt/tests/test_x2sys_init.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99

1010

1111
@pytest.fixture(name="mock_x2sys_home")
12-
def fixture_mock_x2sys_home(monkeypatch):
12+
def _fixture_mock_x2sys_home(monkeypatch):
1313
"""
1414
Set the X2SYS_HOME environment variable to the current working directory
1515
for the test session.
1616
"""
1717
monkeypatch.setenv("X2SYS_HOME", os.getcwd())
1818

1919

20-
def test_x2sys_init_region_spacing(mock_x2sys_home):
20+
@pytest.mark.usefixtures("mock_x2sys_home")
21+
def test_x2sys_init_region_spacing():
2122
"""
2223
Test that x2sys_init's region (R) and spacing (I) sequence arguments accept
2324
a list properly.
@@ -34,7 +35,8 @@ def test_x2sys_init_region_spacing(mock_x2sys_home):
3435
assert "-I5/5" in tail_line
3536

3637

37-
def test_x2sys_init_units_gap(mock_x2sys_home):
38+
@pytest.mark.usefixtures("mock_x2sys_home")
39+
def test_x2sys_init_units_gap():
3840
"""
3941
Test that x2sys_init's units (N) and gap (W) arguments accept a list
4042
properly.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ select = [
118118
"PGH", # pygrep-hooks
119119
"PIE", # flake8-pie
120120
"PL", # pylint
121+
"PT", # flake8-pytest-style
121122
"RET", # flake8-return
122123
"RSE", # flake8-raise
123124
"S", # flake8-bandit
@@ -133,6 +134,7 @@ ignore = [
133134
"E501", # Avoid enforcing line-length violations
134135
"ISC001", # Single-line-implicit-string-concatenation, conflict with formatter
135136
"PD901", # Allow using the generic variable name `df` for DataFrames
137+
"PT023", # Allow using pytest marker without parentheses
136138
"PLR2004", # Allow any magic values
137139
"RET504", # Allow variable assignment and return immediately for readability
138140
"S603", # Allow method calls that initiate a subprocess without a shell

0 commit comments

Comments
 (0)