nautechsystems/nautilus_trader · warning

effective raw scale should fit in PriceRaw

Error message

effective raw scale should fit in PriceRaw

What it means

Price's Python __int__ divides the internal raw fixed-precision integer by the scale for the price's precision, computed via PriceRaw::try_from and expected to always fit. It panics only when the precision implies a scale outside PriceRaw's range, i.e. an invalid price precision outside supported bounds (0..=9 in this codebase).

Source

Thrown at crates/model/src/python/types/price.rs:390

            )))
        }
    }

    fn __neg__(&self) -> Self {
        -*self
    }

    fn __pos__(&self) -> Self {
        *self
    }

    fn __abs__(&self) -> Self {
        if self.raw < 0 { -*self } else { *self }
    }

    fn __int__(&self) -> PriceRaw {
        let scale = PriceRaw::try_from(raw_scale(self.precision))
            .expect("effective raw scale should fit in PriceRaw");
        self.raw / scale
    }

    fn __float__(&self) -> f64 {
        self.as_f64()
    }

    #[pyo3(signature = (ndigits=None))]
    fn __round__(&self, ndigits: Option<u32>) -> Decimal {
        self.as_decimal()
            .round_dp_with_strategy(ndigits.unwrap_or(0), RoundingStrategy::MidpointNearestEven)
    }

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

    fn __str__(&self) -> String {

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Validate precision is within supported range before constructing Price (use Price::new / try_from_raw with checked precision)
  2. Avoid int() on suspect Price values; use float(price) / as_f64 instead
  3. Fix the upstream precision source rather than the conversion site

Example fix

// before
ticks = int(price)
// after
assert 0 <= price.precision <= 9, "invalid price precision"
ticks = int(price)
Defensive patterns

Strategy: type-guard

Validate before calling

# Python
assert 0 <= price.precision <= 9, "invalid price precision"
ticks = int(price)

Type guard

def safe_int(p: Price) -> int | None:
    return int(p) if 0 <= p.precision <= 9 else None

Try / catch

try:
    ticks = int(price)
except Exception:
    ticks = None  # fall back to float(price)

Prevention

When it happens

Trigger: Calling int(price) on a Price constructed with an out-of-bounds precision so raw_scale(precision) doesn't fit PriceRaw.

Common situations: Building Price from untrusted venue metadata with extreme precision values; pyo3/FFI hand-constructed prices bypassing validation; test fixtures with fabricated precisions.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/8941e63e7a31b481. Report an issue: GitHub.