Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve stubs for the bisect module #13350

Closed
wants to merge 1 commit into from
Closed

Conversation

lmoureaux
Copy link

The stubs were requiring SupportsRichComparisonT, but bisect only uses lt. Be more specific as to which comparison is actually required, which depends on the function. This allows using bisect with types that only implement lt.

Closes #13347.

The stubs were requiring SupportsRichComparisonT, but bisect only
uses __lt__. Be more specific as to which comparison is actually
required, which depends on the function. This allows using bisect with
types that only implement __lt__.

Closes python#13347.
@Daverball
Copy link
Contributor

As mentioned in the issue, objects can implement either __lt__ or __gt__ in order to support <, it doesn't need to be __lt__. That's what SupportsRichComparison is for, it's a looser restriction than SupportsDunderLT[Any], not a stricter one.

The core of the issue was the use of a type variable to ensure comparability between the needles in the haystack and the given needle, not the use of SupportsDunderLT | SupportsDunderGT as an upper bound for the type variable.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

spark (https://github.com/apache/spark)
+ python/pyspark/core/rdd.py:1392: error: Argument 1 to "bisect_left" has incompatible type "list[K]"; expected "SupportsLenAndGetItem[SupportsDunderLT[Any]]"  [arg-type]

mkdocs (https://github.com/mkdocs/mkdocs)
+ mkdocs/plugins.py:525: error: No overload variant matches argument types "list[Callable[..., Any]]", "Callable[..., Any]", "Callable[[Any], Any | int]"  [call-overload]
+ mkdocs/plugins.py:525: note: Possible overload variants:
+ mkdocs/plugins.py:525: note:     def [_T] insort_right(a: MutableSequence[_T], x: SupportsDunderLT[_T], lo: int = ..., hi: int | None = ..., *, key: None = ...) -> None
+ mkdocs/plugins.py:525: note:     def [_T, _U] insort_right(a: MutableSequence[_T], x: SupportsDunderLT[_U], lo: int = ..., hi: int | None = ..., *, key: Callable[[_T], _U]) -> None

core (https://github.com/home-assistant/core)
+ homeassistant/components/huawei_lte/sensor.py:95: error: Cannot infer function type argument  [misc]
+ homeassistant/components/huawei_lte/sensor.py:104: error: Cannot infer function type argument  [misc]
+ homeassistant/components/sensibo/climate.py:116: error: Cannot infer type argument 1 of "bisect_left"  [misc]

@lmoureaux
Copy link
Author

Ok, now I understand why this is complicated, < calls __gt__ if __lt__ is not present or with derived classes. I won't spend more time in this rabbit hole.

@lmoureaux lmoureaux closed this Dec 31, 2024
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.

Too restrictive types for bisect.bisect_left
2 participants