Skip to content

BytesIO should inherit BufferedIOBase #4075

Closed

Description

@tarcisioe

I noticed this because mypy isn't letting me pass BytesIO to a dataclass field typed BufferedIOBase. There is a TODO here:

# TODO should extend from BufferedIOBase
, but I didn't find an issue with the rationale for it not to inherit as of now.

I can make a Pull Request if introducing the inheritance (and removing redundant attributes/methods) is all that is needed, but I am very unsure if that's it, so I decided to open this issue before anything else.

Activity

  1. JelleZijlstra commented on May 25, 2020

    @JelleZijlstra
    Member

    Changes to the IO class hierarchy tend to be tricky, because the IO protocols are inconsistent and there are sometimes incompatible method overrides. In this case, BytesIO already inherits from BinaryIO, so there's a risk that making it also inherit from BufferedIOBase will cause trouble.

  2. tarcisioe commented on May 25, 2020

    @tarcisioe
    ContributorAuthor

    I see. I find it fairly unexpected that mypy ends up with an error that directly contradicts the definition of BytesIO in the standard library, and its documentation as well (the documentation explicitly says that it inherits BufferedIOBase). But I see how this definition can be fairly tricky, especially after reading some other issues here.

    I ended up depending on BinaryIO where I could, and where I used isinstance checks I ended up settling for hasattr(obj, "read") or similar. Serves my purpose right now.

    Feel free to close this if desired (or maybe keep as an issue version of the TODO?).

  3. JelleZijlstra commented on May 25, 2020

    @JelleZijlstra
    Member

    Just to be clear I don't know for sure what will happen if we make BytesIO inherit from BufferedIOBase; feel free to submit a PR and see what Travis has to say. I just wanted to give some explanation for why this might turn out to be tricky. I do agree that this should be fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: ioI/O related issues

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions