fix(solve): monte_carlo_integral 入口收口 + 失败 message 不再被覆盖

三项待办由用户本人改定;Codex 只补 ruff / docstring / tests / 文档。

- bounds 形状校验:`[0, 1]` 从裸 IndexError 变成可读 ValueError(带形状提示);
  空 / 非 list-tuple / 参差不齐一并收口
- 失败 message:改成 if result.success / else 两支,success=False 时不再出现"成功"文案
- n 注解放宽为 int | np.integer;event 注解 Callable[..., bool] -> Callable[..., Any]
  (决策 50 的遗留清零;pyright 复测不再拒绝向量化事件函数)
- 工程侧:清掉 2 行 ruff W293;docstring 的 bounds / Raises 同步;
  非法 bounds 测试 1 类扩到 4 类,新增失败 message 语义与 np.int64 两条
- 验证:199 收集 / 168 passed / 31 skipped / 0 xfail,ruff 0,pyright 0
- docs:决策 52 + 2 条新口径(bounds 收不收 ndarray、if/else 三行重复);CHANGELOG
This commit is contained in:
guzhujushi committed 2026-10-05 16:04:39 +08:00
1 parent ed1f9f0c53
commit dcda080be4
4 files changed
+99 -35

No files matched your search

+24 -2
View File
@@ -51,10 +51,32 @@ def test_monte_carlo_integral_2d_and_params():
def test_monte_carlo_integral_rejects_bad_bounds():
"""四类非法 bounds 都要在入口抛 ValueError:区间反了 / 漏括号 / 空 / 参差不齐。"""
from mcm_toolkit.solve import monte_carlo_integral
with pytest.raises(ValueError):
monte_carlo_integral(lambda X: X[:, 0], bounds=[(1, 0)], n=100, random_state=0)
for bad in ([(1, 0)], [0, 1], [], [(0, 1), (0, 2, 3)]):
with pytest.raises(ValueError):
monte_carlo_integral(lambda X: X[:, 0], bounds=bad, n=100, random_state=0)
def test_monte_carlo_integral_failure_message_stays_failure():
"""失败时 message 不能被包装层改写成"成功"(易错点 22)。"""
from mcm_toolkit.solve import monte_carlo_integral
r = monte_carlo_integral(lambda X: 1.0, bounds=[(0, 1)], n=100, random_state=0)
assert r.success is False
assert "成功" not in r.message
assert "失败" in r.message
assert r.params["dimension"] == 1 # 失败也要留下追溯信息
def test_monte_carlo_integral_accepts_numpy_integer_n():
"""§4.7:`np.int64` 不是 `int` 的子类,注解与运行期都要收(易错点 1)。"""
from mcm_toolkit.solve import monte_carlo_integral
r = monte_carlo_integral(lambda X: X[:, 0], bounds=[(0, 1)], n=np.int64(1000), random_state=0)
assert r.success
assert len(r.data) == 1000
def test_monte_carlo_probability_of_standard_normal():