Skip to content

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

Closed
@twoertwein

Description

@twoertwein

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementIO DataIO issues that don't fit into a more specific labelTypingtype annotations, mypy/pyright type checking

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions