Skip to content

TYP: tighten IO Protocols #326

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

Merged
merged 1 commit into from
Sep 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,32 @@ DtypeObj = Union[np.dtype[np.generic], ExtensionDtype]
AnyStr_cov = TypeVar("AnyStr_cov", str, bytes, covariant=True)
AnyStr_con = TypeVar("AnyStr_con", str, bytes, contravariant=True)

class BaseBuffer(Protocol): ...
class ReadBuffer(BaseBuffer, Protocol[AnyStr_cov]): ...
class WriteBuffer(BaseBuffer, Protocol[AnyStr_cov]): ...
class BaseBuffer(Protocol):
@property
def mode(self) -> str: ...
def seek(self, __offset: int, __whence: int = ...) -> int: ...
def seekable(self) -> bool: ...
def tell(self) -> int: ...

class ReadBuffer(BaseBuffer, Protocol[AnyStr_cov]):
def read(self, __n: int = ...) -> AnyStr_cov: ...

class WriteBuffer(BaseBuffer, Protocol[AnyStr_con]):
def write(self, __b: AnyStr_con) -> Any: ...
def flush(self) -> Any: ...

class ReadPickleBuffer(ReadBuffer[bytes], Protocol):
def readline(self, size: int | None = ...) -> bytes: ...
def readline(self) -> bytes: ...
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the buffers are used as input arguments (not as return values), so we can remove not needed optional arguments to be compatible with a wider class of IO functions that might or might not implement optional keywords.


class ReadCsvBuffer(ReadBuffer[AnyStr_cov], Protocol[AnyStr_cov]):
def __iter__(self) -> Iterator[AnyStr_cov]: ...
def fileno(self) -> int: ...
def readline(self) -> AnyStr_cov: ...
@property
def closed(self) -> bool: ...

class WriteExcelBuffer(WriteBuffer[bytes], Protocol):
def truncate(self, size: Union[int, None] = ...) -> int: ...
def truncate(self, size: int | None = ...) -> int: ...

FilePath = Union[str, PathLike[str]]
FilePathOrBuffer = Union[
Expand Down