Skip to content

Commit c4c1dc3

Browse files
authored
Using os.PathLike instead of pathlib.Path (#37979) (#38018)
1 parent e2564fd commit c4c1dc3

File tree

2 files changed

+4
-16
lines changed

2 files changed

+4
-16
lines changed

pandas/_typing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime, timedelta, tzinfo
22
from io import BufferedIOBase, RawIOBase, TextIOBase, TextIOWrapper
33
from mmap import mmap
4-
from pathlib import Path
4+
from os import PathLike
55
from typing import (
66
IO,
77
TYPE_CHECKING,
@@ -135,7 +135,7 @@
135135
# filenames and file-like-objects
136136
Buffer = Union[IO[AnyStr], RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper, mmap]
137137
FileOrBuffer = Union[str, Buffer[T]]
138-
FilePathOrBuffer = Union[Path, FileOrBuffer[T]]
138+
FilePathOrBuffer = Union["PathLike[str]", FileOrBuffer[T]]
139139

140140
# for arbitrary kwargs passed during reading/writing files
141141
StorageOptions = Optional[Dict[str, Any]]

pandas/io/common.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from io import BufferedIOBase, BytesIO, RawIOBase, TextIOWrapper
88
import mmap
99
import os
10-
import pathlib
1110
from typing import IO, Any, AnyStr, Dict, List, Mapping, Optional, Tuple, cast
1211
from urllib.parse import (
1312
urljoin,
@@ -176,19 +175,8 @@ def stringify_path(
176175
Any other object is passed through unchanged, which includes bytes,
177176
strings, buffers, or anything else that's not even path-like.
178177
"""
179-
if hasattr(filepath_or_buffer, "__fspath__"):
180-
# https://github.com/python/mypy/issues/1424
181-
# error: Item "str" of "Union[str, Path, IO[str]]" has no attribute
182-
# "__fspath__" [union-attr]
183-
# error: Item "IO[str]" of "Union[str, Path, IO[str]]" has no attribute
184-
# "__fspath__" [union-attr]
185-
# error: Item "str" of "Union[str, Path, IO[bytes]]" has no attribute
186-
# "__fspath__" [union-attr]
187-
# error: Item "IO[bytes]" of "Union[str, Path, IO[bytes]]" has no
188-
# attribute "__fspath__" [union-attr]
189-
filepath_or_buffer = filepath_or_buffer.__fspath__() # type: ignore[union-attr]
190-
elif isinstance(filepath_or_buffer, pathlib.Path):
191-
filepath_or_buffer = str(filepath_or_buffer)
178+
if isinstance(filepath_or_buffer, os.PathLike):
179+
filepath_or_buffer = filepath_or_buffer.__fspath__()
192180
return _expand_user(filepath_or_buffer)
193181

194182

0 commit comments

Comments
 (0)