-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add typing.SupportsIndex to int/float/complex type hints
#5891
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
Conversation
This corrects a mistake where these types were supported but the type hint was not updated to reflect that SupportsIndex objects are accepted. To track the resulting test failures: The output of "$(cat PYROOT)"/bin/python3 $HOME/clone/pybind11_scons/run_tests.py $HOME/forked/pybind11 -v is in ~/logs/pybind11_pr5879_scons_run_tests_v_log_2025-11-10+122217.txt
|
Only |
InvincibleRMC
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're missing the tests I believe which show this is already happening at run time.
I'm not completely clear about that (yet) myself. The goal of this PR
Currently I'm thinking PR #5879 is good as-is, but
|
If you look at the bottom of float and complex they fallback on index. https://docs.python.org/3.13/library/functions.html#float |
|
Ahh, this is for input only? |
I want to actually merge this PR, then merge master into PR 5879. Hopefully that'll clear up a lot of the unit test diffs there. |
|
oops, I clicked ready for review button too fast, I'm seeing the request for change only now |
|
@InvincibleRMC wrote:
Could you please help me with concrete pointers? I did not try to transfer any additional tests from PR 5879, but if you point at specific ones that you think should be included here, I could do that. I could also give you write permission to this branch, so that you can make changes directly. Please let me know. |
|
The tests that pass in custom classes with __index__ into float and complex args. I think it's float convert and complex convert. |
Summary: Changes Made 1. **C++ Bindings** (`tests/test_builtin_casters.cpp`) • Added complex_convert and complex_noconvert functions needed for the tests 2. **Python Tests** (`tests/test_builtin_casters.py`) `test_float_convert`: • Added Index class with __index__ returning -7 • Added Int class with __int__ returning -5 • Added test showing Index() works with convert mode: assert pytest.approx(convert(Index())) == -7.0 • Added test showing Index() doesn't work with noconvert mode: requires_conversion(Index()) • Added additional assertions for int literals and Int() class `test_complex_cast`: • Expanded the test to include convert and noconvert functionality • Added Index, Complex, Float, and Int classes • Added test showing Index() works with convert mode: assert convert(Index()) == 1 and assert isinstance(convert(Index()), complex) • Added test showing Index() doesn't work with noconvert mode: requires_conversion(Index()) • Added type hint assertions matching the SupportsIndex additions These tests demonstrate that custom __index__ objects work with float and complex in convert mode, matching the typing.SupportsIndex type hint added in PR 5891.
… will have to be reapplied under PR 5879.
@InvincibleRMC do these commits look right? |
|
The three pypy-3.10 jobs (ubuntu, macos-13, windows-latest) are failing with the error below. Fixing that will be a behavior change, but I think we should do that in this PR, because it's in I.e. we'd need to extract out the corresponding changes from PR 5879 as well. @InvincibleRMC could you please let me know if you agree or disagree? I'll play with Cursor some more to see if it is capable of doing that for us. |
|
Ah yeah that makes sense. Copying the back porting of __index__ to old versions of pypy. |
|
OK, I'll get Cursor going on this. (Peeling the PR apart like this is a great way for me to get my head around it. I wasn't able to follow the individual commits on 5879; I'm operating on a tiny time budget.) |
Extract PyPy-specific __index__ backporting from PR 5879 to fix PyPy 3.10
test failures in PR 5891. This adds:
1. PYBIND11_INDEX_CHECK macro in detail/common.h:
- Uses PyIndex_Check on CPython
- Uses hasattr check on PyPy (workaround for PyPy 7.3.3 behavior)
2. PyPy-specific __index__ handling in complex.h:
- Handles __index__ objects on PyPy 7.3.7's 3.8 which doesn't
implement PyLong_*'s __index__ calls
- Mirrors the logic used in numeric_caster for ints and floats
This backports __index__ handling for PyPy, matching the approach
used in PR 5879's expand-float-strict branch.
|
After commit 6267614 all tests passed. Merging. Thanks @InvincibleRMC! I'll try to use Cursor to update 5879, hoping it'll be able to deal with any merge conflicts. |
During merge, HEAD's regex pattern was kept, but master's version is preferred. The order of ` ` and `\|` in the character class is arbitrary. Keep master's order (already fixed in PR pybind#5891; sorry I missed looking back here when working on 5891).
Description
Production code changes isolated from #5879
Quoting from there:
Isolated out mainly to have the corresponding sprawling changes to the unit tests separately, to minimize the chances that we're overlooking something important in the more critical PR #5879.
Note: In contrast to #5879, the
noconvertconversion behavior is unchanged. Only the complex-casterconvertbehavior is changed (bug fix), and the type hints.Note: Most of the legwork here was done by Cursor.
@gentlegiantJGC @InvincibleRMC for visibility
Suggested changelog entry:
__index__was added, but the type hints were not updated to reflect thatSupportsIndexobjects are accepted. Also fix a long-standing bug: the complex-caster was not accepting__index__inconvertmode.