-
Notifications
You must be signed in to change notification settings - Fork 927
Introspection: adds constants to the AST #5743
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
base: main
Are you sure you want to change the base?
Conversation
Use it for default values and user-provided hints First step to support Literal type-hints and user-provided hints written using Rust AST
|
Code coverage is sparse, I plan to improve it as part of the support for the iteration on custom signature annotations |
|
|
||
| #[derive(Clone)] | ||
| #[derive(Clone, Debug)] | ||
| pub struct PythonTypeHint(PyExpr); |
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 name is not great anymore. I am considering a follow up to remove PythonTypeHint and use directly PyExpr
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 like choice to use constant strings in tests here changed the outputs (I assume this was intended).
pyo3-macros-backend/src/type_hint.rs
Outdated
| Lit::Int(i) => PyConstant::Int(i.base10_digits().into()), | ||
| Lit::Float(f) => PyConstant::Float(f.base10_digits().into()), | ||
| Lit::Bool(b) => PyConstant::Bool(b.value()), | ||
| _ => return None, |
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.
Probably can handle CStr here, maybe the byte constants too?
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.
Indeed. I have added a TODO. I am not sure how useful both are in practice.
pytests/src/consts.rs
Outdated
|
|
||
| #[pymodule_export] | ||
| pub const SIMPLE: &str = "SIMPLE"; | ||
| pub const SIMPLE: &str = "S\0\x01\t\n\r\"'\\"; |
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.
Not so simple any more 😂
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.
Indeed; Renamed
| a: "int", *_args: "str", _b: "int | None" = None, **_kwargs: "bool" | ||
| ) -> "int": ... |
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.
Is it expected that these changed? Seems potentially unfortunate.
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.
Yes! My motivation is to have a simple translation behavior between user-provided annotation and what is in the stub. For example that strings gets translated to strings. I think it will be useful if we allow to write things like Literal["foo"] in the future. The good news is that it's allowed to write type annotations as strings and type checkers are supposed to properly parse them (doc).
However, I am fine to revert this change if you prefer so.
|
I realized I forgot to migrate argument default values. Here is a fix and introduction of ellipsis in the expressions: ddbbe11 |
Use it for default values and custom signature hints
First step to support Literal type-hints and user-provided hints written using Rust AST