-
Couldn't load subscription status.
- Fork 2.2k
Expand float and complex strict mode to allow ints and ints/float. Updates type hints to match better with typing.SupportsIndex
#5879
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
base: master
Are you sure you want to change the base?
Conversation
|
I have started working on this too. |
This is known at compile time so it can be constexpr
|
Here is my implementation and documentation changes. It needs tests but I am going to stop for today. |
|
You will need to remove the constexpr check. I didn't realise it was a newer C++ feature |
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
|
TODO: figure out why it doesn't work in C++11. |
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
|
I have just found that a python float passed to an int argument will raise a TypeError. This is probably something we should resolve here because it effects these tests. m.def("func", [](int){});>>> func.__doc__
func(arg: typing.SupportsInt) -> None
>>> func(5.5)
TypeError
>>> func(numpy.float32(5.5)) # This works fine |
The int type caster allows anything that implements __int__ with explicit exception of the python float. I can't see any reason for this. This modifies the int casting behaviour to accept a float. If the argument is marked as noconvert() it will only accept int.
|
Here is my proposed change. You will need to update the tests. Here are my unittests |
|
I noticed something else to have address. According to the documentation all of int,float,complex support falling back on |
typing.SupportsIndex
I need to find a block of time to look at this carefully. Might be a couple days. Thanks for working on this! |
| #if defined(PYPY_VERSION) | ||
| object index; | ||
| if (PYBIND11_INDEX_CHECK(src.ptr())) { | ||
| index = reinterpret_steal<object>(PyNumber_Index(src.ptr())); | ||
| if (!index) { | ||
| PyErr_Clear(); | ||
| if (!convert) | ||
| return false; | ||
| } else { | ||
| src_or_index = index; | ||
| } | ||
| } | ||
| #endif |
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.
Can you explain why you are doing this?
Doesn't PyComplex_AsCComplex handle that?
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.
Without this I was getting failures on old pypy versions see https://github.com/pybind/pybind11/actions/runs/18790097220/job/53618039827.
See this issue for more info pypy/pypy#3383
So I copied the logic from the numeric caster. Ref
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.
Okay I think I understand. Can you add a comment in the code explaining it?
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Description
Breaking change to allow Python ints into float when under strict typing. This change was also done to complex to allow floats and ints. Now that Python ints can be passed into floats it changes behavior for overload resolution. A method that takes float that is registered before a method that takes an int will now get executed when a Python int is passed in. This overload resolution also affects methods with
std::complex.This was done to better match PEP 484 numeric tower rules.
Add
typing.SupportsIndexto the int/float input types to better match fall backs.This corrects a mistake that these where supported but, the type hint was not updated.
Change complex input types to
typing.SupportsComplex | typing.SupportsFloat | typing.SupportsIndexto match runtime conversion rules.Resolves #5878
Provides a work around for #5767
Suggested changelog entry:
Expand float and complex strict mode to allow ints and ints/float. Updates type hints to match better with
typing.SupportsIndex.📚 Documentation preview 📚: https://pybind11--5879.org.readthedocs.build/
📚 Documentation preview 📚: https://pybind11--5879.org.readthedocs.build/
📚 Documentation preview 📚: https://pybind11--5879.org.readthedocs.build/
📚 Documentation preview 📚: https://pybind11--5879.org.readthedocs.build/
📚 Documentation preview 📚: https://pybind11--5879.org.readthedocs.build/
📚 Documentation preview 📚: https://pybind11--5879.org.readthedocs.build/
📚 Documentation preview 📚: https://pybind11--5879.org.readthedocs.build/
📚 Documentation preview 📚: https://pybind11--5879.org.readthedocs.build/
📚 Documentation preview 📚: https://pybind11--5879.org.readthedocs.build/