Skip to content

Reword inheritance diagram for BufferedRandom to encompass _io and _pyio implementations #141627

Closed

Description

@tbrugere

Documentation

The documentation for io.BufferedRandom states

A buffered binary stream providing higher-level access to a seekable RawIOBase raw binary stream. It inherits from BufferedReader and BufferedWriter.

(emphasis mine)

Except when runing cpython,

Python 3.14.0rc3 (main, Sep 18 2025, 19:47:22) [Clang 20.1.4 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from io import BufferedRandom, BufferedReader
>>> issubclass(BufferedRandom, BufferedReader)
False
>>> BufferedRandom.mro()
[<class '_io.BufferedRandom'>, <class '_io._BufferedIOBase'>, <class '_io._IOBase'>, <class 'object'>]

It seems that BufferedRandom does not inherit from either BufferedReader or BufferedWriter

Linked PRs

Activity

  1. added a commit that references this issue on Nov 16, 2025
  2. picnixz commented on Nov 16, 2025

    @picnixz
    Member

    This is actually half-true. It does inherit from those classes when it's using the pure Python implementation. However, I see how confusing this can be and the alternative is to register the C implementation of BufferedRandom as a virtual subclass of BufferedReader and BufferedWriter but this is an overkill IMO.

  3. changed the title [-]Documentation states BufferedRandom inherits from BufferedReader and BufferedWriter, but it doesn't.[/-] [+]Relax inheritance assumptions for `_io.BufferedRandom`[/+] on Nov 16, 2025
  4. changed the title [-]Relax inheritance assumptions for `_io.BufferedRandom`[/-] [+]Reword inheritance diagram for `BufferedRandom` to encompass `_io` and `_pyio` implementations[/+] on Nov 16, 2025
  5. cmaloney commented on Nov 16, 2025

    @cmaloney
    Contributor

    In the C case, BufferedReader, BufferedWriter, and BufferedRandom all have the same core struct buffered + many method implementations. So it is implemented as though it inherited, just doesn't expose that at the Python abstraction.

    That it "behaves like" is already documented in the last paragraph of the BufferedRandom class documentation:

    BufferedRandom is capable of anything BufferedReader or BufferedWriter can do. In addition, seek() and tell() are guaranteed to be implemented.

    I lean for just removing the "It inherits from" sentence entirely. It doesn't inherit but in my going through topic-IO issues haven't seen any bugs filed against that until this one. Making it virtually inherit changes isinstance might break some code; removing the doc line is unlikely to break anything.

  6. mohsinm-dev commented on Nov 16, 2025

    @mohsinm-dev
    Contributor

    @cmaloney Thanks, that matches what I’m seeing too. In the C path, they do it effectively “as if” they inherited, but the Python MRO doesn’t expose that.

    I’ll take the least‑surprising fix and drop the "It inherits from …” sentence entirely. The next paragraph already communicates the important part.

  7. added 5 commits that reference this issue on Jun 2, 2026
  8. added
    3.13only security fixes
    3.14bugs and security fixes
    3.15bugs and security fixes
    3.16new features, bugs and security fixes
    on Jun 2, 2026
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

    3.13only security fixes3.14bugs and security fixes3.15bugs and security fixes3.16new features, bugs and security fixesdocsDocumentation in the Doc dirtopic-IO

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions