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

feat(types): Use typing.SupportsInt and typing.SupportsFloat #5540

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

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.

Closes #5158

Suggested changelog entry:

    Adds support for `typing.SupportsInt` and `typing.SupportsFloat`

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.

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
2 participants