-
Notifications
You must be signed in to change notification settings - Fork 791
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
Runtime type information for objects crossing the Rust–Python boundary #2490
Conversation
You'll need to rebase on main to make CI work (because of #2489 ) :) |
@mejrs it's rebased :) |
Fixed |
👍 looks like you were thinking along the same lines for a first step as me with this PR! Nice work! So I guess the only question here is whether Similarly I wonder if users can already use these to include function signatures in docstrings? Might need some macro support. One to think about, can probably implement this in a separate PR if we want it. Just for completeness, do other use cases for these beyond the introspection API come to mind? I'm sure users will always surprise us 😊 |
Also probably obvious from my other comments, though just wanted to add I'd prefer we merge this after 0.17 is released so that we allow ourselves time to iterate on this further if needed after merge. |
Well, this PR itself is just the building block of "what is the actual Python type of this type as seen from Python". I expect that this can be used for hundreds of different things 😓. Indeed, debugging/error information seems like a good use case.
'should' is your decision, but it definitely sounds like it 'could'. I don't think it should be a part of this PR though.
Do you have an estimate on when that will be? I'm a bit tight on schedule and there's a lot of things to do until .pyi generation is a thing :) |
Tracking preparation for 0.17 at #2493 - TBH at the moment I'm working on getting those pieces complete whenever I can. I'd like it to be at most a week or two, though it depends how much time I can manage to free up to make progress. |
Fixed |
I'm not sure I understand why test fails. What should I do to fix it? |
Run |
@CLOVIS-AI sorry for the long delay with this caused by the 0.17 release. I'm now able to widen bandwidth again, I propose let's merge this and then we can continue iterating on the stub generation? I think this PR is good as-is, just needs a rebase :) |
Updated the expected test output & rebased. |
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.
This looks great, I have just a couple of nits :)
src/inspect/types.rs
Outdated
Any, | ||
/// The type `typing.None`. | ||
None, | ||
/// The type `typing.NoReturn`, which represents functions that never end (similar to `never` in Rust). |
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.
Minor nit - such functions can still panic (in rust) or raise an exception (in python)
/// The type `typing.NoReturn`, which represents functions that never end (similar to `never` in Rust). | |
/// The type `typing.NoReturn`, which represents functions that never return (similar to `never` in Rust). |
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.
Done, added clarification in parenthesis
src/inspect/types.rs
Outdated
/// The module this class comes from. | ||
module: ModuleName, | ||
/// The name of this class, as it appears in a type hint. | ||
name: &'static str, |
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.
Could someone want to create names at runtime? In that case Cow<'static, str>
is more flexible 🤔
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.
Oh, I didn't know it was possible to create a Cow
in a const
context. That does sound more flexible.
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 don't really see a use case for creating names at runtime, but I don't really see a reason to forbid it either.
src/inspect/types.rs
Outdated
/// The type is in the current module: it doesn't need to be imported in this module, but needs to be imported in others. | ||
CurrentModule, | ||
/// The type is in the specified module. | ||
Module(&'static str), |
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.
Same as above.
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.
Done.
0d34f72
to
28cc154
Compare
Thanks! With apologies will need one final rebase as #2587 has conflicted on the CHANGELOG. (Really need to find some time to work on #2337 soon.) Also the coverage job is reporting that a lot of new implementations are not covered by tests, it would be much appreciated if we could add a few more. I find it's much better to add coverage at the same time as new APIs wherever possible rather than hope it gets covered later 😄 |
The TypeInfo structure represents Python types used in hints. Its Display implementation converts it to the exact syntax for it to appear in a type hint.
The rebase is done. I do not have the enough time on my hands at the moment to increase the code coverage. I'm not familiar with GitHub's UI for code coverage so I have some trouble reading it, but it seems to me like it is complaining about all the one-liner functions that simply return a |
Understood. Given that I'm the source of the delay that caused you to lose your availability, seems reasonable to me to proceed to merge in that case. |
Thank you for contributing to pyo3!
Please consider adding the following to your pull request:
docs to all new functions and / or detail in the guideDocumentation will be added when the wholeinspect
module will be doneFirst step towards #2454.
The goal of this PR is to provide runtime information about Python types for any object that is transferred to/from Python.
Example usage:
This PR introduces two new methods:
FromPyObject::type_input
: the Python type when the object appears as a parameter to a Rust function,IntoPy::type_output
: the Python type when the object appears as a return value of a Rust function.Both methods are implemented for the most important Rust types (primitives like
usize
, collections likeHashMap
).The default implementation is to return
Any
(which is the recommended type when the exact type is missing, and avoids this PR being a breaking change).In the future, this will allow the inspection module to generate code calling
type_input
andtype_output
to delay the Python type generation until runtime, which is required because macros are executed before the Rust compiler resolves the types.Future work:
PyDict
, hashbrown tables,...); this is fairly easy and may be done on a case-by-case basis when more precise information is needed#[pyclass]