Skip to content

Should FilePathorBuffer use os.PathLike instead of pathlib.Path? #37979

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

Closed
willfrey opened this issue Nov 20, 2020 · 4 comments · Fixed by #38018
Closed

Should FilePathorBuffer use os.PathLike instead of pathlib.Path? #37979

willfrey opened this issue Nov 20, 2020 · 4 comments · Fixed by #38018
Labels
Typing type annotations, mypy/pyright type checking
Milestone

Comments

@willfrey
Copy link

The documentation for pandas.read_csv(...) states that it can support anything that adheres to the os.PathLike protocol, but the definition of FilePathOrBuffer is too strict.

FilePathOrBuffer = Union[Path, FileOrBuffer[T]]

where Path is a pathlib.Path object.

Could this be changed to use os.PathLike[str] or just os.PathLike?

from os import PathLike

FilePathOrBuffer = Union["PathLike[str]", FileOrBuffer]

PathLike is in quotes because PathLike only supports __class_getitem__ for Python 3.9 and later and PathLike alone might be too broad, assuming you don't want to support byte-encoded paths.

Thanks!

@twoertwein
Copy link
Member

Replacing pathlib.Path with os.PathLike[str] should definitely work! Most (all?) IO calls go through get_handle which internally calls stringify_path which converts all os.PathLike[str] using __fspath__ to a string. Unless you add a decode function to stringify_path, I wouldn't add os.PathLike[bytes].

I think we already have tests for non-pathlib.Path PathLike objects.

@twoertwein twoertwein added the Typing type annotations, mypy/pyright type checking label Nov 21, 2020
@Abhi-H
Copy link
Contributor

Abhi-H commented Nov 21, 2020

From what I gathered through @twoertwein comment, the PathLike[str] change will bring the definition of FilePathorBuffer closer to being in-line with the documentation for pandas.read_csv(...). Should I go ahead and make that change?

@twoertwein
Copy link
Member

yes, feel free to make the change.

@Abhi-H
Copy link
Contributor

Abhi-H commented Nov 22, 2020

Okay, will make a PR soon. Setting up my dev environment currently.

@jreback jreback modified the milestones: 1.2, Contributions Welcome Nov 24, 2020
@jreback jreback modified the milestones: Contributions Welcome, 1.2 Dec 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants