Skip to content

Commit 7b93333

Browse files
mwtoewsIllviljanmax-sixty
authored
Check for path-like objects rather than Path type, use os.fspath (#5879)
* Check for path-like objects rather than Path type, use os.fspath * Add whats-new entry Co-authored-by: Illviljan <[email protected]> Co-authored-by: Maximilian Roos <[email protected]>
1 parent df76461 commit 7b93333

File tree

7 files changed

+24
-25
lines changed

7 files changed

+24
-25
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ Bug fixes
8080
By `Jimmy Westling <https://github.com/illviljan>`_.
8181
- Numbers are properly formatted in a plot's title (:issue:`5788`, :pull:`5789`).
8282
By `Maxime Liquet <https://github.com/maximlt>`_.
83+
- With backends, check for path-like objects rather than ``pathlib.Path``
84+
type, use ``os.fspath`` (:pull:`5879`).
85+
By `Mike Taves <https://github.com/mwtoews>`_.
8386
- ``open_mfdataset()`` now accepts a single ``pathlib.Path`` object (:issue: `5881`).
8487
By `Panos Mavrogiorgos <https://github.com/pmav99>`_.
8588

xarray/backends/api.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from glob import glob
33
from io import BytesIO
44
from numbers import Number
5-
from pathlib import Path
65
from typing import (
76
TYPE_CHECKING,
87
Callable,
@@ -808,7 +807,7 @@ def open_mfdataset(
808807
- "override": if indexes are of same size, rewrite indexes to be
809808
those of the first object with that dimension. Indexes for the same
810809
dimension must have the same size in all objects.
811-
attrs_file : str or pathlib.Path, optional
810+
attrs_file : str or path-like, optional
812811
Path of the file used to read global attributes from.
813812
By default global attributes are read from the first file provided,
814813
with wildcard matches sorted by filename.
@@ -868,7 +867,7 @@ def open_mfdataset(
868867
elif isinstance(paths, os.PathLike):
869868
paths = [os.fspath(paths)]
870869
else:
871-
paths = [str(p) if isinstance(p, Path) else p for p in paths]
870+
paths = [os.fspath(p) if isinstance(p, os.PathLike) else p for p in paths]
872871

873872
if not paths:
874873
raise OSError("no files to open")
@@ -960,8 +959,8 @@ def multi_file_closer():
960959

961960
# read global attributes from the attrs_file or from the first dataset
962961
if attrs_file is not None:
963-
if isinstance(attrs_file, Path):
964-
attrs_file = str(attrs_file)
962+
if isinstance(attrs_file, os.PathLike):
963+
attrs_file = os.fspath(attrs_file)
965964
combined.attrs = datasets[paths.index(attrs_file)].attrs
966965

967966
return combined
@@ -994,8 +993,8 @@ def to_netcdf(
994993
995994
The ``multifile`` argument is only for the private use of save_mfdataset.
996995
"""
997-
if isinstance(path_or_file, Path):
998-
path_or_file = str(path_or_file)
996+
if isinstance(path_or_file, os.PathLike):
997+
path_or_file = os.fspath(path_or_file)
999998

1000999
if encoding is None:
10011000
encoding = {}
@@ -1136,7 +1135,7 @@ def save_mfdataset(
11361135
----------
11371136
datasets : list of Dataset
11381137
List of datasets to save.
1139-
paths : list of str or list of Path
1138+
paths : list of str or list of path-like objects
11401139
List of paths to which to save each corresponding dataset.
11411140
mode : {"w", "a"}, optional
11421141
Write ("w") or append ("a") mode. If mode="w", any existing file at
@@ -1304,7 +1303,7 @@ def check_dtype(var):
13041303

13051304
def to_zarr(
13061305
dataset: Dataset,
1307-
store: Union[MutableMapping, str, Path] = None,
1306+
store: Union[MutableMapping, str, os.PathLike] = None,
13081307
chunk_store=None,
13091308
mode: str = None,
13101309
synchronizer=None,
@@ -1328,7 +1327,7 @@ def to_zarr(
13281327
if v.size == 0:
13291328
v.load()
13301329

1331-
# expand str and Path arguments
1330+
# expand str and path-like arguments
13321331
store = _normalize_path(store)
13331332
chunk_store = _normalize_path(chunk_store)
13341333

xarray/backends/common.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import logging
2-
import os.path
2+
import os
33
import time
44
import traceback
5-
from pathlib import Path
65
from typing import Any, Dict, Tuple, Type, Union
76

87
import numpy as np
@@ -20,8 +19,8 @@
2019

2120

2221
def _normalize_path(path):
23-
if isinstance(path, Path):
24-
path = str(path)
22+
if isinstance(path, os.PathLike):
23+
path = os.fspath(path)
2524

2625
if isinstance(path, str) and not is_remote_uri(path):
2726
path = os.path.abspath(os.path.expanduser(path))

xarray/backends/netCDF4_.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import functools
22
import operator
33
import os
4-
import pathlib
54
from contextlib import suppress
65

76
import numpy as np
@@ -346,7 +345,7 @@ def open(
346345
autoclose=False,
347346
):
348347

349-
if isinstance(filename, pathlib.Path):
348+
if isinstance(filename, os.PathLike):
350349
filename = os.fspath(filename)
351350

352351
if not isinstance(filename, str):

xarray/backends/zarr.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import pathlib
32
import warnings
43
from distutils.version import LooseVersion
54

@@ -346,7 +345,7 @@ def open_group(
346345
):
347346

348347
# zarr doesn't support pathlib.Path objects yet. zarr-python#601
349-
if isinstance(store, pathlib.Path):
348+
if isinstance(store, os.PathLike):
350349
store = os.fspath(store)
351350

352351
open_kwargs = dict(

xarray/core/dataset.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from html import escape
88
from numbers import Number
99
from operator import methodcaller
10-
from pathlib import Path
10+
from os import PathLike
1111
from typing import (
1212
TYPE_CHECKING,
1313
Any,
@@ -1832,7 +1832,7 @@ def to_netcdf(
18321832
18331833
Parameters
18341834
----------
1835-
path : str, Path or file-like, optional
1835+
path : str, path-like or file-like, optional
18361836
Path to which to save this dataset. File-like objects are only
18371837
supported by the scipy engine. If no path is provided, this
18381838
function returns the resulting netCDF file as bytes; in this case,
@@ -1914,8 +1914,8 @@ def to_netcdf(
19141914

19151915
def to_zarr(
19161916
self,
1917-
store: Union[MutableMapping, str, Path] = None,
1918-
chunk_store: Union[MutableMapping, str, Path] = None,
1917+
store: Union[MutableMapping, str, PathLike] = None,
1918+
chunk_store: Union[MutableMapping, str, PathLike] = None,
19191919
mode: str = None,
19201920
synchronizer=None,
19211921
group: str = None,
@@ -1944,9 +1944,9 @@ def to_zarr(
19441944
19451945
Parameters
19461946
----------
1947-
store : MutableMapping, str or Path, optional
1947+
store : MutableMapping, str or path-like, optional
19481948
Store or path to directory in local or remote file system.
1949-
chunk_store : MutableMapping, str or Path, optional
1949+
chunk_store : MutableMapping, str or path-like, optional
19501950
Store or path to directory in local or remote file system only for Zarr
19511951
array chunks. Requires zarr-python v2.4.0 or later.
19521952
mode : {"w", "w-", "a", "r+", None}, optional

xarray/tutorial.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
def _construct_cache_dir(path):
2424
import pooch
2525

26-
if isinstance(path, pathlib.Path):
26+
if isinstance(path, os.PathLike):
2727
path = os.fspath(path)
2828
elif path is None:
2929
path = pooch.os_cache(_default_cache_dir_name)

0 commit comments

Comments
 (0)