From 9a649c3f0400861b5181e9d4087322662b01d280 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 16 Aug 2021 18:58:48 +0200 Subject: [PATCH 1/2] ENH: Allow `np.fromregex` to accept `os.PathLike` implementations --- numpy/lib/npyio.py | 6 +++++- numpy/lib/npyio.pyi | 4 ++-- numpy/lib/tests/test_io.py | 6 ++++-- numpy/typing/tests/data/fail/npyio.py | 1 - numpy/typing/tests/data/reveal/npyio.py | 1 + 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index a593af65e212..7a594f25bad5 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1484,8 +1484,11 @@ def fromregex(file, regexp, dtype, encoding=None): Parameters ---------- - file : str or file + file : path or file Filename or file object to read. + + .. versionchanged:: 1.22.0 + Now accepts `os.PathLike` implementations. regexp : str or regexp Regular expression used to parse the file. Groups in the regular expression correspond to fields in the dtype. @@ -1535,6 +1538,7 @@ def fromregex(file, regexp, dtype, encoding=None): """ own_fh = False if not hasattr(file, "read"): + file = os.fspath(file) file = np.lib._datasource.open(file, 'rt', encoding=encoding) own_fh = True diff --git a/numpy/lib/npyio.pyi b/numpy/lib/npyio.pyi index 264ceef14b7f..de6bc3ded8bb 100644 --- a/numpy/lib/npyio.pyi +++ b/numpy/lib/npyio.pyi @@ -182,14 +182,14 @@ def savetxt( @overload def fromregex( - file: str | IO[Any], + file: str | os.PathLike[str] | IO[Any], regexp: str | bytes | Pattern[Any], dtype: _DTypeLike[_SCT], encoding: None | str = ... ) -> NDArray[_SCT]: ... @overload def fromregex( - file: str | IO[Any], + file: str | os.PathLike[str] | IO[Any], regexp: str | bytes | Pattern[Any], dtype: DTypeLike, encoding: None | str = ... diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 02a9789a7db8..11f2b7d4d420 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -1229,9 +1229,11 @@ def test_record_3(self): a = np.array([(1312,), (1534,), (4444,)], dtype=dt) assert_array_equal(x, a) - def test_record_unicode(self): + @pytest.mark.parametrize("path_type", [str, Path]) + def test_record_unicode(self, path_type): utf8 = b'\xcf\x96' - with temppath() as path: + with temppath() as str_path: + path = path_type(str_path) with open(path, 'wb') as f: f.write(b'1.312 foo' + utf8 + b' \n1.534 bar\n4.444 qux') diff --git a/numpy/typing/tests/data/fail/npyio.py b/numpy/typing/tests/data/fail/npyio.py index 89c511c1cb57..8edabf2b3dc2 100644 --- a/numpy/typing/tests/data/fail/npyio.py +++ b/numpy/typing/tests/data/fail/npyio.py @@ -24,7 +24,6 @@ np.loadtxt(bytes_path) # E: No overload variant np.fromregex(bytes_path, ".", np.int64) # E: No overload variant -np.fromregex(pathlib_path, ".", np.int64) # E: No overload variant np.recfromtxt(bytes_path) # E: No overload variant diff --git a/numpy/typing/tests/data/reveal/npyio.py b/numpy/typing/tests/data/reveal/npyio.py index 36c0c540bf26..05005eb1c1c5 100644 --- a/numpy/typing/tests/data/reveal/npyio.py +++ b/numpy/typing/tests/data/reveal/npyio.py @@ -56,6 +56,7 @@ reveal_type(np.fromregex(bytes_file, "test", np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.fromregex(str_file, b"test", dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] reveal_type(np.fromregex(str_path, re.compile("test"), dtype=np.str_, encoding="utf8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] +reveal_type(np.fromregex(pathlib_path, "test", np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.genfromtxt(bytes_file)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.genfromtxt(pathlib_path, dtype=np.str_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] From ce1188384ad6b2b0e3f2e673e89ff2b84262e038 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 16 Aug 2021 19:45:27 +0200 Subject: [PATCH 2/2] DOC: Add a release note --- doc/release/upcoming_changes/19680.improvement.rst | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 doc/release/upcoming_changes/19680.improvement.rst diff --git a/doc/release/upcoming_changes/19680.improvement.rst b/doc/release/upcoming_changes/19680.improvement.rst new file mode 100644 index 000000000000..1a2a3496b835 --- /dev/null +++ b/doc/release/upcoming_changes/19680.improvement.rst @@ -0,0 +1,5 @@ +`numpy.fromregex` now accepts ``os.PathLike`` implementations +------------------------------------------------------------- + +`numpy.fromregex` now accepts objects implementing the `__fspath__` +protocol, *e.g.* `pathlib.Path`.