Skip to content
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

TYP/DOC: Use Protocols for file-like objects in read/to_* #41610

Closed
twoertwein opened this issue May 22, 2021 · 0 comments · Fixed by #43951
Closed

TYP/DOC: Use Protocols for file-like objects in read/to_* #41610

twoertwein opened this issue May 22, 2021 · 0 comments · Fixed by #43951
Labels
Enhancement IO Data IO issues that don't fit into a more specific label Typing type annotations, mypy/pyright type checking
Milestone

Comments

@twoertwein
Copy link
Member

twoertwein commented May 22, 2021

Is your feature request related to a problem?

It is often not clear which methods a "file-like object" needs to implement to be compatible with read/to_*.

Describe the solution you'd like

Define multiple typing.Protocols to make clear which methods need to be implemented, similar to python/typeshed/issues/4212.

This will probably be a bit messy: 1) many protocols and 2) most libraries expect typing.IO (which will lead to a few ignore/cast statements but also might make it difficult to determine the set of compatible protocols).

edit:

If feasible, I would like that pandas.io.common.get_handle has roughly the following interface:

IOProtocols = TypeVar("IOProtocols", "insert long(?) list of generic protocols (str/bytes)")

@overload
def get_handle(path_or_buf: os.PathLike, ...) -> IOHandles[IO[str]]: ...

@overload
def get_handle(path_or_buf: IOProtocols[AnyStr], ...) -> IOHandles[IOProtocols[str]]: ...

@overload
def get_handle(path_or_buf: os.PathLike, ..., *, is_text: Literal[False]) -> IOHandles[IO[bytes]]: ...

@overload
def get_handle(path_or_buf: IOProtocols[bytes], ..., *, is_text: Literal[False]) -> IOHandles[IOProtocols[bytes]]: ...

def get_handle(path_or_buf: IOProtocols | os.PathLike, ...,  is_text: bool = True) -> IOHandles:
    # implementation
@twoertwein twoertwein added Enhancement Typing type annotations, mypy/pyright type checking Needs Triage Issue that has not been reviewed by a pandas team member labels May 22, 2021
@jbrockmendel jbrockmendel added IO Data IO issues that don't fit into a more specific label and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2021
@jreback jreback added this to the 1.4 milestone Nov 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement IO Data IO issues that don't fit into a more specific label Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants