Skip to content

Using os.PathLike instead of pathlib.Path (#37979) #38018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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]]
Expand Down
16 changes: 2 additions & 14 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)


Expand Down