diff --git a/pandas/_typing.py b/pandas/_typing.py index 7f01bcaa1c50e..09c490e64957d 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -1,7 +1,7 @@ from datetime import datetime, timedelta, tzinfo from io import BufferedIOBase, RawIOBase, TextIOBase, TextIOWrapper from mmap import mmap -from pathlib import Path +from os import PathLike from typing import ( IO, TYPE_CHECKING, @@ -135,7 +135,7 @@ # filenames and file-like-objects Buffer = Union[IO[AnyStr], RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper, mmap] FileOrBuffer = Union[str, Buffer[T]] -FilePathOrBuffer = Union[Path, FileOrBuffer[T]] +FilePathOrBuffer = Union["PathLike[str]", FileOrBuffer[T]] # for arbitrary kwargs passed during reading/writing files StorageOptions = Optional[Dict[str, Any]] diff --git a/pandas/io/common.py b/pandas/io/common.py index 8ec0a869c7042..9fede5180e727 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -7,7 +7,6 @@ from io import BufferedIOBase, BytesIO, RawIOBase, TextIOWrapper import mmap import os -import pathlib from typing import IO, Any, AnyStr, Dict, List, Mapping, Optional, Tuple, cast from urllib.parse import ( urljoin, @@ -176,19 +175,8 @@ def stringify_path( Any other object is passed through unchanged, which includes bytes, strings, buffers, or anything else that's not even path-like. """ - if hasattr(filepath_or_buffer, "__fspath__"): - # https://github.com/python/mypy/issues/1424 - # error: Item "str" of "Union[str, Path, IO[str]]" has no attribute - # "__fspath__" [union-attr] - # error: Item "IO[str]" of "Union[str, Path, IO[str]]" has no attribute - # "__fspath__" [union-attr] - # error: Item "str" of "Union[str, Path, IO[bytes]]" has no attribute - # "__fspath__" [union-attr] - # error: Item "IO[bytes]" of "Union[str, Path, IO[bytes]]" has no - # attribute "__fspath__" [union-attr] - filepath_or_buffer = filepath_or_buffer.__fspath__() # type: ignore[union-attr] - elif isinstance(filepath_or_buffer, pathlib.Path): - filepath_or_buffer = str(filepath_or_buffer) + if isinstance(filepath_or_buffer, os.PathLike): + filepath_or_buffer = filepath_or_buffer.__fspath__() return _expand_user(filepath_or_buffer)