-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: Option for arg/return type hints and correct typing for std::filesystem::path #5450
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
rwgk
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.
Nice!
Update Custom type casters documentation
Could you please try to include that in this PR? — For awareness, please see this comment. Not sure if that changes anything for you. If not, completely fine.
Update type hints for Eigen
Best in a follow-on PR.
Sure! I can also try to adopt some of the documentation updates from #3862 as you suggested in #5416 since I am already at it. |
…to arg_return_type_hints
|
While thinking about a test function for Outstanding task: "Update Custom type casters documentation" |
|
I have updated the documentation. This should also fix #5416 since the example changed (and hopefully I made no mistake). One CI check failed, but that is a dependency timeout (maybe you can rerun it?). |
Done and it passed (very quickly). I can look at the code changes only later (might be a day or two). |
…ustom caster example
|
I have changed the example to only use the pybind11 API. The documentation text was also changed a little bit:
|
rwgk
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.
Awesome work, thanks a lot!
@cryos could you help with a second set of eyes? Is there anything that stands out to you when glancing through?
|
Made a small correction in the |
|
Since there was no feedback from anyone else, I looked through the entire PR again. I found only one tiny oversight (aa21ab5). I also merged master. Waiting for CI to finish. When I see it passing I'll merge. |
|
|
Description
This PR adds the option for defining
arg_nameandreturn_namein a customtype_caster, which allows having different python type hints for arguments and return value. To check ifarg_nameorreturn_nameis available ornameshould be used as fallback is implemented using the template classesas_arg_typeandas_return_type.Being entirely optional, this should not impact any existing
type_caster.This type hint specialization system is applied to the
type_casterforstd::filesystem::path.Here,
Union[os.PathLike, str, bytes]is the argument type hint andPathis the return type hint.This is more accurate than the previous usage of
os.PathLikefor both argument and return type hint.Also, most classes in pybind11::typing now support this feature for nested types, e.g.,
py::typing::List<std::filesystem::path>becomesList[Union[os.PathLike, str, bytes]]in arguments andList[Path]in return types.I have added unit tests to check for the new signatures of stl/filesystem in typing containers and also to verify that
nameis used as fallback for other types (tested forstd::vector<std::filesystem::path>).Suggested changelog entry:
Possible TODOs
Custom type castersdocumentationUpdate type hints for Eigen (currently uses(going into follow-up PR)np.ndarrayas arg type hint but also takes lists and tuples (should probably benumpy.typing.ArrayLike+ maybetyping.Annotatedfor shape/dtype annotationTests for py::typing classes:
std::vector<T>(fallback)Tuple<T...>Tuple<T, ellipsis>Dict<K, V>List<T>Set<T>Iterable<T>Iterator<T>Callable<Return(Args...)>Callable<Return(ellipsis)>Union<T...>Optional<T>TypeGuard<T>TypeIs<T>I would love to get feedback on this!
Especially on the "Possible TODO" regarding Eigen. Should I implement that here in this PR, or should I open a separate PR later?
Currently, I am working on adding typing stubs to Open3D (isl-org/Open3D#6917) using
pybind11-stubgen.Applying mypy/pyright on existing example code showed that a lot of type checks failed, which could be fixed by this PR.