Skip to content

Commit f17b776

Browse files
committed
Reject non-finite cash reserve settings
1 parent 19b8074 commit f17b776

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

runtime_config_support.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import math
34
import os
45
from dataclasses import dataclass
56
from pathlib import Path
@@ -188,6 +189,8 @@ def _resolve_non_negative_float_env(name: str, *, default: float) -> float:
188189
value = resolve_optional_float_env(os.environ, name)
189190
if value is None:
190191
return float(default)
192+
if not math.isfinite(value):
193+
raise ValueError(f"{name} must be finite, got {value}")
191194
if value < 0:
192195
raise ValueError(f"{name} must be non-negative, got {value}")
193196
return float(value)

tests/test_runtime_config_support.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
from runtime_config_support import (
6+
_resolve_non_negative_float_env,
67
_resolve_ratio_env,
78
_runtime_execution_window_trading_days_env,
89
load_platform_runtime_settings,
@@ -72,6 +73,22 @@ def test_reserved_cash_ratio_rejects_invalid_env(monkeypatch):
7273
_resolve_ratio_env("FIRSTRADE_RESERVED_CASH_RATIO", default=0.0)
7374

7475

76+
@pytest.mark.parametrize("raw_value", ["nan", "inf", "-inf"])
77+
def test_reserved_cash_floor_rejects_non_finite_env(monkeypatch, raw_value):
78+
monkeypatch.setenv("FIRSTRADE_MIN_RESERVED_CASH_USD", raw_value)
79+
80+
with pytest.raises(ValueError, match="FIRSTRADE_MIN_RESERVED_CASH_USD must be finite"):
81+
_resolve_non_negative_float_env("FIRSTRADE_MIN_RESERVED_CASH_USD", default=0.0)
82+
83+
84+
@pytest.mark.parametrize("raw_value", ["nan", "inf", "-inf"])
85+
def test_reserved_cash_ratio_rejects_non_finite_env(monkeypatch, raw_value):
86+
monkeypatch.setenv("FIRSTRADE_RESERVED_CASH_RATIO", raw_value)
87+
88+
with pytest.raises(ValueError, match="FIRSTRADE_RESERVED_CASH_RATIO must be finite"):
89+
_resolve_ratio_env("FIRSTRADE_RESERVED_CASH_RATIO", default=0.0)
90+
91+
7592
@pytest.mark.parametrize("raw_value", ["0", "-1", "abc"])
7693
def test_runtime_execution_window_rejects_invalid_generic_env(monkeypatch, raw_value):
7794
monkeypatch.setenv("FIRSTRADE_RUNTIME_EXECUTION_WINDOW_TRADING_DAYS", raw_value)

0 commit comments

Comments
 (0)