-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Annotate to_json #26449
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
Annotate to_json #26449
Changes from all commits
8a5e3cd
dfc8f18
066ea21
bef941e
c92423b
98bea68
466f7b5
223f7ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,19 +9,23 @@ | |
import lzma | ||
import mmap | ||
import os | ||
import pathlib | ||
from urllib.error import URLError # noqa | ||
from urllib.parse import ( # noqa | ||
urlencode, urljoin, urlparse as parse_url, uses_netloc, uses_params, | ||
uses_relative) | ||
from urllib.request import pathname2url, urlopen | ||
import zipfile | ||
from typing import AnyStr, IO, Union | ||
|
||
from pandas.errors import ( # noqa | ||
AbstractMethodError, DtypeWarning, EmptyDataError, ParserError, | ||
ParserWarning) | ||
|
||
from pandas.core.dtypes.common import is_file_like | ||
|
||
from pandas._typing import FilePathOrBuffer | ||
|
||
# gh-12665: Alias for now and remove later. | ||
CParserError = ParserError | ||
|
||
|
@@ -67,7 +71,8 @@ def _is_url(url): | |
return False | ||
|
||
|
||
def _expand_user(filepath_or_buffer): | ||
def _expand_user( | ||
filepath_or_buffer: FilePathOrBuffer) -> FilePathOrBuffer: | ||
"""Return the argument with an initial component of ~ or ~user | ||
replaced by that user's home directory. | ||
|
||
|
@@ -82,6 +87,8 @@ def _expand_user(filepath_or_buffer): | |
""" | ||
if isinstance(filepath_or_buffer, str): | ||
return os.path.expanduser(filepath_or_buffer) | ||
elif isinstance(filepath_or_buffer, pathlib.Path): | ||
return filepath_or_buffer.expanduser() | ||
return filepath_or_buffer | ||
|
||
|
||
|
@@ -93,7 +100,8 @@ def _validate_header_arg(header): | |
"the row(s) making up the column names") | ||
|
||
|
||
def _stringify_path(filepath_or_buffer): | ||
def _stringify_path( | ||
filepath_or_buffer: FilePathOrBuffer) -> Union[str, IO[AnyStr]]: | ||
"""Attempt to convert a path-like object to a string. | ||
|
||
Parameters | ||
|
@@ -115,25 +123,24 @@ def _stringify_path(filepath_or_buffer): | |
Any other object is passed through unchanged, which includes bytes, | ||
strings, buffers, or anything else that's not even path-like. | ||
""" | ||
try: | ||
import pathlib | ||
_PATHLIB_INSTALLED = True | ||
except ImportError: | ||
_PATHLIB_INSTALLED = False | ||
|
||
try: | ||
from py.path import local as LocalPath | ||
_PY_PATH_INSTALLED = True | ||
except ImportError: | ||
_PY_PATH_INSTALLED = False | ||
|
||
if hasattr(filepath_or_buffer, '__fspath__'): | ||
return filepath_or_buffer.__fspath__() | ||
if _PATHLIB_INSTALLED and isinstance(filepath_or_buffer, pathlib.Path): | ||
return str(filepath_or_buffer) | ||
# mypy lacks comprehensive support for hasattr; see mypy#1424 | ||
# TODO (PY36): refactor to use os.PathLike | ||
return filepath_or_buffer.__fspath__() # type: ignore | ||
if _PY_PATH_INSTALLED and isinstance(filepath_or_buffer, LocalPath): | ||
return filepath_or_buffer.strpath | ||
return _expand_user(filepath_or_buffer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to refactor this because Mypy wasn't narrowing types at this point and still guessed that I commented on python/mypy#6847 (comment) so will see if that gets any attention, but the refactor works as well |
||
|
||
expanded = _expand_user(filepath_or_buffer) | ||
if isinstance(expanded, pathlib.Path): | ||
return str(expanded) | ||
|
||
return expanded | ||
|
||
|
||
def is_s3_url(url): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We weren't expanding Path objects with existing code so this should handle that as well. Reviewing where to add test for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might split this off into a separate issue/PR if I can find a bug using Path objects not expanding