Skip to content

Silence scipy RuntimeWarning (invalid value in multiply) in annual-breakdown stats path #839

Description

@tschm

Summary

make test runs green at 100% coverage, but the suite emits 2 RuntimeWarning: invalid value encountered in multiply from scipy. They originate from a stats computation reaching a scipy distribution call with a degenerate (NaN/empty/constant) input — a code path that is exercised but not explicitly handled or asserted.

Evidence

From make test output:

tests/test_jquantstats/test_migration/test_new_stats.py::test_annual_breakdown_metrics_match_summary_set
  .venv/lib/python3.12/site-packages/scipy/stats/_distn_infrastructure.py:2334: RuntimeWarning: invalid value encountered in multiply
  .venv/lib/python3.12/site-packages/scipy/stats/_distn_infrastructure.py:2335: RuntimeWarning: invalid value encountered in multiply

The triggering test is tests/test_jquantstats/test_migration/test_new_stats.py::test_annual_breakdown_metrics_match_summary_set. The relevant production code is in src/jquantstats/_stats/ (the annual-breakdown / summary stats path; see src/jquantstats/_stats/_reporting.py:704 annual_breakdown and the metric helpers it feeds). Note _montecarlo.py already wraps scipy calls in np.errstate(invalid="ignore", ...) (lines 146, 205) — this path does not.

Why it matters

A RuntimeWarning escaping the suite signals an untested edge case (degenerate input reaching scipy). Either the input should be guarded before the scipy call, or the condition is expected and should be made explicit rather than tolerated silently.

Proposed fix

  • Identify the degenerate input (empty / constant / NaN return series) reaching the scipy distribution call.
  • Either guard it explicitly before the call (preferred, mirroring the np.errstate pattern in _montecarlo.py), or document it as expected and scope a targeted filterwarnings.
  • Add an explicit test for the edge case.

Acceptance criteria

  • make test completes with zero RuntimeWarnings.
  • The degenerate-input edge case has an explicit, named test.
  • Coverage stays at 100%.

Filed from a /rhiza_quality assessment (coverage & depth subcategory, 9→10).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions