Skip to content

Commit

Permalink
Merge pull request #19680 from BvB93/fromregex
Browse files Browse the repository at this point in the history
ENH: Allow `np.fromregex` to accept `os.PathLike` implementations
  • Loading branch information
charris committed Aug 16, 2021
2 parents 9e06c2c + ce11883 commit cde6992
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions 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__<os.PathLike>`
protocol, *e.g.* `pathlib.Path`.
6 changes: 5 additions & 1 deletion numpy/lib/npyio.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions numpy/lib/npyio.pyi
Expand Up @@ -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 = ...
Expand Down
6 changes: 4 additions & 2 deletions numpy/lib/tests/test_io.py
Expand Up @@ -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')

Expand Down
1 change: 0 additions & 1 deletion numpy/typing/tests/data/fail/npyio.py
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions numpy/typing/tests/data/reveal/npyio.py
Expand Up @@ -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_]]
Expand Down

0 comments on commit cde6992

Please sign in to comment.