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

fix: Render py::function as Callable #4829

Merged
merged 2 commits into from
Aug 31, 2023

Conversation

sizmailov
Copy link
Contributor

@sizmailov sizmailov commented Aug 31, 2023

Description

Render py::function as Callable

Suggested changelog entry:

Render ``py::function`` as ``Callable`` in docstring

I'm not sure I've put the test into right place. pre-commit forced the reformatting of other lines.

Copy link
Collaborator

@henryiii henryiii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine. Technically it's Callable[..., Any], but we don't do the others that way, so this is better.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
Interesting oversight.
I'll go ahead merge this, but question for completeness: did you look systematically if there are other similar oversights? (Maybe not so easy?)

@rwgk rwgk merged commit db412e6 into pybind:master Aug 31, 2023
82 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Aug 31, 2023
@sizmailov sizmailov deleted the render-py-function-as-callalble branch August 31, 2023 06:13
@sizmailov
Copy link
Contributor Author

Here is what I already have in pybind11-stubgen that is not yet fixed here (omitting typing. prefix)

py::object-> Any
py::handle -> Any
py::sequence -> Sequence
py::buffer -> Buffer

I'll take another look at pybind source and get back to you if I find anything else.

@sizmailov
Copy link
Contributor Author

@rwgk I ran through the sources one more time. The above four are the only ones missing casters.

IMO there are no downsides in capitalizing py::sequence and py::buffer implying the correspondence to typing.* types like in other places across the library.

The decision on py::object is a tricky one.
In the context of pybind we usually mean Any when we write py::object argument/return type. We don't mean to return exactly instances of python's object. So my personal preference would be to change py::object render to Any to better reflect the semantics of binding code. Here is a mypy's take on the "Any vs object":
https://mypy.readthedocs.io/en/stable/dynamic_typing.html#any-vs-object

py::handle usually should not appear in the docstrings, but sometimes it does:

[](handle arg) -> str {

At least py::handle should be rendered the same way as py::object.

I've submitted a PR for 3/4 of the above, except for py::object. I think it deserves a separate issue for discussion.

@rwgk
Copy link
Collaborator

rwgk commented Sep 1, 2023 via email

@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Mar 27, 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.

3 participants