Skip to content

Commit a1a31f1

Browse files
test: Use tmp_path pytest fixture over tmpdir (#2384)
* Use `tmp_path` over `tmpdir`. * Use pathlib.Path functions now that paths and not files are being used. - Remove use of `.strpath`. - Update from `.join` to `.joinpath`. - For `json.loads` update from `.read` to `.read_text`. - For `json.load` update to use `.open`. - Update from `.write` to `.write_text` for patches. - Update from `.mkdir` to creating the path and then calling `.mkdir`.
1 parent f78442a commit a1a31f1

File tree

6 files changed

+223
-216
lines changed

6 files changed

+223
-216
lines changed

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,6 @@ def datadir(tmp_path, request):
167167
dir_util.copy_tree(test_dir, str(tmp_path))
168168
# shutil is nicer, but doesn't work: https://bugs.python.org/issue20849
169169
# Once pyhf is Python 3.8+ only then the below can be used.
170-
# shutil.copytree(test_dir, tmpdir)
170+
# shutil.copytree(test_dir, tmp_path)
171171

172172
return tmp_path
Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import tarfile
22
import zipfile
3-
from pathlib import Path
43
from shutil import rmtree
54

65
import pytest
@@ -10,70 +9,69 @@
109

1110

1211
@pytest.fixture(scope="function")
13-
def tarfile_path(tmpdir):
14-
with open(
15-
tmpdir.join("test_file.txt").strpath, "w", encoding="utf-8"
16-
) as write_file:
12+
def tarfile_path(tmp_path):
13+
with open(tmp_path.joinpath("test_file.txt"), "w", encoding="utf-8") as write_file:
1714
write_file.write("test file")
1815
with tarfile.open(
19-
tmpdir.join("test_tar.tar.gz").strpath, mode="w:gz", encoding="utf-8"
16+
tmp_path.joinpath("test_tar.tar.gz"), mode="w:gz", encoding="utf-8"
2017
) as archive:
21-
archive.add(tmpdir.join("test_file.txt").strpath)
22-
return Path(tmpdir.join("test_tar.tar.gz").strpath)
18+
archive.add(tmp_path.joinpath("test_file.txt"))
19+
return tmp_path.joinpath("test_tar.tar.gz")
2320

2421

2522
@pytest.fixture(scope="function")
26-
def tarfile_uncompressed_path(tmpdir):
27-
with open(
28-
tmpdir.join("test_file.txt").strpath, "w", encoding="utf-8"
29-
) as write_file:
23+
def tarfile_uncompressed_path(tmp_path):
24+
with open(tmp_path.joinpath("test_file.txt"), "w", encoding="utf-8") as write_file:
3025
write_file.write("test file")
3126
with tarfile.open(
32-
tmpdir.join("test_tar.tar").strpath, mode="w", encoding="utf-8"
27+
tmp_path.joinpath("test_tar.tar"), mode="w", encoding="utf-8"
3328
) as archive:
34-
archive.add(tmpdir.join("test_file.txt").strpath)
35-
return Path(tmpdir.join("test_tar.tar").strpath)
29+
archive.add(tmp_path.joinpath("test_file.txt"))
30+
return tmp_path.joinpath("test_tar.tar")
3631

3732

3833
@pytest.fixture(scope="function")
39-
def zipfile_path(tmpdir):
40-
with open(
41-
tmpdir.join("test_file.txt").strpath, "w", encoding="utf-8"
42-
) as write_file:
34+
def zipfile_path(tmp_path):
35+
with open(tmp_path.joinpath("test_file.txt"), "w", encoding="utf-8") as write_file:
4336
write_file.write("test file")
44-
with zipfile.ZipFile(tmpdir.join("test_zip.zip").strpath, "w") as archive:
45-
archive.write(tmpdir.join("test_file.txt").strpath)
46-
return Path(tmpdir.join("test_zip.zip").strpath)
37+
with zipfile.ZipFile(tmp_path.joinpath("test_zip.zip"), "w") as archive:
38+
archive.write(tmp_path.joinpath("test_file.txt"))
39+
return tmp_path.joinpath("test_zip.zip")
4740

4841

49-
def test_download_untrusted_archive_host(tmpdir, requests_mock):
42+
def test_download_untrusted_archive_host(tmp_path, requests_mock):
5043
archive_url = "https://www.pyhfthisdoesnotexist.org"
5144
requests_mock.get(archive_url)
5245

5346
with pytest.raises(InvalidArchiveHost):
54-
download(archive_url, tmpdir.join("likelihoods").strpath)
47+
download(archive_url, tmp_path.joinpath("likelihoods"))
5548

5649

57-
def test_download_invalid_archive(tmpdir, requests_mock):
50+
def test_download_invalid_archive(tmp_path, requests_mock):
5851
archive_url = "https://www.hepdata.net/record/resource/1408476?view=true"
5952
requests_mock.get(archive_url, status_code=404)
6053

6154
with pytest.raises(InvalidArchive):
62-
download(archive_url, tmpdir.join("likelihoods").strpath)
55+
download(archive_url, tmp_path.joinpath("likelihoods"))
6356

6457

65-
def test_download_compress(tmpdir, requests_mock):
58+
def test_download_compress(tmp_path, requests_mock):
6659
archive_url = "https://www.hepdata.net/record/resource/1408476?view=true"
6760
requests_mock.get(archive_url)
6861

69-
download(archive_url, tmpdir.join("likelihoods").strpath, compress=True)
62+
download(archive_url, tmp_path.joinpath("likelihoods"), compress=True)
7063

7164

7265
def test_download_archive_type(
73-
tmpdir, mocker, requests_mock, tarfile_path, tarfile_uncompressed_path, zipfile_path
66+
tmp_path,
67+
mocker,
68+
requests_mock,
69+
tarfile_path,
70+
tarfile_uncompressed_path,
71+
zipfile_path,
7472
):
7573
archive_url = "https://www.hepdata.net/record/resource/1408476?view=true"
76-
output_directory = tmpdir.join("likelihoods").strpath
74+
output_directory = tmp_path.joinpath("likelihoods")
7775
# Give BytesIO a tarfile
7876
requests_mock.get(archive_url, content=open(tarfile_path, "rb").read())
7977
download(archive_url, output_directory)
@@ -86,7 +84,7 @@ def test_download_archive_type(
8684
requests_mock.get(archive_url, content=open(zipfile_path, "rb").read())
8785
# Run without and with existing output_directory to cover both
8886
# cases of the shutil.rmtree logic
89-
rmtree(Path(output_directory))
87+
rmtree(output_directory)
9088
download(archive_url, output_directory) # without
9189
download(archive_url, output_directory) # with
9290

@@ -97,13 +95,13 @@ def test_download_archive_type(
9795
download(archive_url, output_directory)
9896

9997

100-
def test_download_archive_force(tmpdir, requests_mock, tarfile_path):
98+
def test_download_archive_force(tmp_path, requests_mock, tarfile_path):
10199
archive_url = "https://www.cern.ch/record/resource/123456789"
102100
requests_mock.get(
103101
archive_url, content=open(tarfile_path, "rb").read(), status_code=200
104102
)
105103

106104
with pytest.raises(InvalidArchiveHost):
107-
download(archive_url, tmpdir.join("likelihoods").strpath, force=False)
105+
download(archive_url, tmp_path.joinpath("likelihoods"), force=False)
108106

109-
download(archive_url, tmpdir.join("likelihoods").strpath, force=True)
107+
download(archive_url, tmp_path.joinpath("likelihoods"), force=True)

tests/test_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import shlex
22

33

4-
def test_2bin_1channel(tmpdir, script_runner):
4+
def test_2bin_1channel(tmp_path, script_runner):
55
command = f"pyhf inspect {'docs/examples/json/2-bin_1-channel.json':s}"
66
ret = script_runner.run(shlex.split(command))
77
assert ret.success

tests/test_infer.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def check_uniform_type(in_list):
2323
)
2424

2525

26-
def test_toms748_scan(tmpdir, hypotest_args):
26+
def test_toms748_scan(tmp_path, hypotest_args):
2727
"""
2828
Test the upper limit toms748 scan returns the correct structure and values
2929
"""
@@ -166,7 +166,7 @@ def test_upper_limit_with_kwargs(hypotest_args):
166166
)
167167

168168

169-
def test_mle_fit_default(tmpdir, hypotest_args):
169+
def test_mle_fit_default(tmp_path, hypotest_args):
170170
"""
171171
Check that the default return structure of pyhf.infer.mle.fit is as expected
172172
"""
@@ -180,7 +180,7 @@ def test_mle_fit_default(tmpdir, hypotest_args):
180180
assert pyhf.tensorlib.shape(result) == (model.config.npars,)
181181

182182

183-
def test_mle_fit_return_fitted_val(tmpdir, hypotest_args):
183+
def test_mle_fit_return_fitted_val(tmp_path, hypotest_args):
184184
"""
185185
Check that the return structure of pyhf.infer.mle.fit with the
186186
return_fitted_val keyword arg is as expected
@@ -196,7 +196,7 @@ def test_mle_fit_return_fitted_val(tmpdir, hypotest_args):
196196
assert pyhf.tensorlib.shape(result[1]) == ()
197197

198198

199-
def test_hypotest_default(tmpdir, hypotest_args):
199+
def test_hypotest_default(tmp_path, hypotest_args):
200200
"""
201201
Check that the default return structure of pyhf.infer.hypotest is as expected
202202
"""
@@ -209,7 +209,7 @@ def test_hypotest_default(tmpdir, hypotest_args):
209209
assert isinstance(result, type(tb.astensor(result)))
210210

211211

212-
def test_hypotest_poi_outofbounds(tmpdir, hypotest_args):
212+
def test_hypotest_poi_outofbounds(tmp_path, hypotest_args):
213213
"""
214214
Check that the fit errors for POI outside of parameter bounds
215215
"""
@@ -226,7 +226,7 @@ def test_hypotest_poi_outofbounds(tmpdir, hypotest_args):
226226

227227

228228
@pytest.mark.parametrize('test_stat', ['q0', 'q', 'qtilde'])
229-
def test_hypotest_return_tail_probs(tmpdir, hypotest_args, test_stat):
229+
def test_hypotest_return_tail_probs(tmp_path, hypotest_args, test_stat):
230230
"""
231231
Check that the return structure of pyhf.infer.hypotest with the
232232
return_tail_probs keyword arg is as expected
@@ -243,7 +243,7 @@ def test_hypotest_return_tail_probs(tmpdir, hypotest_args, test_stat):
243243

244244

245245
@pytest.mark.parametrize('test_stat', ['q0', 'q', 'qtilde'])
246-
def test_hypotest_return_expected(tmpdir, hypotest_args, test_stat):
246+
def test_hypotest_return_expected(tmp_path, hypotest_args, test_stat):
247247
"""
248248
Check that the return structure of pyhf.infer.hypotest with the
249249
addition of the return_expected keyword arg is as expected
@@ -265,7 +265,7 @@ def test_hypotest_return_expected(tmpdir, hypotest_args, test_stat):
265265

266266

267267
@pytest.mark.parametrize('test_stat', ['q0', 'q', 'qtilde'])
268-
def test_hypotest_return_expected_set(tmpdir, hypotest_args, test_stat):
268+
def test_hypotest_return_expected_set(tmp_path, hypotest_args, test_stat):
269269
"""
270270
Check that the return structure of pyhf.infer.hypotest with the
271271
addition of the return_expected_set keyword arg is as expected
@@ -300,7 +300,7 @@ def test_hypotest_return_expected_set(tmpdir, hypotest_args, test_stat):
300300
@pytest.mark.parametrize('return_expected', [True, False])
301301
@pytest.mark.parametrize('return_expected_set', [True, False])
302302
def test_hypotest_return_calculator(
303-
tmpdir,
303+
tmp_path,
304304
hypotest_args,
305305
calctype,
306306
kwargs,
@@ -491,7 +491,7 @@ def test_significance_to_pvalue_roundtrip(backend):
491491
assert np.allclose(sigma, back_to_sigma, atol=0, rtol=rtol)
492492

493493

494-
def test_emperical_distribution(tmpdir, hypotest_args):
494+
def test_emperical_distribution(tmp_path, hypotest_args):
495495
"""
496496
Check that the empirical distribution of the test statistic gives
497497
expected results
@@ -537,7 +537,7 @@ def test_emperical_distribution(tmpdir, hypotest_args):
537537
)
538538

539539

540-
def test_toy_calculator(tmpdir, hypotest_args):
540+
def test_toy_calculator(tmp_path, hypotest_args):
541541
"""
542542
Check that the toy calculator is performing as expected
543543
"""

tests/test_notebooks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212

1313
@pytest.fixture()
14-
def common_kwargs(tmpdir):
15-
outputnb = tmpdir.join('output.ipynb')
14+
def common_kwargs(tmp_path):
15+
outputnb = tmp_path.joinpath('output.ipynb')
1616
return {
1717
'output_path': str(outputnb),
1818
'kernel_name': f'python{sys.version_info.major}',

0 commit comments

Comments
 (0)