-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Conversation
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>
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.
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).
Yes this more related to the |
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>
@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 |
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Great that you could find a way to move the loop to a function. 👍 Regarding I also think this should go into a separate PR, since this PR contains some good additional fixes for |
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.
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.
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
@timohl I have updated the pr description. Please let me know if you want more information or have any questions. |
typing.SupportsInt
and typing.SupportsFloat
typing.SupportsInt
and typing.SupportsFloat
and fix othery typing based bugs.
typing.SupportsInt
and typing.SupportsFloat
and fix othery typing based bugs.typing.SupportsInt
and typing.SupportsFloat
and fix other typing based bugs.
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.
Looks good to me. My suggestions are purely regarding naming.
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
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, thank you!
@henryiii Do you want to glance through, too?
Thanks for working on this. I was thinking about this the other day. |
No problem! |
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 theCallable
type.Update
attr_with_type_hint
to properly supportio_name
. This was accomplished by extracting out the parser into the new functiongenerate_signature
so it could use withinattr_with_type_hint
.Closes #5158
Suggested changelog entry: