Skip to content

Commit 016c50b

Browse files
committed
Check for path-like objects rather than Path type, use os.fspath
1 parent 07de257 commit 016c50b

File tree

6 files changed

+21
-24
lines changed

6 files changed

+21
-24
lines changed

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.
@@ -866,7 +865,7 @@ def open_mfdataset(
866865
else:
867866
paths = sorted(glob(_normalize_path(paths)))
868867
else:
869-
paths = [str(p) if isinstance(p, Path) else p for p in paths]
868+
paths = [os.fspath(p) if isinstance(p, os.PathLike) else p for p in paths]
870869

871870
if not paths:
872871
raise OSError("no files to open")
@@ -958,8 +957,8 @@ def multi_file_closer():
958957

959958
# read global attributes from the attrs_file or from the first dataset
960959
if attrs_file is not None:
961-
if isinstance(attrs_file, Path):
962-
attrs_file = str(attrs_file)
960+
if isinstance(attrs_file, os.PathLike):
961+
attrs_file = os.fspath(attrs_file)
963962
combined.attrs = datasets[paths.index(attrs_file)].attrs
964963

965964
return combined
@@ -992,8 +991,8 @@ def to_netcdf(
992991
993992
The ``multifile`` argument is only for the private use of save_mfdataset.
994993
"""
995-
if isinstance(path_or_file, Path):
996-
path_or_file = str(path_or_file)
994+
if isinstance(path_or_file, os.PathLike):
995+
path_or_file = os.fspath(path_or_file)
997996

998997
if encoding is None:
999998
encoding = {}
@@ -1134,7 +1133,7 @@ def save_mfdataset(
11341133
----------
11351134
datasets : list of Dataset
11361135
List of datasets to save.
1137-
paths : list of str or list of Path
1136+
paths : list of str or list of path-like objects
11381137
List of paths to which to save each corresponding dataset.
11391138
mode : {"w", "a"}, optional
11401139
Write ("w") or append ("a") mode. If mode="w", any existing file at
@@ -1302,7 +1301,7 @@ def check_dtype(var):
13021301

13031302
def to_zarr(
13041303
dataset: Dataset,
1305-
store: Union[MutableMapping, str, Path] = None,
1304+
store: Union[MutableMapping, str, os.PathLike] = None,
13061305
chunk_store=None,
13071306
mode: str = None,
13081307
synchronizer=None,
@@ -1326,7 +1325,7 @@ def to_zarr(
13261325
if v.size == 0:
13271326
v.load()
13281327

1329-
# expand str and Path arguments
1328+
# expand str and path-like arguments
13301329
store = _normalize_path(store)
13311330
chunk_store = _normalize_path(chunk_store)
13321331

xarray/backends/common.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
import os.path
2+
import os
33
import time
44
import traceback
55
from pathlib import Path
@@ -20,8 +20,8 @@
2020

2121

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

2626
if isinstance(path, str) and not is_remote_uri(path):
2727
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,
@@ -1827,7 +1827,7 @@ def to_netcdf(
18271827
18281828
Parameters
18291829
----------
1830-
path : str, Path or file-like, optional
1830+
path : str, path-like or file-like, optional
18311831
Path to which to save this dataset. File-like objects are only
18321832
supported by the scipy engine. If no path is provided, this
18331833
function returns the resulting netCDF file as bytes; in this case,
@@ -1909,8 +1909,8 @@ def to_netcdf(
19091909

19101910
def to_zarr(
19111911
self,
1912-
store: Union[MutableMapping, str, Path] = None,
1913-
chunk_store: Union[MutableMapping, str, Path] = None,
1912+
store: Union[MutableMapping, str, PathLike] = None,
1913+
chunk_store: Union[MutableMapping, str, PathLike] = None,
19141914
mode: str = None,
19151915
synchronizer=None,
19161916
group: str = None,
@@ -1939,9 +1939,9 @@ def to_zarr(
19391939
19401940
Parameters
19411941
----------
1942-
store : MutableMapping, str or Path, optional
1942+
store : MutableMapping, str or path-like, optional
19431943
Store or path to directory in local or remote file system.
1944-
chunk_store : MutableMapping, str or Path, optional
1944+
chunk_store : MutableMapping, str or path-like, optional
19451945
Store or path to directory in local or remote file system only for Zarr
19461946
array chunks. Requires zarr-python v2.4.0 or later.
19471947
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)