Skip to content

Add typing.Reader and typing.Writer #564

@ilevkivskyi

Description

@ilevkivskyi

I recently stumbled at a TODO in typeshed that proposes to split IO[AnyStr] into smaller pieces. I think it is a good idea and propose to add two protocols:

class Reader(Protocol[AnyStr]):
    def read(self, n: int = ...) -> AnyStr: ...
    def readlines(self) -> List[AnyStr]: ...
    def close(self) -> None: ...

class Writer(Protocol[AnyStr]):
    def write(self, s: AnyStr) -> int: ...
    def writelines(self, lines: List[AnyStr]) -> None: ...
    def close(self) -> None: ...

Then IO[AnyStr] can subclass both, plus add some less often used methods (also IO should stay nominal). What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions