Skip to content

Commit b26658e

Browse files
committed
Improve type stub gen for nautilus_trader v2
1 parent 928af7d commit b26658e

File tree

15 files changed

+558
-31
lines changed

15 files changed

+558
-31
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ repos:
160160
rev: v1.18.2
161161
hooks:
162162
- id: mypy
163-
exclude: ^python/nautilus_trader/.*__init__\.py$
163+
exclude: (^python/|^nautilus_trader/.*\.pyi$)
164164
args: [
165165
"--config", "pyproject.toml",
166166
"--allow-incomplete-defs",

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/adapters/blockchain/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ python = [
3737
"nautilus-network/python",
3838
"nautilus-system/python",
3939
"pyo3",
40+
"pyo3-stub-gen",
4041
]
4142

4243
[package.metadata.docs.rs]
@@ -78,6 +79,7 @@ ustr = { workspace = true }
7879
hypersync-client = { workspace = true, optional = true }
7980
hypersync-schema = { workspace = true, optional = true }
8081
pyo3 = { workspace = true, optional = true }
82+
pyo3-stub-gen = { workspace = true, optional = true }
8183

8284
[dev-dependencies]
8385
rstest = { workspace = true }

crates/adapters/blockchain/src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ use nautilus_model::defi::{DexType, SharedChain};
2222
feature = "python",
2323
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.blockchain")
2424
)]
25+
#[cfg_attr(
26+
feature = "python",
27+
pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.adapters.blockchain")
28+
)]
2529
pub struct DexPoolFilters {
2630
/// Whether to exclude pools containing tokens with empty name or symbol fields.
2731
pub remove_pools_with_empty_erc20fields: bool,
@@ -52,6 +56,10 @@ impl Default for DexPoolFilters {
5256
feature = "python",
5357
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.blockchain")
5458
)]
59+
#[cfg_attr(
60+
feature = "python",
61+
pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.adapters.blockchain")
62+
)]
5563
pub struct BlockchainDataClientConfig {
5664
/// The blockchain chain configuration.
5765
pub chain: SharedChain,

crates/adapters/blockchain/src/factories.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ impl ClientConfig for BlockchainDataClientConfig {
3838
feature = "python",
3939
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.blockchain")
4040
)]
41+
#[cfg_attr(
42+
feature = "python",
43+
pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.adapters.blockchain")
44+
)]
4145
pub struct BlockchainDataClientFactory;
4246

4347
impl BlockchainDataClientFactory {

crates/adapters/blockchain/src/python/config.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use pyo3::prelude::*;
2424
use crate::config::{BlockchainDataClientConfig, DexPoolFilters};
2525

2626
#[pymethods]
27+
#[pyo3_stub_gen::derive::gen_stub_pymethods(module = "nautilus_trader.adapters.blockchain")]
2728
impl DexPoolFilters {
2829
/// Creates a new `DexPoolFilters` instance.
2930
#[new]
@@ -34,13 +35,26 @@ impl DexPoolFilters {
3435
}
3536

3637
#[pymethods]
38+
#[pyo3_stub_gen::derive::gen_stub_pymethods(module = "nautilus_trader.adapters.blockchain")]
3739
impl BlockchainDataClientConfig {
3840
/// Creates a new `BlockchainDataClientConfig` instance.
3941
#[new]
4042
#[allow(clippy::too_many_arguments)]
4143
#[pyo3(signature = (chain, dex_ids, http_rpc_url, rpc_requests_per_second=None, multicall_calls_per_rpc_request=None, wss_rpc_url=None, use_hypersync_for_live_data=true, from_block=None, pool_filters=None, postgres_cache_database_config=None))]
4244
fn py_new(
45+
#[gen_stub(
46+
override_type(
47+
type_repr = "nautilus_trader.model.Chain",
48+
imports = ("nautilus_trader.model",),
49+
),
50+
)]
4351
chain: &Chain,
52+
#[gen_stub(
53+
override_type(
54+
type_repr = "typing.Sequence[nautilus_trader.model.DexType]",
55+
imports = ("typing", "nautilus_trader.model"),
56+
),
57+
)]
4458
dex_ids: Vec<DexType>,
4559
http_rpc_url: String,
4660
rpc_requests_per_second: Option<u32>,
@@ -49,6 +63,12 @@ impl BlockchainDataClientConfig {
4963
use_hypersync_for_live_data: bool,
5064
from_block: Option<u64>,
5165
pool_filters: Option<DexPoolFilters>,
66+
#[gen_stub(
67+
override_type(
68+
type_repr = "typing.Optional[nautilus_trader.infrastructure.PostgresConnectOptions]",
69+
imports = ("typing", "nautilus_trader.infrastructure"),
70+
),
71+
)]
5272
postgres_cache_database_config: Option<PostgresConnectOptions>,
5373
) -> Self {
5474
Self::new(
@@ -67,6 +87,12 @@ impl BlockchainDataClientConfig {
6787

6888
/// Returns the chain configuration.
6989
#[getter]
90+
#[gen_stub(
91+
override_return_type(
92+
type_repr = "nautilus_trader.model.Chain",
93+
imports = ("nautilus_trader.model",),
94+
),
95+
)]
7096
fn chain(&self) -> Chain {
7197
(*self.chain).clone()
7298
}

crates/model/src/defi/chain.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ use strum::{Display, EnumIter, EnumString};
4242
)]
4343
#[non_exhaustive]
4444
#[strum(ascii_case_insensitive)]
45-
#[cfg_attr(
46-
feature = "python",
47-
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
48-
)]
45+
#[cfg_attr(feature = "python", pyo3::pyclass(module = "nautilus_trader.model"))]
46+
#[cfg_attr(feature = "python", pyo3_stub_gen::derive::gen_stub_pyclass_enum)]
4947
pub enum Blockchain {
5048
Abstract,
5149
Arbitrum,
@@ -131,6 +129,7 @@ pub enum Blockchain {
131129

132130
/// Defines a blockchain with its unique identifiers and connection details for network interaction.
133131
#[cfg_attr(feature = "python", pyo3::pyclass(module = "nautilus_pyo3.model"))]
132+
#[cfg_attr(feature = "python", pyo3_stub_gen::derive::gen_stub_pyclass)]
134133
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
135134
pub struct Chain {
136135
/// The blockchain network type.

crates/model/src/defi/dex.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ use crate::{
3838
strum::Display,
3939
strum::EnumIter,
4040
)]
41-
#[cfg_attr(
42-
feature = "python",
43-
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
44-
)]
41+
#[cfg_attr(feature = "python", pyo3::pyclass(module = "nautilus_trader.model"))]
42+
#[cfg_attr(feature = "python", pyo3_stub_gen::derive::gen_stub_pyclass_enum)]
4543
#[non_exhaustive]
4644
pub enum AmmType {
4745
/// Constant Product Automated Market Maker.
@@ -74,10 +72,8 @@ pub enum AmmType {
7472
Serialize,
7573
Deserialize,
7674
)]
77-
#[cfg_attr(
78-
feature = "python",
79-
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
80-
)]
75+
#[cfg_attr(feature = "python", pyo3::pyclass(module = "nautilus_trader.model"))]
76+
#[cfg_attr(feature = "python", pyo3_stub_gen::derive::gen_stub_pyclass_enum)]
8177
pub enum DexType {
8278
AerodromeSlipstream,
8379
AerodromeV1,
@@ -107,10 +103,8 @@ impl DexType {
107103

108104
/// Represents a decentralized exchange (DEX) in a blockchain ecosystem.
109105
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
110-
#[cfg_attr(
111-
feature = "python",
112-
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
113-
)]
106+
#[cfg_attr(feature = "python", pyo3::pyclass(module = "nautilus_trader.model"))]
107+
#[cfg_attr(feature = "python", pyo3_stub_gen::derive::gen_stub_pyclass)]
114108
pub struct Dex {
115109
/// The blockchain network where this DEX operates.
116110
pub chain: Chain,

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ no_strict_optional = false
261261
warn_no_return = true
262262
warn_unused_configs = true
263263
warn_unused_ignores = true
264+
mypy_path = ["."]
265+
exclude = "(^python/examples/|^examples/)"
264266

265267
[[tool.mypy.overrides]]
266268
no_strict_optional = true

0 commit comments

Comments
 (0)