Skip to content

Commit

Permalink
Add initial Python type stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Oct 22, 2023
1 parent d717fe3 commit 1521b52
Show file tree
Hide file tree
Showing 50 changed files with 961 additions and 284 deletions.
4 changes: 2 additions & 2 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _build_rust_libs() -> None:
)
except subprocess.CalledProcessError as e:
raise RuntimeError(
f"Error running cargo: {e.stderr.decode()}",
f"Error running cargo: {e}",
) from e


Expand Down Expand Up @@ -297,7 +297,7 @@ def _strip_unneeded_symbols() -> None:
capture_output=True,
)
except subprocess.CalledProcessError as e:
raise RuntimeError(f"Error when stripping symbols.\n{e.stderr.decode()}") from e
raise RuntimeError(f"Error when stripping symbols.\n{e}") from e


def build() -> None:
Expand Down
69 changes: 42 additions & 27 deletions nautilus_core/pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

use std::str::FromStr;

use pyo3::{prelude::*, types::PyDict};
use pyo3::{
prelude::*,
types::{PyDict, PyString},
};
use tracing::Level;
use tracing_appender::{
non_blocking::WorkerGuard,
Expand Down Expand Up @@ -93,54 +96,66 @@ pub fn set_global_log_collector(
/// Need to modify sys modules so that submodule can be loaded directly as
/// import supermodule.submodule
///
/// Also re-exports all submodule attributes so they can be imported directly from `nautilus_pyo3`.
/// refer: https://github.com/PyO3/pyo3/issues/2644
#[pymodule]
fn nautilus_pyo3(py: Python<'_>, m: &PyModule) -> PyResult<()> {
let sys = PyModule::import(py, "sys")?;
let sys_modules: &PyDict = sys.getattr("modules")?.downcast()?;
let module_name = "nautilus_trader.core.nautilus_pyo3";

// Set pyo3_nautilus to be recognized as a subpackage
sys_modules.set_item(module_name, m)?;

m.add_class::<LogGuard>()?;
m.add_function(wrap_pyfunction!(set_global_log_collector, m)?)?;

// Core
let n = "core";
let submodule = pyo3::wrap_pymodule!(nautilus_core::python::core);
m.add_wrapped(submodule)?;
sys_modules.set_item(
"nautilus_trader.core.nautilus_pyo3.core",
m.getattr("core")?,
)?;

// Indicators
let submodule = pyo3::wrap_pymodule!(nautilus_indicators::indicators);
m.add_wrapped(submodule)?;
sys_modules.set_item(
"nautilus_trader.core.nautilus_pyo3.indicators",
m.getattr("indicators")?,
)?;
sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
re_export_module_attributes(m, n)?;

// Model
let n = "model";
let submodule = pyo3::wrap_pymodule!(nautilus_model::python::model);
m.add_wrapped(submodule)?;
sys_modules.set_item(
"nautilus_trader.core.nautilus_pyo3.model",
m.getattr("model")?,
)?;
sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
re_export_module_attributes(m, n)?;

// Indicators
let n = "indicators";
let submodule = pyo3::wrap_pymodule!(nautilus_indicators::indicators);
m.add_wrapped(submodule)?;
sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
re_export_module_attributes(m, n)?;

// Network
let n = "network";
let submodule = pyo3::wrap_pymodule!(nautilus_network::network);
m.add_wrapped(submodule)?;
sys_modules.set_item(
"nautilus_trader.core.nautilus_pyo3.network",
m.getattr("network")?,
)?;
sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
re_export_module_attributes(m, n)?;

// Persistence
let n = "persistence";
let submodule = pyo3::wrap_pymodule!(nautilus_persistence::python::persistence);
m.add_wrapped(submodule)?;
sys_modules.set_item(
"nautilus_trader.core.nautilus_pyo3.persistence",
m.getattr("persistence")?,
)?;
sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
re_export_module_attributes(m, n)?;

m.add_class::<LogGuard>()?;
m.add_function(wrap_pyfunction!(set_global_log_collector, m)?)?;
Ok(())
}

fn re_export_module_attributes(parent_module: &PyModule, submodule_name: &str) -> PyResult<()> {
let submodule = parent_module.getattr(submodule_name)?;
for item in submodule.dir() {
let item_name: &PyString = item.extract()?;
if let Ok(attr) = submodule.getattr(item_name) {
parent_module.add(item_name.to_str()?, attr)?;
}
}

Ok(())
}
6 changes: 3 additions & 3 deletions nautilus_trader/adapters/betfair/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@

from nautilus_trader.common.logging import Logger
from nautilus_trader.common.logging import LoggerAdapter
from nautilus_trader.core.nautilus_pyo3.network import HttpClient
from nautilus_trader.core.nautilus_pyo3.network import HttpMethod
from nautilus_trader.core.nautilus_pyo3.network import HttpResponse
from nautilus_trader.core.nautilus_pyo3 import HttpClient
from nautilus_trader.core.nautilus_pyo3 import HttpMethod
from nautilus_trader.core.nautilus_pyo3 import HttpResponse
from nautilus_trader.core.rust.common import LogColor


Expand Down
6 changes: 4 additions & 2 deletions nautilus_trader/adapters/betfair/sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from nautilus_trader.adapters.betfair.client import BetfairHttpClient
from nautilus_trader.common.logging import Logger
from nautilus_trader.common.logging import LoggerAdapter
from nautilus_trader.core.nautilus_pyo3.network import SocketClient
from nautilus_trader.core.nautilus_pyo3.network import SocketConfig
from nautilus_trader.core.nautilus_pyo3 import SocketClient
from nautilus_trader.core.nautilus_pyo3 import SocketConfig


HOST = "stream-api.betfair.com"
Expand Down Expand Up @@ -123,6 +123,8 @@ async def post_disconnection(self) -> None:

async def send(self, message: bytes):
self._log.debug(f"[SEND] {message.decode()}")
if self._client is None:
raise RuntimeError("Cannot send message: no client.")
await self._client.send(message)
self._log.debug("[SENT]")

Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/binance/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from nautilus_trader.common.clock import LiveClock
from nautilus_trader.common.logging import Logger
from nautilus_trader.config import InstrumentProviderConfig
from nautilus_trader.core.nautilus_pyo3.network import Quota
from nautilus_trader.core.nautilus_pyo3 import Quota
from nautilus_trader.live.factories import LiveDataClientFactory
from nautilus_trader.live.factories import LiveExecClientFactory
from nautilus_trader.msgbus.bus import MessageBus
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/binance/futures/http/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from nautilus_trader.adapters.binance.http.client import BinanceHttpClient
from nautilus_trader.adapters.binance.http.endpoint import BinanceHttpEndpoint
from nautilus_trader.common.clock import LiveClock
from nautilus_trader.core.nautilus_pyo3.network import HttpMethod
from nautilus_trader.core.nautilus_pyo3 import HttpMethod


class BinanceFuturesPositionModeHttp(BinanceHttpEndpoint):
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/binance/futures/http/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from nautilus_trader.adapters.binance.http.client import BinanceHttpClient
from nautilus_trader.adapters.binance.http.endpoint import BinanceHttpEndpoint
from nautilus_trader.adapters.binance.http.market import BinanceMarketHttpAPI
from nautilus_trader.core.nautilus_pyo3.network import HttpMethod
from nautilus_trader.core.nautilus_pyo3 import HttpMethod


class BinanceFuturesExchangeInfoHttp(BinanceHttpEndpoint):
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/binance/futures/http/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from nautilus_trader.adapters.binance.http.client import BinanceHttpClient
from nautilus_trader.adapters.binance.http.endpoint import BinanceHttpEndpoint
from nautilus_trader.common.clock import LiveClock
from nautilus_trader.core.nautilus_pyo3.network import HttpMethod
from nautilus_trader.core.nautilus_pyo3 import HttpMethod


class BinanceFuturesCommissionRateHttp(BinanceHttpEndpoint):
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/binance/http/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from nautilus_trader.adapters.binance.http.endpoint import BinanceHttpEndpoint
from nautilus_trader.common.clock import LiveClock
from nautilus_trader.core.correctness import PyCondition
from nautilus_trader.core.nautilus_pyo3.network import HttpMethod
from nautilus_trader.core.nautilus_pyo3 import HttpMethod


class BinanceOrderHttp(BinanceHttpEndpoint):
Expand Down
8 changes: 4 additions & 4 deletions nautilus_trader/adapters/binance/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
from nautilus_trader.common.clock import LiveClock
from nautilus_trader.common.logging import Logger
from nautilus_trader.common.logging import LoggerAdapter
from nautilus_trader.core.nautilus_pyo3.network import HttpClient
from nautilus_trader.core.nautilus_pyo3.network import HttpMethod
from nautilus_trader.core.nautilus_pyo3.network import HttpResponse
from nautilus_trader.core.nautilus_pyo3.network import Quota
from nautilus_trader.core.nautilus_pyo3 import HttpClient
from nautilus_trader.core.nautilus_pyo3 import HttpMethod
from nautilus_trader.core.nautilus_pyo3 import HttpResponse
from nautilus_trader.core.nautilus_pyo3 import Quota


class BinanceHttpClient:
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/binance/http/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from nautilus_trader.adapters.binance.common.schemas.symbol import BinanceSymbol
from nautilus_trader.adapters.binance.common.schemas.symbol import BinanceSymbols
from nautilus_trader.adapters.binance.http.client import BinanceHttpClient
from nautilus_trader.core.nautilus_pyo3.network import HttpMethod
from nautilus_trader.core.nautilus_pyo3 import HttpMethod


def enc_hook(obj: Any) -> Any:
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/binance/http/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from nautilus_trader.adapters.binance.http.endpoint import BinanceHttpEndpoint
from nautilus_trader.core.correctness import PyCondition
from nautilus_trader.core.datetime import nanos_to_millis
from nautilus_trader.core.nautilus_pyo3.network import HttpMethod
from nautilus_trader.core.nautilus_pyo3 import HttpMethod
from nautilus_trader.model.data import BarType
from nautilus_trader.model.data import OrderBookDeltas
from nautilus_trader.model.data import TradeTick
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/binance/http/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from nautilus_trader.adapters.binance.http.client import BinanceHttpClient
from nautilus_trader.adapters.binance.http.endpoint import BinanceHttpEndpoint
from nautilus_trader.core.correctness import PyCondition
from nautilus_trader.core.nautilus_pyo3.network import HttpMethod
from nautilus_trader.core.nautilus_pyo3 import HttpMethod


class BinanceListenKeyHttp(BinanceHttpEndpoint):
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/binance/spot/http/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from nautilus_trader.adapters.binance.spot.schemas.account import BinanceSpotAccountInfo
from nautilus_trader.adapters.binance.spot.schemas.account import BinanceSpotOrderOco
from nautilus_trader.common.clock import LiveClock
from nautilus_trader.core.nautilus_pyo3.network import HttpMethod
from nautilus_trader.core.nautilus_pyo3 import HttpMethod


class BinanceSpotOpenOrdersHttp(BinanceOpenOrdersHttp):
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/binance/spot/http/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from nautilus_trader.adapters.binance.spot.enums import BinanceSpotPermissions
from nautilus_trader.adapters.binance.spot.schemas.market import BinanceSpotAvgPrice
from nautilus_trader.adapters.binance.spot.schemas.market import BinanceSpotExchangeInfo
from nautilus_trader.core.nautilus_pyo3.network import HttpMethod
from nautilus_trader.core.nautilus_pyo3 import HttpMethod


class BinanceSpotExchangeInfoHttp(BinanceHttpEndpoint):
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/binance/spot/http/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from nautilus_trader.adapters.binance.http.endpoint import BinanceHttpEndpoint
from nautilus_trader.adapters.binance.spot.schemas.wallet import BinanceSpotTradeFee
from nautilus_trader.common.clock import LiveClock
from nautilus_trader.core.nautilus_pyo3.network import HttpMethod
from nautilus_trader.core.nautilus_pyo3 import HttpMethod


class BinanceSpotTradeFeeHttp(BinanceHttpEndpoint):
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/binance/websocket/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from nautilus_trader.common.enums import LogColor
from nautilus_trader.common.logging import Logger
from nautilus_trader.common.logging import LoggerAdapter
from nautilus_trader.core.nautilus_pyo3.network import WebSocketClient
from nautilus_trader.core.nautilus_pyo3 import WebSocketClient


class BinanceWebSocketClient:
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/backtest/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from nautilus_trader.config import BacktestVenueConfig
from nautilus_trader.core.correctness import PyCondition
from nautilus_trader.core.inspect import is_nautilus_class
from nautilus_trader.core.nautilus_pyo3.persistence import DataBackendSession
from nautilus_trader.core.nautilus_pyo3 import DataBackendSession
from nautilus_trader.model.currency import Currency
from nautilus_trader.model.data import Bar
from nautilus_trader.model.data.base import capsule_to_list
Expand Down
7 changes: 0 additions & 7 deletions nautilus_trader/core/datetime.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ from cpython.datetime cimport datetime
from libc.stdint cimport uint64_t


cpdef uint64_t secs_to_nanos(double seconds)
cpdef uint64_t secs_to_millis(double secs)
cpdef uint64_t millis_to_nanos(double millis)
cpdef uint64_t micros_to_nanos(double micros)
cpdef double nanos_to_secs(uint64_t nanos)
cpdef uint64_t nanos_to_millis(uint64_t nanos)
cpdef uint64_t nanos_to_micros(uint64_t nanos)
cpdef unix_nanos_to_dt(uint64_t nanos)
cpdef dt_to_unix_nanos(dt: pd.Timestamp)
cpdef maybe_unix_nanos_to_dt(nanos)
Expand Down
Loading

0 comments on commit 1521b52

Please sign in to comment.