Skip to content

Commit 7669b86

Browse files
authored
Merge pull request numpy#23822 from ganesh-k13/npyio_pathlib
DOC: Added `pathlib.Path` where applicable
2 parents ee91426 + 7407423 commit 7669b86

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

numpy/lib/_datasource.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def open(path, mode='r', destpath=os.curdir, encoding=None, newline=None):
161161
162162
Parameters
163163
----------
164-
path : str
164+
path : str or pathlib.Path
165165
Local file path or URL to open.
166166
mode : str, optional
167167
Mode to open `path`. Mode 'r' for reading, 'w' for writing, 'a' to
@@ -382,7 +382,7 @@ def abspath(self, path):
382382
383383
Parameters
384384
----------
385-
path : str
385+
path : str or pathlib.Path
386386
Can be a local file or a remote URL.
387387
388388
Returns
@@ -442,7 +442,7 @@ def exists(self, path):
442442
443443
Parameters
444444
----------
445-
path : str
445+
path : str or pathlib.Path
446446
Can be a local file or a remote URL.
447447
448448
Returns
@@ -493,7 +493,7 @@ def open(self, path, mode='r', encoding=None, newline=None):
493493
494494
Parameters
495495
----------
496-
path : str
496+
path : str or pathlib.Path
497497
Local file path or URL to open.
498498
mode : {'r', 'w', 'a'}, optional
499499
Mode to open `path`. Mode 'r' for reading, 'w' for writing,
@@ -604,7 +604,7 @@ def abspath(self, path):
604604
605605
Parameters
606606
----------
607-
path : str
607+
path : str or pathlib.Path
608608
Can be a local file or a remote URL. This may, but does not
609609
have to, include the `baseurl` with which the `Repository` was
610610
initialized.
@@ -631,7 +631,7 @@ def exists(self, path):
631631
632632
Parameters
633633
----------
634-
path : str
634+
path : str or pathlib.Path
635635
Can be a local file or a remote URL. This may, but does not
636636
have to, include the `baseurl` with which the `Repository` was
637637
initialized.
@@ -660,7 +660,7 @@ def open(self, path, mode='r', encoding=None, newline=None):
660660
661661
Parameters
662662
----------
663-
path : str
663+
path : str or pathlib.Path
664664
Local file path or URL to open. This may, but does not have to,
665665
include the `baseurl` with which the `Repository` was
666666
initialized.
@@ -689,7 +689,7 @@ def listdir(self):
689689
690690
Returns
691691
-------
692-
files : list of str
692+
files : list of str or pathlib.Path
693693
List of file names (not containing a directory part).
694694
695695
Notes

numpy/lib/npyio.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class NpzFile(Mapping):
148148
149149
Parameters
150150
----------
151-
fid : file or str
151+
fid : file, str, or pathlib.Path
152152
The zipped archive to open. This is either a file-like object
153153
or a string containing the path to the archive.
154154
own_fid : bool, optional
@@ -564,7 +564,7 @@ def savez(file, *args, **kwds):
564564
565565
Parameters
566566
----------
567-
file : str or file
567+
file : file, str, or pathlib.Path
568568
Either the filename (string) or an open file (file-like object)
569569
where the data will be saved. If file is a string or a Path, the
570570
``.npz`` extension will be appended to the filename if it is not
@@ -657,7 +657,7 @@ def savez_compressed(file, *args, **kwds):
657657
658658
Parameters
659659
----------
660-
file : str or file
660+
file : file, str, or pathlib.Path
661661
Either the filename (string) or an open file (file-like object)
662662
where the data will be saved. If file is a string or a Path, the
663663
``.npz`` extension will be appended to the filename if it is not
@@ -824,7 +824,7 @@ def _read(fname, *, delimiter=',', comment='#', quote='"',
824824
825825
Parameters
826826
----------
827-
fname : str or file object
827+
fname : file, str, or pathlib.Path
828828
The filename or the file to be read.
829829
delimiter : str, optional
830830
Field delimiter of the fields in line of the file.
@@ -1395,7 +1395,7 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='',
13951395
13961396
Parameters
13971397
----------
1398-
fname : filename or file handle
1398+
fname : filename, file handle or pathlib.Path
13991399
If the filename ends in ``.gz``, the file is automatically saved in
14001400
compressed gzip format. `loadtxt` understands gzipped files
14011401
transparently.
@@ -1646,7 +1646,7 @@ def fromregex(file, regexp, dtype, encoding=None):
16461646
16471647
Parameters
16481648
----------
1649-
file : path or file
1649+
file : file, str, or pathlib.Path
16501650
Filename or file object to read.
16511651
16521652
.. versionchanged:: 1.22.0

numpy/lib/tests/test_io.py

+24-20
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,12 @@ def test_header_footer(self):
471471
assert_equal(c.read(),
472472
asbytes('1 2\n3 4\n' + commentstr + test_header_footer + '\n'))
473473

474-
def test_file_roundtrip(self):
474+
@pytest.mark.parametrize("filename_type", [Path, str])
475+
def test_file_roundtrip(self, filename_type):
475476
with temppath() as name:
476477
a = np.array([(1, 2), (3, 4)])
477-
np.savetxt(name, a)
478-
b = np.loadtxt(name)
478+
np.savetxt(filename_type(name), a)
479+
b = np.loadtxt(filename_type(name))
479480
assert_array_equal(a, b)
480481

481482
def test_complex_arrays(self):
@@ -2567,10 +2568,10 @@ def test_save_load_memmap(self):
25672568
break_cycles()
25682569

25692570
@pytest.mark.xfail(IS_WASM, reason="memmap doesn't work correctly")
2570-
def test_save_load_memmap_readwrite(self):
2571-
# Test that pathlib.Path instances can be written mem-mapped.
2571+
@pytest.mark.parametrize("filename_type", [Path, str])
2572+
def test_save_load_memmap_readwrite(self, filename_type):
25722573
with temppath(suffix='.npy') as path:
2573-
path = Path(path)
2574+
path = filename_type(path)
25742575
a = np.array([[1, 2], [3, 4]], int)
25752576
np.save(path, a)
25762577
b = np.load(path, mmap_mode='r+')
@@ -2583,35 +2584,37 @@ def test_save_load_memmap_readwrite(self):
25832584
data = np.load(path)
25842585
assert_array_equal(data, a)
25852586

2586-
def test_savez_load(self):
2587-
# Test that pathlib.Path instances can be used with savez.
2587+
@pytest.mark.parametrize("filename_type", [Path, str])
2588+
def test_savez_load(self, filename_type):
25882589
with temppath(suffix='.npz') as path:
2589-
path = Path(path)
2590+
path = filename_type(path)
25902591
np.savez(path, lab='place holder')
25912592
with np.load(path) as data:
25922593
assert_array_equal(data['lab'], 'place holder')
25932594

2594-
def test_savez_compressed_load(self):
2595-
# Test that pathlib.Path instances can be used with savez.
2595+
@pytest.mark.parametrize("filename_type", [Path, str])
2596+
def test_savez_compressed_load(self, filename_type):
25962597
with temppath(suffix='.npz') as path:
2597-
path = Path(path)
2598+
path = filename_type(path)
25982599
np.savez_compressed(path, lab='place holder')
25992600
data = np.load(path)
26002601
assert_array_equal(data['lab'], 'place holder')
26012602
data.close()
26022603

2603-
def test_genfromtxt(self):
2604+
@pytest.mark.parametrize("filename_type", [Path, str])
2605+
def test_genfromtxt(self, filename_type):
26042606
with temppath(suffix='.txt') as path:
2605-
path = Path(path)
2607+
path = filename_type(path)
26062608
a = np.array([(1, 2), (3, 4)])
26072609
np.savetxt(path, a)
26082610
data = np.genfromtxt(path)
26092611
assert_array_equal(a, data)
26102612

2611-
def test_recfromtxt(self):
2613+
@pytest.mark.parametrize("filename_type", [Path, str])
2614+
def test_recfromtxt(self, filename_type):
26122615
with temppath(suffix='.txt') as path:
2613-
path = Path(path)
2614-
with path.open('w') as f:
2616+
path = filename_type(path)
2617+
with open(path, 'w') as f:
26152618
f.write('A,B\n0,1\n2,3')
26162619

26172620
kwargs = dict(delimiter=",", missing_values="N/A", names=True)
@@ -2621,10 +2624,11 @@ def test_recfromtxt(self):
26212624
assert_(isinstance(test, np.recarray))
26222625
assert_equal(test, control)
26232626

2624-
def test_recfromcsv(self):
2627+
@pytest.mark.parametrize("filename_type", [Path, str])
2628+
def test_recfromcsv(self, filename_type):
26252629
with temppath(suffix='.txt') as path:
2626-
path = Path(path)
2627-
with path.open('w') as f:
2630+
path = filename_type(path)
2631+
with open(path, 'w') as f:
26282632
f.write('A,B\n0,1\n2,3')
26292633

26302634
kwargs = dict(missing_values="N/A", names=True, case_sensitive=True)

0 commit comments

Comments
 (0)