Introspection: Adds basic input type annotations
Implementation notes:
- The type annotation is a plain string. Indeed, it is not possible at my knowledge to properly concatenate strings from
constcode (hence without macros), making advanced structs not possible. Moreover, because it is not possible to use generic types inconstcode, we won't be able to generatelist[int]types at the moment but onlylist. This limitation will be lifted ifmin_generic_const_argsunstable feature lands at some point. However it should be possible to write a macro that validates the types and wrap them in someTypeAnnotation(&'static str)struct. Note that because our macros are aware of theOption<T>parameters annotations likeT | Noneare properly generated. We might expand that to other common containers likeVecorHashMap. - The stub generator autogenerates import with very naive code. I am not sure we want to keep autogenerating them with I fear more complexity or request the user to write them by hand. If we auto generate them we must make it more robust (maybe in a follow up?)
- The stubs generator does not support maximum line length yet, the test now reformats the stubs before doing string equality
- I have only written types for a few built-in elements to not overload this MR
- I have not added test coverage of types generated from
#[pyclass]. We will get that for free after merging #5087 - The default when no type is set is
typing.Any. It might be cleaner to makeINPUT_TYPEanOption<&'static str>and useNoneas the default value to not set an annotation instead.
Thank you for the review.
I guess we will miss the 0.25 release for this (my bad).
There is still a bunch of follow ups to get something working properly so it's definitely not a big deal.
Thanks, given all the comments I think this is good to merge
Amazing!
(and I guess I can rebase the 0.25 branch to include this after all 😂)
I would feel better to have it in 0.26 to get time to do more testing, add more types across the codebase, deprecate the input_type and output_type methods...
@Tpt seems like there was a conflict introduced in the merge queue for this one?
@abrisco Yes! Rebasing this MR encovered some edge cases I have not considered (Self in signatures and PyO3-only magic methods like __traverse__). I hope to find some time soon to fix them.
Rebase done. CI semverchecks is failing because this MR introduces a new associated constant to traits
The relevant bit to review 42c0cc664e96777b9f332dfc2a8d5928af3f76cc
@davidhewitt This MR would love a second review after the rebases. The relevant additional commit is 42c0cc664e96777b9f332dfc2a8d5928af3f76cc
Thank you for the review!