Add Interactive Brokers PyO3 type stub annotations#4350
Conversation
3e1dbb0 to
5d73e7d
Compare
|
Hi @dfjmax, Thanks for adding the IB PyO3 stub coverage. The annotations are broadly in the right shape, but I found two generated-stub gaps to fix before this lands:
If the branch has merge conflicts from a recent PR landing, please rebase on current Let me know if anything is unclear. |
|
Hi @cjdsellers, before I push an update, two quick questions. The uppercase names on the IB enums (like MarketDataType.DELAYED) were missing from the stub. The other adapters get theirs with rename_all = "SCREAMING_SNAKE_CASE", so I did the same on the IB enums and the names show up now. One catch: 9 IbTickType names were hand-spelled differently from what rename_all produces (e.g. LOW_13_WEEK becomes LOW13_WEEK), and I couldn't find any code that uses them. Is rename_all the way to go here, and are those 9 name changes OK? DockerizedIBGateway and ContainerStatus only build under the gateway feature, and that feature only gets switched on by extension-module, which the stub step drops. So they never got compiled when the stub was generated. I'd switch on nautilus-interactive-brokers/gateway in the stub step so they're included. Sound right? |
|
Sounds right to me |
f754f9d to
41bec52
Compare
|
For the uppercase aliases, I added the rename_all attribute (SCREAMING_SNAKE_CASE) to the IB enums so the stub exposes them like the other adapters. The macro-generated enums in misc.rs were still coming through in PascalCase because the stub generator couldn't see rename_all through the macro, so I inlined define_ib_i32_enum there to apply it directly. That also made the hand-written classattr alias blocks redundant, so I removed those. For the gateway exports, I enabled nautilus-interactive-brokers/gateway in the stub step so DockerizedIBGateway and ContainerStatus get included. The rename also broke a couple of PascalCase references in the interactive_brokers_pyo3 wrapper and one test, so I updated those. The 9 IbTickType names follow rename_all, as @faysou confirmed. Let me know if this looks good or if you'd like anything changed. |
The Interactive Brokers adapter exposed 60 `#[pyclass]` and 35 `#[pymethods]` bindings to Python with no matching `pyo3-stub-gen` annotations, leaving every config, factory, enum, and the historical client untyped. Add the stub derive attributes and generated stub files to bring coverage in line with the other Rust-backed adapters (e.g. betfair). - Enable `pyo3-stub-gen` in `crates/adapters/interactive_brokers` (added to the `python` feature and as an optional workspace dependency) - `gen_stub_pyclass` on structs (configs, factories, historical client, gateway, instrument provider, execution/data cores) - `gen_stub_pyclass_enum` on enums (error kinds, config enums, and the `common/enums/*` IB domain enums, including the `macro_rules!`-generated set) - `gen_stub_pymethods` on every `#[pymethods]` impl in `python/` - Regenerate and commit the v2 stub output (`adapters/interactive_brokers/__init__.pyi` and the `adapters/__init__.pyi` re-export) Verified with `cargo check -p nautilus-interactive-brokers --features python` and `make py-stubs-v2`.
41bec52 to
6546c44
Compare
cjdsellers
left a comment
There was a problem hiding this comment.
Thank you for the follow-ups @dfjmax 👌
Summary
The Interactive Brokers adapter is the last Rust-backed adapter with no
pyo3-stub-gencoverage: all60#[pyclass]and35#[pymethods]bindings were exposed to Python without matching stub annotations, so every config, factory, enum, error, the historical client, and the data/execution clients resolved as untyped (Unknown) and carried no constructor signatures.This brings IB in line with the other Rust-backed adapters (e.g. betfair) by enabling
pyo3-stub-genand adding the derive attributes per the Type stub annotations convention.Changes
crates/adapters/interactive_brokers/Cargo.toml: addpyo3-stub-gento thepythonfeature and as an optional workspace dependency.gen_stub_pyclasson the11structs (configs, factories, historical client, gateway, instrument provider, execution/data cores).gen_stub_pyclass_enumon the49enums (error kinds, config enums, and thecommon/enums/*IB domain enums — including the set produced by themacro_rules!incommon/enums/misc.rs).gen_stub_pymethodson every#[pymethods]impl inpython/.Stubs emit to
nautilus_trader.adapters.interactive_brokers, matching the existingnautilus_trader.core.nautilus_pyo3.interactive_brokersruntime module already declared on each#[pyclass].Verification
cargo check -p nautilus-interactive-brokers --features python— all annotations expand cleanly (incl. themacro_rules!-generated enums).cargo fmt,cargo clippy,cargo doc,cargo machete(confirmspyo3-stub-genis used), and all nautilus/pyo3 convention checks.