Skip to content

Automate Python stub generation for builtin structs#10983

Merged
burhankhanzada merged 3 commits intoslint-ui:masterfrom
burhankhanzada:python-stub-auto-generate
Mar 19, 2026
Merged

Automate Python stub generation for builtin structs#10983
burhankhanzada merged 3 commits intoslint-ui:masterfrom
burhankhanzada:python-stub-auto-generate

Conversation

@burhankhanzada
Copy link
Member

@burhankhanzada burhankhanzada commented Mar 9, 2026

This PR automates the maintenance of Python type stubs .pyi for Slint's builtin structs, ensuring they stay perfectly in sync with the internal Rust definitions.

@burhankhanzada burhankhanzada marked this pull request as draft March 9, 2026 11:57
@burhankhanzada burhankhanzada force-pushed the python-stub-auto-generate branch from c8b5a7d to 958e508 Compare March 10, 2026 15:28
@burhankhanzada burhankhanzada marked this pull request as ready for review March 10, 2026 21:00
@burhankhanzada burhankhanzada requested a review from tronical March 10, 2026 21:00
#[path = "stub-gen/language.rs"]
mod language;

fn main() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

language.pyi is checked in, it's also generated in build.rs. We need to pick one ;-).

When I suggested to put this into stub-get, I had in mind that stub-gen is run manually and the generated file is checked in.

If it's possible to do this all in build.rs and the file is saved in the right location, that would also be nice. Are you sure that works?

(If it does, that would be great)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well i have try to check first delete the language.pyi file then run cargo build -p slint-python and it build that file. So if you went i can remove language.pyi from git history.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that sounds good. At least I think that's the general direction we want also later then with the pyo3 exposed types, too. In that case, I suggest to fold language.rs into build.rs.

mod language;

fn main() {
println!("cargo:rerun-if-changed=../../internal/common/builtin_structs.rs");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? Doesn't rustc take care of the as the .rs files are included via mod?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right its not need coz it will be detected automatically as we include i-slint-common as dependency.

writeln!(writer, "import typing").unwrap();

$(
if stringify!($NameTy) == "BuiltinPublicStruct" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of stringify, would it perhaps possible to use a trick similar to export_builtin_structs in api/rs/slint/lib.rs ? So invoke the macro on for_each_builtin_structs, match for "big" pattern as you do here, produces tokens for the scaffolding (leading and trailing stuff), and then invoke recursively, but with a tag to disambiguate and with $NameTy so that then you can match for BuiltinPrivateStruct ?

@burhankhanzada burhankhanzada requested a review from tronical March 19, 2026 09:46
@burhankhanzada burhankhanzada merged commit 3536d6c into slint-ui:master Mar 19, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants