Skip to content

bisect: Allow sequences without __len__ when hi is explicitly provided#15493

Merged
srittau merged 1 commit intopython:mainfrom
FuYnAloft:main
Mar 7, 2026
Merged

bisect: Allow sequences without __len__ when hi is explicitly provided#15493
srittau merged 1 commit intopython:mainfrom
FuYnAloft:main

Conversation

@FuYnAloft
Copy link
Contributor

Fixes #15492

Currently, the bisect module stubs strictly require the first argument a to implement __len__ (via SupportsLenAndGetItem or similar sequence protocols). However, in the CPython source code, len(a) is never evaluated if the hi parameter is explicitly provided.

This causes false-positive type errors when users pass custom lazy-evaluated sequences, database cursors, or virtual arrays that only support __getitem__, even when they properly define the upper bound hi.

This PR fixes this by introducing new overloads that fall back to SupportsGetItem[int, T] when hi: int is specified.

CPython's `bisect` does not evaluate `len(a)` if `hi` is explicitly passed.
This commit adds `SupportsGetItem` overloads to reflect this runtime behavior.
@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2026

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

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@srittau srittau merged commit 6827cd4 into python:main Mar 7, 2026
51 checks passed
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.

bisect: functions require __len__ on the sequence even when hi is explicitly provided

2 participants