-
Notifications
You must be signed in to change notification settings - Fork 234
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
Python type aliases don't use forward references #2067
Comments
Type aliases are complicated in Python and we use strings exactly to avoid the problem with forward references. I don't understand what change you are proposing should be made to uniffi (ie, this seems a problem with Python in general and nothing specific to uniffi?) |
Sorry for not being clear. You said "we use strings exactly to avoid the problem with forward references" but we're getting compilation errors because we're not doing anything like that for aliases today, only for function types. But when I went down the path of dealing with the problem the same way, I ran into the "Variable not allowed in type expression" error. So I can think of two possible ways to deal with this problem, but was hoping others could come up with something better:
|
We support all currently supported Python versions, which IIRC, is currently 3.8+ (even though CI is currently using 3.11) I'm afraid I still don't have a good understanding of the problem you are hitting though - would it be possible to demonstrate it by making the smallest possible patch to one of our tests? Eg, test_simple_fns has one test, test_futures has another, but similar tests in any other fixture would be great too. We'd need such a test for any potential fix anyway, so this would help get the ball rolling. |
Sure, I opened a PR here #2068 to demonstrate the error in the proc-macro fixture, but I can move it to a different test if you'd prefer |
That's perfect, thanks! |
In that PR, you are trying to define a "custom type" in terms of another custom type. The custom type docs indicate that they are used to wrap a "builtin" - there's no intention they are able to wrap non-builtin types. In other words, the bug in that PR seems to be that we don't fail with a clear error about the wrapped type. I'd like to better understand your use-case - in that example, why wouldn't your API just use |
Sure! The example in the test was a minimal reproducible example; I definitely would just use My use case is mainly around composition of types in For example, I'd like to do pub struct FieldId(pub String);
pub struct EncryptedField(pub Vec<u8>);
pub struct EncryptedFields(pub HashMap<FieldId, EncryptedField>); When I use |
…rd references. Also includes many other custom-type tests, all of which work, and clarifies in the docs about which types can be used with custom types. Fixes mozilla#2067
Welp, I guess I jumped the gun there - we do support any type, not just "builtin" types. In the linked PR I updated the docs and added tests for some of these cases. |
…rd references. Also includes many other custom-type tests, all of which work, and clarifies in the docs about which types can be used with custom types. Fixes mozilla#2067
…rd references. Also includes many other custom-type tests, all of which work, and clarifies in the docs about which types can be used with custom types. Fixes mozilla#2067
…rd references. Also includes many other custom-type tests, all of which work, and clarifies in the docs about which types can be used with custom types. Fixes mozilla#2067
That's fantastic, thanks so much for doing that @mhammond |
In generated Python, our type aliases are sorted alphabetically and placed towards the end of the file. If there are nested aliases, this fails to compile when they're in the wrong order (
"Foo" is not defined
).For function type hints, we use quotation marks to allow forward references (described here). I tried this for the type aliases, but got lots of warnings
Variable not allowed in type expression
and unknown types when they're used in functions.I also tried changing the type aliases to
this worked well, but was only added in Python version 3.10 (is the minimum supported version for uniffi documented somewhere?).
My temporary solution is just to avoid nested aliases, but I have some complex types that would benefit from another solution to this problem.
The text was updated successfully, but these errors were encountered: