From 4bbc20a3e5f5c360b4e01566bd4f68ae40fe562e Mon Sep 17 00:00:00 2001 From: ganesh-k13 Date: Sat, 27 May 2023 16:53:57 +0530 Subject: [PATCH 1/2] DOC: Added `pathlib.Path` where applicable --- numpy/lib/_datasource.py | 16 ++++++++-------- numpy/lib/npyio.py | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/numpy/lib/_datasource.py b/numpy/lib/_datasource.py index 613733fa5167..af93c1ae6b32 100644 --- a/numpy/lib/_datasource.py +++ b/numpy/lib/_datasource.py @@ -161,7 +161,7 @@ def open(path, mode='r', destpath=os.curdir, encoding=None, newline=None): Parameters ---------- - path : str + path : str or pathlib.Path Local file path or URL to open. mode : str, optional Mode to open `path`. Mode 'r' for reading, 'w' for writing, 'a' to @@ -382,7 +382,7 @@ def abspath(self, path): Parameters ---------- - path : str + path : str or pathlib.Path Can be a local file or a remote URL. Returns @@ -442,7 +442,7 @@ def exists(self, path): Parameters ---------- - path : str + path : str or pathlib.Path Can be a local file or a remote URL. Returns @@ -493,7 +493,7 @@ def open(self, path, mode='r', encoding=None, newline=None): Parameters ---------- - path : str + path : str or pathlib.Path Local file path or URL to open. mode : {'r', 'w', 'a'}, optional Mode to open `path`. Mode 'r' for reading, 'w' for writing, @@ -604,7 +604,7 @@ def abspath(self, path): Parameters ---------- - path : str + path : str or pathlib.Path Can be a local file or a remote URL. This may, but does not have to, include the `baseurl` with which the `Repository` was initialized. @@ -631,7 +631,7 @@ def exists(self, path): Parameters ---------- - path : str + path : str or pathlib.Path Can be a local file or a remote URL. This may, but does not have to, include the `baseurl` with which the `Repository` was initialized. @@ -660,7 +660,7 @@ def open(self, path, mode='r', encoding=None, newline=None): Parameters ---------- - path : str + path : str or pathlib.Path Local file path or URL to open. This may, but does not have to, include the `baseurl` with which the `Repository` was initialized. @@ -689,7 +689,7 @@ def listdir(self): Returns ------- - files : list of str + files : list of str or pathlib.Path List of file names (not containing a directory part). Notes diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 339b1dc62113..1e8d8a71232b 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -148,7 +148,7 @@ class NpzFile(Mapping): Parameters ---------- - fid : file or str + fid : file, str, or pathlib.Path The zipped archive to open. This is either a file-like object or a string containing the path to the archive. own_fid : bool, optional @@ -564,7 +564,7 @@ def savez(file, *args, **kwds): Parameters ---------- - file : str or file + file : file, str, or pathlib.Path Either the filename (string) or an open file (file-like object) where the data will be saved. If file is a string or a Path, the ``.npz`` extension will be appended to the filename if it is not @@ -657,7 +657,7 @@ def savez_compressed(file, *args, **kwds): Parameters ---------- - file : str or file + file : file, str, or pathlib.Path Either the filename (string) or an open file (file-like object) where the data will be saved. If file is a string or a Path, the ``.npz`` extension will be appended to the filename if it is not @@ -824,7 +824,7 @@ def _read(fname, *, delimiter=',', comment='#', quote='"', Parameters ---------- - fname : str or file object + fname : file, str, or pathlib.Path The filename or the file to be read. delimiter : str, optional Field delimiter of the fields in line of the file. @@ -1395,7 +1395,7 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', Parameters ---------- - fname : filename or file handle + fname : filename, file handle or pathlib.Path If the filename ends in ``.gz``, the file is automatically saved in compressed gzip format. `loadtxt` understands gzipped files transparently. @@ -1646,7 +1646,7 @@ def fromregex(file, regexp, dtype, encoding=None): Parameters ---------- - file : path or file + file : file, str, or pathlib.Path Filename or file object to read. .. versionchanged:: 1.22.0 From 74074231b681a322341143fa90c44b8d96f719a2 Mon Sep 17 00:00:00 2001 From: ganesh-k13 Date: Sat, 27 May 2023 19:19:31 +0530 Subject: [PATCH 2/2] TST: Added `pathlib.Path` tests for io functions --- numpy/lib/tests/test_io.py | 44 +++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index c1032df8e1d3..05cd0fbeb649 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -471,11 +471,12 @@ def test_header_footer(self): assert_equal(c.read(), asbytes('1 2\n3 4\n' + commentstr + test_header_footer + '\n')) - def test_file_roundtrip(self): + @pytest.mark.parametrize("filename_type", [Path, str]) + def test_file_roundtrip(self, filename_type): with temppath() as name: a = np.array([(1, 2), (3, 4)]) - np.savetxt(name, a) - b = np.loadtxt(name) + np.savetxt(filename_type(name), a) + b = np.loadtxt(filename_type(name)) assert_array_equal(a, b) def test_complex_arrays(self): @@ -2567,10 +2568,10 @@ def test_save_load_memmap(self): break_cycles() @pytest.mark.xfail(IS_WASM, reason="memmap doesn't work correctly") - def test_save_load_memmap_readwrite(self): - # Test that pathlib.Path instances can be written mem-mapped. + @pytest.mark.parametrize("filename_type", [Path, str]) + def test_save_load_memmap_readwrite(self, filename_type): with temppath(suffix='.npy') as path: - path = Path(path) + path = filename_type(path) a = np.array([[1, 2], [3, 4]], int) np.save(path, a) b = np.load(path, mmap_mode='r+') @@ -2583,35 +2584,37 @@ def test_save_load_memmap_readwrite(self): data = np.load(path) assert_array_equal(data, a) - def test_savez_load(self): - # Test that pathlib.Path instances can be used with savez. + @pytest.mark.parametrize("filename_type", [Path, str]) + def test_savez_load(self, filename_type): with temppath(suffix='.npz') as path: - path = Path(path) + path = filename_type(path) np.savez(path, lab='place holder') with np.load(path) as data: assert_array_equal(data['lab'], 'place holder') - def test_savez_compressed_load(self): - # Test that pathlib.Path instances can be used with savez. + @pytest.mark.parametrize("filename_type", [Path, str]) + def test_savez_compressed_load(self, filename_type): with temppath(suffix='.npz') as path: - path = Path(path) + path = filename_type(path) np.savez_compressed(path, lab='place holder') data = np.load(path) assert_array_equal(data['lab'], 'place holder') data.close() - def test_genfromtxt(self): + @pytest.mark.parametrize("filename_type", [Path, str]) + def test_genfromtxt(self, filename_type): with temppath(suffix='.txt') as path: - path = Path(path) + path = filename_type(path) a = np.array([(1, 2), (3, 4)]) np.savetxt(path, a) data = np.genfromtxt(path) assert_array_equal(a, data) - def test_recfromtxt(self): + @pytest.mark.parametrize("filename_type", [Path, str]) + def test_recfromtxt(self, filename_type): with temppath(suffix='.txt') as path: - path = Path(path) - with path.open('w') as f: + path = filename_type(path) + with open(path, 'w') as f: f.write('A,B\n0,1\n2,3') kwargs = dict(delimiter=",", missing_values="N/A", names=True) @@ -2621,10 +2624,11 @@ def test_recfromtxt(self): assert_(isinstance(test, np.recarray)) assert_equal(test, control) - def test_recfromcsv(self): + @pytest.mark.parametrize("filename_type", [Path, str]) + def test_recfromcsv(self, filename_type): with temppath(suffix='.txt') as path: - path = Path(path) - with path.open('w') as f: + path = filename_type(path) + with open(path, 'w') as f: f.write('A,B\n0,1\n2,3') kwargs = dict(missing_values="N/A", names=True, case_sensitive=True)