Skip to content

Commit b3389ed

Browse files
committed
Refine PyO3 types and attributes
- Apply frozen attribute to Price, Quantity, Money, Currency, Balance - Add frozen, eq, eq_int, hash to 29 enums (removes 108 manual impls) - Preserve cross-type comparisons via manual __richcmp__ methods - Replace __setstate__ with 2-tuple pickle protocol for frozen types - Convert CurrencyType enum to string in Currency pickle
1 parent 5b6fa62 commit b3389ed

File tree

12 files changed

+342
-393
lines changed

12 files changed

+342
-393
lines changed

crates/model/src/enums.rs

Lines changed: 203 additions & 29 deletions
Large diffs are not rendered by default.

crates/model/src/python/enums.rs

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ impl AccountType {
3939
Self::py_from_str(&t, value)
4040
}
4141

42-
fn __hash__(&self) -> isize {
43-
*self as isize
44-
}
45-
4642
fn __repr__(&self) -> String {
4743
format!(
4844
"<{}.{}: '{}'>",
@@ -108,10 +104,6 @@ impl AggregationSource {
108104
Self::py_from_str(&t, value)
109105
}
110106

111-
fn __hash__(&self) -> isize {
112-
*self as isize
113-
}
114-
115107
fn __repr__(&self) -> String {
116108
format!(
117109
"<{}.{}: '{}'>",
@@ -171,10 +163,6 @@ impl AggressorSide {
171163
Self::py_from_str(&t, value)
172164
}
173165

174-
fn __hash__(&self) -> isize {
175-
*self as isize
176-
}
177-
178166
fn __repr__(&self) -> String {
179167
format!(
180168
"<{}.{}: '{}'>",
@@ -240,10 +228,6 @@ impl AssetClass {
240228
Self::py_from_str(&t, value)
241229
}
242230

243-
fn __hash__(&self) -> isize {
244-
*self as isize
245-
}
246-
247231
fn __repr__(&self) -> String {
248232
format!(
249233
"<{}.{}: '{}'>",
@@ -333,10 +317,6 @@ impl InstrumentClass {
333317
Self::py_from_str(&t, value)
334318
}
335319

336-
fn __hash__(&self) -> isize {
337-
*self as isize
338-
}
339-
340320
fn __repr__(&self) -> String {
341321
format!(
342322
"<{}.{}: '{}'>",
@@ -438,10 +418,6 @@ impl BarAggregation {
438418
Self::py_from_str(&t, value)
439419
}
440420

441-
fn __hash__(&self) -> isize {
442-
*self as isize
443-
}
444-
445421
fn __repr__(&self) -> String {
446422
format!(
447423
"<{}.{}: '{}'>",
@@ -584,10 +560,6 @@ impl BetSide {
584560
Self::py_from_str(&t, value)
585561
}
586562

587-
fn __hash__(&self) -> isize {
588-
*self as isize
589-
}
590-
591563
fn __repr__(&self) -> String {
592564
format!(
593565
"<{}.{}: '{}'>",
@@ -658,10 +630,6 @@ impl BookAction {
658630
Self::py_from_str(&t, value)
659631
}
660632

661-
fn __hash__(&self) -> isize {
662-
*self as isize
663-
}
664-
665633
fn __repr__(&self) -> String {
666634
format!(
667635
"<{}.{}: '{}'>",
@@ -733,10 +701,6 @@ impl ContingencyType {
733701
Self::py_from_str(&t, value)
734702
}
735703

736-
fn __hash__(&self) -> isize {
737-
*self as isize
738-
}
739-
740704
fn __repr__(&self) -> String {
741705
format!(
742706
"<{}.{}: '{}'>",
@@ -808,10 +772,6 @@ impl CurrencyType {
808772
Self::py_from_str(&t, value)
809773
}
810774

811-
fn __hash__(&self) -> isize {
812-
*self as isize
813-
}
814-
815775
fn __repr__(&self) -> String {
816776
format!(
817777
"<{}.{}: '{}'>",
@@ -877,10 +837,6 @@ impl InstrumentCloseType {
877837
Self::py_from_str(&t, value)
878838
}
879839

880-
fn __hash__(&self) -> isize {
881-
*self as isize
882-
}
883-
884840
fn __repr__(&self) -> String {
885841
format!(
886842
"<{}.{}: '{}'>",
@@ -940,10 +896,6 @@ impl LiquiditySide {
940896
Self::py_from_str(&t, value)
941897
}
942898

943-
fn __hash__(&self) -> isize {
944-
*self as isize
945-
}
946-
947899
fn __repr__(&self) -> String {
948900
format!(
949901
"<{}.{}: '{}'>",
@@ -1009,10 +961,6 @@ impl MarketStatus {
1009961
Self::py_from_str(&t, value)
1010962
}
1011963

1012-
fn __hash__(&self) -> isize {
1013-
*self as isize
1014-
}
1015-
1016964
fn __repr__(&self) -> String {
1017965
format!(
1018966
"<{}.{}: '{}'>",
@@ -1097,10 +1045,6 @@ impl MarketStatusAction {
10971045
Self::py_from_str(&t, value)
10981046
}
10991047

1100-
fn __hash__(&self) -> isize {
1101-
*self as isize
1102-
}
1103-
11041048
fn __repr__(&self) -> String {
11051049
format!(
11061050
"<{}.{}: '{}'>",
@@ -1244,10 +1188,6 @@ impl OmsType {
12441188
Self::py_from_str(&t, value)
12451189
}
12461190

1247-
fn __hash__(&self) -> isize {
1248-
*self as isize
1249-
}
1250-
12511191
fn __repr__(&self) -> String {
12521192
format!(
12531193
"<{}.{}: '{}'>",
@@ -1313,10 +1253,6 @@ impl OptionKind {
13131253
Self::py_from_str(&t, value)
13141254
}
13151255

1316-
fn __hash__(&self) -> isize {
1317-
*self as isize
1318-
}
1319-
13201256
fn __repr__(&self) -> String {
13211257
format!(
13221258
"<{}.{}: '{}'>",
@@ -1376,10 +1312,6 @@ impl OrderSide {
13761312
Self::py_from_str(&t, value)
13771313
}
13781314

1379-
fn __hash__(&self) -> isize {
1380-
*self as isize
1381-
}
1382-
13831315
fn __repr__(&self) -> String {
13841316
format!(
13851317
"<{}.{}: '{}'>",
@@ -1445,10 +1377,6 @@ impl OrderStatus {
14451377
Self::py_from_str(&t, value)
14461378
}
14471379

1448-
fn __hash__(&self) -> isize {
1449-
*self as isize
1450-
}
1451-
14521380
fn __repr__(&self) -> String {
14531381
format!(
14541382
"<{}.{}: '{}'>",
@@ -1580,10 +1508,6 @@ impl OrderType {
15801508
Self::py_from_str(&t, value)
15811509
}
15821510

1583-
fn __hash__(&self) -> isize {
1584-
*self as isize
1585-
}
1586-
15871511
fn __repr__(&self) -> String {
15881512
format!(
15891513
"<{}.{}: '{}'>",
@@ -1685,10 +1609,6 @@ impl PositionSide {
16851609
Self::py_from_str(&t, value)
16861610
}
16871611

1688-
fn __hash__(&self) -> isize {
1689-
*self as isize
1690-
}
1691-
16921612
fn __repr__(&self) -> String {
16931613
format!(
16941614
"<{}.{}: '{}'>",
@@ -1760,10 +1680,6 @@ impl PriceType {
17601680
Self::py_from_str(&t, value)
17611681
}
17621682

1763-
fn __hash__(&self) -> isize {
1764-
*self as isize
1765-
}
1766-
17671683
fn __repr__(&self) -> String {
17681684
format!(
17691685
"<{}.{}: '{}'>",
@@ -1848,10 +1764,6 @@ impl RecordFlag {
18481764
Self::py_from_str(&t, value)
18491765
}
18501766

1851-
fn __hash__(&self) -> isize {
1852-
*self as isize
1853-
}
1854-
18551767
fn __repr__(&self) -> String {
18561768
format!(
18571769
"<{}.{}: '{}'>",
@@ -1928,10 +1840,6 @@ impl TimeInForce {
19281840
Self::py_from_str(&t, value)
19291841
}
19301842

1931-
fn __hash__(&self) -> isize {
1932-
*self as isize
1933-
}
1934-
19351843
fn __repr__(&self) -> String {
19361844
format!(
19371845
"<{}.{}: '{}'>",
@@ -2021,10 +1929,6 @@ impl TrailingOffsetType {
20211929
Self::py_from_str(&t, value)
20221930
}
20231931

2024-
fn __hash__(&self) -> isize {
2025-
*self as isize
2026-
}
2027-
20281932
fn __repr__(&self) -> String {
20291933
format!(
20301934
"<{}.{}: '{}'>",
@@ -2102,10 +2006,6 @@ impl TriggerType {
21022006
Self::py_from_str(&t, value)
21032007
}
21042008

2105-
fn __hash__(&self) -> isize {
2106-
*self as isize
2107-
}
2108-
21092009
fn __repr__(&self) -> String {
21102010
format!(
21112011
"<{}.{}: '{}'>",
@@ -2213,10 +2113,6 @@ impl BookType {
22132113
Self::py_from_str(&t, value)
22142114
}
22152115

2216-
fn __hash__(&self) -> isize {
2217-
*self as isize
2218-
}
2219-
22202116
fn __repr__(&self) -> String {
22212117
format!(
22222118
"<{}.{}: '{}'>",
@@ -2282,10 +2178,6 @@ impl TradingState {
22822178
Self::py_from_str(&t, value)
22832179
}
22842180

2285-
fn __hash__(&self) -> isize {
2286-
*self as isize
2287-
}
2288-
22892181
fn __repr__(&self) -> String {
22902182
format!(
22912183
"<{}.{}: '{}'>",

crates/model/src/python/types/balance.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515

1616
use std::str::FromStr;
1717

18-
use nautilus_core::python::{
19-
IntoPyObjectNautilusExt, parsing::get_required_string, to_pyvalue_err,
20-
};
21-
use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
18+
use nautilus_core::python::{parsing::get_required_string, to_pyvalue_err};
19+
use pyo3::{prelude::*, types::PyDict};
2220

2321
use crate::{
2422
identifiers::InstrumentId,
@@ -32,14 +30,6 @@ impl AccountBalance {
3230
Self::new_checked(total, locked, free).map_err(to_pyvalue_err)
3331
}
3432

35-
fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
36-
match op {
37-
CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
38-
CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
39-
_ => py.NotImplemented(),
40-
}
41-
}
42-
4333
fn __repr__(&self) -> String {
4434
format!("{self:?}")
4535
}
@@ -120,13 +110,6 @@ impl MarginBalance {
120110
fn py_new(initial: Money, maintenance: Money, instrument: InstrumentId) -> Self {
121111
Self::new(initial, maintenance, instrument)
122112
}
123-
fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
124-
match op {
125-
CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
126-
CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
127-
_ => py.NotImplemented(),
128-
}
129-
}
130113

131114
fn __repr__(&self) -> String {
132115
format!("{self:?}")

0 commit comments

Comments
 (0)