From aa1a644cc78d1c350863916e9e030d6850c8d57e Mon Sep 17 00:00:00 2001 From: Abhi-H Date: Mon, 23 Nov 2020 15:07:09 +0000 Subject: [PATCH 1/3] Using os.PathLike instead of pathlib.Path --- pandas/_typing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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]] From 35a89244fe13c6ffcb33cd5fa8013e904505be3c Mon Sep 17 00:00:00 2001 From: Abhi-H Date: Thu, 26 Nov 2020 19:05:00 +0000 Subject: [PATCH 2/3] Fixing mypy issues --- pandas/io/common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index 8ec0a869c7042..3f6820ee078af 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -7,7 +7,7 @@ from io import BufferedIOBase, BytesIO, RawIOBase, TextIOWrapper import mmap import os -import pathlib +from os import PathLike from typing import IO, Any, AnyStr, Dict, List, Mapping, Optional, Tuple, cast from urllib.parse import ( urljoin, @@ -187,7 +187,7 @@ def stringify_path( # 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): + elif isinstance(filepath_or_buffer, PathLike): filepath_or_buffer = str(filepath_or_buffer) return _expand_user(filepath_or_buffer) From 4370e7685e97e9687e6959754f24d99b8abd4d95 Mon Sep 17 00:00:00 2001 From: Abhi-H Date: Sat, 28 Nov 2020 19:32:22 +0000 Subject: [PATCH 3/3] Cleaning up stringify_path --- pandas/io/common.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index 3f6820ee078af..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 -from os import PathLike 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, PathLike): - 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)