Skip to content

ENH: Remove mode property from BaseBuffer #60660

@ethantkoenig

Description

@ethantkoenig

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I would like to pass an io.IOBase (or a subclass, such as botocore.response.StreamingBody) to functions that accept one of the various "buffer" classes (ReadBuffer, ReadCsvBuffer, etc.)

Currently, this does not type-check (or, at least not with Pyright), because the BaseBuffer protocol defines a mode property:

pandas/pandas/_typing.py

Lines 271 to 274 in 1be2637

@property
def mode(self) -> str:
# for _get_filepath_or_buffer
...

Type "IOBase" is not assignable to declared type "ReadCsvBuffer[bytes]"
  "IOBase" is incompatible with protocol "ReadCsvBuffer[bytes]"
    "mode" is not present

Feature Description

AFAICT, the only place the BaseBuffer.mode property is accessed is this getattr call, which can handle the property not existing (due to the default value).

pandas/pandas/io/common.py

Lines 1196 to 1198 in 1be2637

return isinstance(handle, _get_binary_io_classes()) or "b" in getattr(
handle, "mode", mode
)

Can we just remove the mode property from BaseBuffer, since (AFAICT) it's not actually required by any Pandas code that handles BaseBuffer instances (and/or instances of BaseBuffer subclasses)?

Alternative Solutions

If python/typing#601 ever becomes a reality, BaseBuffer.mode could be marked as an "optional" property.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions