python / python/cpython

The `bytearray()` value set to `bytes.partition()` and `bytes.rpartition()` is not converted to a `bytes()` value, keeping a `bytearray()` value in a tuple

Open
#137,217 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

The bytearray() value set to bytes.partition() and bytes.rpartition() is not converted to a bytes() value, keeping a bytearray() value in a tuple as shown below:

v = bytes(b'ABCD')

                # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓           # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
print(v.partition(bytearray(b'BC')))  # (b'A', bytearray(b'BC'), b'D')
print(v.rpartition(bytearray(b'BC'))) # (b'A', bytearray(b'BC'), b'D')
                 # ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑          # ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

The bytearray() value set to bytes.partition() and bytes.rpartition() should be converted to a bytes() value, not keeping a bytearray() value in a tuple because they are bytes functions and because of readability and consistency as shown below:

v = bytes(b'ABCD')

                # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓           # ↓↓↓↓↓
print(v.partition(bytearray(b'BC')))  # (b'A', b'BC', b'D')
print(v.rpartition(bytearray(b'BC'))) # (b'A', b'BC', b'D')
                 # ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑          # ↑↑↑↑↑

In addition, the bytes() value set to bytearray.partition() and bytearray.rpartition() is converted to a bytearray() value, not keeping a bytes() value in a tuple as shown below:

v = bytearray(b'ABCD')

                # ↓↓↓↓↓↓↓↓↓↓↓↓                      # ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
print(v.partition(bytes(b'BC')))  # (bytearray(b'A'), bytearray(b'BC'), bytearray(b'D'))
print(v.rpartition(bytes(b'BC'))) # (bytearray(b'A'), bytearray(b'BC'), bytearray(b'D'))
                 # ↑↑↑↑↑↑↑↑↑↑↑↑                     # ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
CPython versions tested on:

3.12

Operating systems tested on:

Windows

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the provided Python examples first, then locate the bytes.partition(), bytes.rpartition(), bytearray.partition(), and bytearray.rpartition() implementations and their tests. Done means the returned tuple elements consistently match the type of the object on which partitioning is called, with regression coverage for both directions and both argument types.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.