← Back to Pull requests

✨ `spatial.distance.{c,p}dist`: metric-specific overloads #2392

Open
Aniketsy opened this pull request with 131 lines diff across 2 files
View on GitHub
Aniketsy Aniketsy commented on Sep 23, 2026 22:06 UTC
Author

Fixes #404

In this PR we're adding separate overloads so that each metric only accepts the keyword arguments that actually work at runtime:

  • most metrics (euclidean, cosine, cityblock, ...) accept w only

  • minkowski accepts p and w

  • seuclidean accepts V only, and it must be 1-d

  • mahalanobis accepts VI only

  • correlation accepts w, and centered when w is also given

  • jensenshannon accepts no extra keyword arguments

  • a callable metric still accepts any keyword arguments
  • Diff: 131 lines (+108 / -23) across 2 files Reviewer: jorenham Review duration: 14047m
    All Vouch review confidence checks passed
    Residual risk 0.04 = Change Risk (0.34) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.34 · 131 lines diff across 2 files
    0.34
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.85 · 4 substantive comment(s)
    0.85
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.292
    Real PR Conversation Timeline
    5 items
    jorenham jorenham commented on code on Sep 24, 2026 21:59 UTC
    Code Review
    scipy-stubs/spatial/distance.pyi:1
    @@ -61,35 +61,29 @@ type _MetricName = Literal[
         "ha",
         "h",
         "matching",
    -    "minkowski",
    -    "mi",
    -    "m",
    -    "pnorm",
         "jaccard",
         "jacc",
         "ja",
         "j",
    -    "jensenshannon",
    -    "js",
    -    "mahalanobis",
    -    "mahal",
    -    "mah",
         "rogerstanimoto",
         "russellrao",
    -    "seuclidean",
    -    "se",
    -    "s",
         "sokalsneath",
         "sqeuclidean",
         "sqe",
         "sqeuclid",
         "yule",
     ]
    +type _MetricNameCorrelation = Literal["correlation", "co"]
    +type _MetricNameMinkowski = Literal["minkowski", "mi", "m", "pnorm"]
    +type _MetricNameSEuclidean = Literal["seuclidean", "se", "s"]
    +type _MetricNameMahalanobis = Literal["mahalanobis", "mahal", "mah"]
    +type _MetricNameJS = Literal["jensenshannon", "js"]
    +type _MetricNameAll = _MetricName | _MetricNameMinkowski | _MetricNameSEuclidean | _MetricNameMahalanobis | _MetricNameJS

    nit: This alias is only used once (in _Metric), so let's inline it

    jorenham jorenham commented on code on Sep 24, 2026 22:03 UTC
    Code Review
    tests/spatial/test_distance.pyi:1

    nit: I see multiple tests targeting a single overload, which seems a bit redundant; one type-test per overload is fine.

    jorenham jorenham commented on code on Sep 24, 2026 22:04 UTC
    Code Review
    tests/spatial/test_distance.pyi:1

    Also (not a nit this time), seeing as rejecting invalid kwargs is the whole point of #404, we should probably add some some rejection tests, e.g.

    # pyrefly: ignore [no-matching-overload]
    cdist(_f64_2d, _f64_2d, "euclidean", p=3)  # type: ignore[call-overload]  # pyright: ignore[reportArgumentType, reportCallIssue]
    # pyrefly: ignore [no-matching-overload]
    cdist(_f64_2d, _f64_2d, "correlation", centered=False)  # type: ignore[call-overload]  # pyright: ignore[reportArgumentType]
    # pyrefly: ignore [no-matching-overload]
    cdist(_f64_2d, _f64_2d, "seuclidean", w=_f64_1d)  # type: ignore[call-overload]  # pyright: ignore[reportArgumentType, reportCallIssue]
    # pyrefly: ignore [no-matching-overload]
    cdist(_f64_2d, _f64_2d, "jensenshannon", w=_f64_1d)  # type: ignore[call-overload]  # pyright: ignore[reportArgumentType, reportCallIssue]
    

    Aniketsy Aniketsy commented on code on Sep 25, 2026 05:29 UTC
    Code Review
    tests/spatial/test_distance.pyi:1

    Also (not a nit this time), seeing as rejecting invalid kwargs is the whole point of https://github.com/scipy/scipy-stubs/issues/404, we should probably add some some rejection tests, e.g.

    hmm, i agree 👍 , initially i wrote 2 negative tests, but then i was not sure if i need to add these more, so i removed

    i'll update with this , Thanks for the review :)

    Reviewers
    Assignees
    Aniketsy Aniketsy
    Labels
    risk: low confidence: 89.2% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 33.7% (LOW) · Higher = larger blast radius
    33.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.2% · Lower = rushed or shallow review
    89.2%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 3.6% · Safe to merge
    3.6%