Closed
Description
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.Protocol
s 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