Skip to content

Stub: allow custom imports#5877

Open
Tpt wants to merge 1 commit intoPyO3:mainfrom
Tpt:tpt/custom-imports
Open

Stub: allow custom imports#5877
Tpt wants to merge 1 commit intoPyO3:mainfrom
Tpt:tpt/custom-imports

Conversation

@Tpt
Copy link
Contributor

@Tpt Tpt commented Mar 13, 2026

This feature is required to allow user-provided custom type hint to import types

These imports are written in a small DSL on the module like:

#[pymodule(stubs = {
    from datetime import datetime as dt, time;
    from uuid import UUID;
})]

Then parsed, sent as an AST inside the introspection data (following the same AST format as the type hints) and serialized by the introspection crate that merges these imports with the auto generated ones

The #[pymodule] parameter is named stub because we might include some other features in the future like protocols

These imports are written in a small DSL like:
```rust
#[pymodule(stubs = {
    from datetime import datetime as dt, time;
    from uuid import UUID;
})]
```
Then parsed, sent as an AST inside the introspection data (following the same AST format as the type hints) and serialized by the introspection crate that merges these imports with the auto generated ones

The `#[pymodule]` parameter is named `stub` because we might include some other features in the future like protocols
@Tpt Tpt force-pushed the tpt/custom-imports branch from 3ac8fee to 4fbc371 Compare March 13, 2026 14:02
@Tpt Tpt marked this pull request as ready for review March 13, 2026 14:51
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks, I have a feeling it'll take a couple of rounds of review to figure out my preferred design on this one! Please bear with me 🙏

Comment on lines +6 to +7
from datetime import datetime as dt, time;
from uuid import UUID;
Copy link
Member

Choose a reason for hiding this comment

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

I can't decide how I feel about having full Python syntax embedded within the rust source like this. What are the alternatives? e.g. could be text, then it would be possible to use include_str! to maintain the stub as external Python file. That would also avoid the limitations of formatting & Rust syntax.

Possibly also interesting is https://github.com/m-ou-se/inline-python

Copy link
Contributor Author

@Tpt Tpt Mar 17, 2026

Choose a reason for hiding this comment

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

I see two advantages of the current design:

  • having the imports close to the code (including the custom type annotation) makes it easier to get a single source of truth
  • having it parsed by a macro means we validate the syntax and send it to pyo3-introspection as an AST. This makes quite easy for the stub generation code to add to these custom imports the extra ones that are needed to for the type annotations generated by PyO3 and get a clean output.

I fear that skipping parsing step would make imports management quite painful and if we have to pay the parsing price, then it's nice to have such an embedding.
But I don't feel strongly on that, glad to pivot the MR toward something else if it's better.

if we go the embedding way, I am not sure about the ;
Pros:

  • if we allow more and more Python syntax including top level expressions it is required if we want to keep being able to use syn
  • it might make autoformatting possible

Cons:

  • departs from Python and confusing for the users

Comment on lines +48 to +52
/// A Python statement
enum PyStatement {
ImportFrom(PyImportFrom),
Import(PyImport),
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm curious why limit the stubs to just these statements - is that just to keep initial implementation scope under control?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! I guess we will expend this list to support things like protocol

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