fix: Invert input/output context if a Python function is called from C++#6055
Open
ximion wants to merge 1 commit intopybind:masterfrom
Open
fix: Invert input/output context if a Python function is called from C++#6055ximion wants to merge 1 commit intopybind:masterfrom
ximion wants to merge 1 commit intopybind:masterfrom
Conversation
e867f87 to
dccf0e5
Compare
dccf0e5 to
79afc8b
Compare
When the Callable itself is an input (parameter) to a C++ function, its arguments are outputs (C++ passes them to the Python callback), and vice versa. Therefore, we must invert them if C++ calls a Python function, but keep them the same in the other direction.
6e2a7f8 to
3c2fd06
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi!
Description
In my project, I have a type
ByteVectorwith a type-caster defined like this:PYBIND11_TYPE_CASTER(ByteVector, io_name("bytes | bytearray", "bytes"));So, we accept any
bytesorbytearrayto turn into aByteVector, but when passing it to Python, it's alwaysbytes(because Python can't modify the vector, which is alright in this case).API users pass in Python functions as callbacks that are called on specific events that the C++ interface processes. This currently leads to type annotations that look like this:
The function return type is only
byteseven though it is input to C++ and therefore should accept both byte-like types. Similar things apply for the path.Currently, pybind11 expects Python to call C++ functions when receiving a callback, not the opposite.
This patch fixes the type annotations by inverting them if Python passes in a function into C++, and leaving them the same in the other direction. This leads to type annotations that look like this:
Now, if you pass in a function Python -> C++, its i/o context is inverted, as it will be called from C++. However, in the other direction (receiving one from C++) everything stays the same as before.
I think this is probably the best we can do to address both cases automatically in pybind11, or do you have a better solution to this problem?
Thank you for making pybind11, it's an incredibly helpful project!
Suggested changelog entry: