Skip to content

feat(types): Use typing.SupportsInt and typing.SupportsFloat and fix other typing based bugs. #5540

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

Merged
merged 26 commits into from
Mar 18, 2025

Conversation

InvincibleRMC
Copy link
Contributor

@InvincibleRMC InvincibleRMC commented Feb 21, 2025

Description

Since int and floats by default can convert from __int__ and __float__ properly mark the input type. As discussed in #5158 this feature is useful since pybind already supports this and this pr updates so the type properly conveys that.

Updates the Final type to use the more narrower type hint since it always has to be assigned a value when created. Which means it would need to be assigned on declaration in C++ which is the narrower type hint.

Updates the std::function type to properly match the Callable type.

Update attr_with_type_hint to properly support io_name. This was accomplished by extracting out the parser into the new function generate_signature so it could use within attr_with_type_hint.

Closes #5158

Suggested changelog entry:

    Adds support for ``typing.SupportsInt`` and ``typing.SupportsFloat``. Update ``Final`` to be narrower type hint. Make ``std::function`` match ``Callable`` type. Fix ``io_name`` bug in ``attr_with_type_hint``.

InvincibleRMC and others added 8 commits February 21, 2025 17:33
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>
Copy link
Contributor

@timohl timohl left a comment

Choose a reason for hiding this comment

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

Interesting addition.
You might be still working on this, but I left some comments.

Is the change in attr_with_type_hint related to the issue this PR fixes or is this a fix for another issue introduces by my changes from adding io_name?
Were there any failing tests for that or have you added some?
If not, adding tests would be great. (I have not gone through all the test_... changes yet).

@InvincibleRMC
Copy link
Contributor Author

Is the change in attr_with_type_hint related to the issue this PR fixes or is this a fix for another issue introduces by my changes from adding io_name?

Yes this more related to the io_name changes but, wasn't uncovered since the attr_with_type_hint tests were written without any type that change based on return type. Since int was the old type being used in them making this change exposed this error.

InvincibleRMC and others added 9 commits February 21, 2025 19:03
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>
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
@InvincibleRMC
Copy link
Contributor Author

InvincibleRMC commented Feb 22, 2025

@timohl One other thing that came up is what to do about bool. Since the bool caster also works and there is no typing.SupportsBool. The common suggestions are use the object type or create a custom protocol. However pybind does not currently support protocols. I'm a little hesitant to broaden the type but maybe something like typing.Annotated[object, "bool"] might be a good middle ground for the input type.

InvincibleRMC and others added 2 commits February 22, 2025 17:09
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
@timohl
Copy link
Contributor

timohl commented Feb 26, 2025

Great that you could find a way to move the loop to a function. 👍
I can take a deeper second look on the weekend.

Regarding bool: I think object would do more harm than good.
It would allow any object to be passed and basically disable type checking.
I think this should stay as bool or we have to find a way to create the custom protocol in pybind11.
Creating a class should be no problem, but I am not sure if it is possible to derive from a python class in pybind11?

I also think this should go into a separate PR, since this PR contains some good additional fixes for io_name/attr_with_type_hint/std::function and should not wait on typing.SupportsBool.

Copy link
Contributor

@timohl timohl left a comment

Choose a reason for hiding this comment

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

I have added some comments.

Technically, this looks good to me, but the PR description must be improved.

Could you add a short explanation why supporting __float__/__int__ is useful?
In the end, it adds some clutter to the code and generated documentation, so there should be a good benefit for this.

If I understand correctly, this allows passing for example single element numpy arrays or pytorch tensors (See pytorch/pytorch#1129 (comment))

>>> import numpy as np
>>> zero_dim = np.array(3)
>>> scalar = np.float(3)
>>> single_elem = np.array([[[3]]])
>>> float(zero_dim), float(scalar), float(single_elem)
(3.0, 3.0, 3.0)
>>> empty = np.array([], dtype=np.float32)
>>> float(empty)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: only length-1 arrays can be converted to Python scalars

While pybind11 already supports this under the hood, this PR improves the type hints to reflect this.

@InvincibleRMC
Copy link
Contributor Author

@timohl I have updated the pr description. Please let me know if you want more information or have any questions.

@InvincibleRMC InvincibleRMC changed the title feat(types): Use typing.SupportsInt and typing.SupportsFloat feat(types): Use typing.SupportsInt and typing.SupportsFloat and fix othery typing based bugs. Mar 10, 2025
@InvincibleRMC InvincibleRMC changed the title feat(types): Use typing.SupportsInt and typing.SupportsFloat and fix othery typing based bugs. feat(types): Use typing.SupportsInt and typing.SupportsFloat and fix other typing based bugs. Mar 10, 2025
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.

Looks good to me. My suggestions are purely regarding naming.

InvincibleRMC and others added 2 commits March 17, 2025 16:43
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
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.

Nice, thank you!

@henryiii Do you want to glance through, too?

@rwgk rwgk merged commit dfe7e65 into pybind:master Mar 18, 2025
80 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Mar 18, 2025
@gentlegiantJGC
Copy link
Contributor

Thanks for working on this. I was thinking about this the other day.

@InvincibleRMC
Copy link
Contributor Author

Thanks for working on this. I was thinking about this the other day.

No problem!

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

[FEATURE REQUEST]: typing.SupportsInt type hint
5 participants