Skip to content

bisect: don't require Sequences#9813

Merged
JelleZijlstra merged 1 commit intopython:mainfrom
Rogdham:bisect_no_sequence
Feb 25, 2023
Merged

bisect: don't require Sequences#9813
JelleZijlstra merged 1 commit intopython:mainfrom
Rogdham:bisect_no_sequence

Conversation

@Rogdham
Copy link
Copy Markdown
Contributor

@Rogdham Rogdham commented Feb 25, 2023

This PR changes the type of the first argument of bisect_left and bisect_right (and by design bisect as well) from the bisect module.

Indeed, a full Sequence is not needed, only the following is needed:

  • If the hi parameter is None, only __getitem__ and __len__
  • Else, only __getitem__

I'm asking for SupportsLenAndGetItem instead of only SupportsGetItem in the second case to avoid an exponential number of @overload.


You can check that only these attributes are needed with the following code:

from bisect import bisect, bisect_left, bisect_right


class Minimal:
    content = 'abbbbcd'

    def __len__(self):
        return len(self.content)
    
    def __getitem__(self, pos):
        return self.content[pos]

obj = Minimal()

assert bisect(obj, 'b') == 5
assert bisect_left(obj, 'b') == 1
assert bisect_right(obj, 'b') == 5

@github-actions
Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra
Copy link
Copy Markdown
Member

It's a bit more complicated because it uses the C sequence protocol and therefore doesn't allow e.g. dicts, but I don't think we need to try to express that in the stub.

@JelleZijlstra JelleZijlstra merged commit db82110 into python:main Feb 25, 2023
@Rogdham
Copy link
Copy Markdown
Contributor Author

Rogdham commented Feb 25, 2023

Thank you!

@Rogdham Rogdham deleted the bisect_no_sequence branch February 25, 2023 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants