Skip to content

TYP: accept scalar-returning IRR selectors - #176

Open
pengpengyi92 wants to merge 1 commit into
numpy:mainfrom
pengpengyi92:fix/irr-selector-scalar-typing-20261112
Open

pengpengyi92 wants to merge 1 commit into
numpy:mainfrom
pengpengyi92:fix/irr-selector-scalar-typing-20261112

Conversation

@pengpengyi92

Copy link
Copy Markdown

Problem

The public irr(..., selection_logic=...) documentation says a custom selector receives a one-dimensional array of candidate rates and returns a number. The implementation assigns the selected value to one scalar entry in irr_results.

However, _SelectionFunc currently requires an NDArray[np.float64] return, so mypy rejects valid selectors that return float, np.float64 or np.float32. The default selector is also annotated as returning an array despite returning one indexed element.

For example, this documented use is rejected:

import numpy as np
import numpy.typing as npt
import numpy_financial as npf

def select_rate(candidates: npt.NDArray[np.float64]) -> float:
    return float(candidates[0])

npf.irr([-5, 10.5, 1, -8, 1], selection_logic=select_rate)

Change

  • Type the callback result as float | np.floating[Any] and the default selector result as np.float64.
  • Add typing_tests/irr_selection.py, included by the existing repository-wide mypy . CI command. It covers the default selector, three supported scalar-returning callbacks and rejection of an array-valued callback. The last case uses a narrow type: ignore[arg-type]; the existing warn_unused_ignores setting makes the fixture fail if array selectors become accepted again.
  • No IRR root computation, selection policy, sign filtering or numerical function body changes.

Verification

Base: 031ed55d322b7aad57311b9d63b589c79df942a1.

  • The identical fixture on that unmodified commit: 4 mypy errors (three valid callbacks rejected, plus unused ignore for the wrongly accepted array-valued callback).
  • Patched fixture: no mypy errors.
  • python -m mypy --no-incremental --cache-dir <temporary-path> .: success, 5 source files checked.
  • python -m pyright --pythonpath <isolated-python> --pythonversion 3.13 numpy_financial typing_tests/irr_selection.py: 0 errors / 0 warnings.
  • Ruff check and git diff --check: pass on the two changed files.
  • AST comparison against the pinned base confirms every function is unchanged after removing return annotations. This is an additional scope check, not a substitute for numerical tests.
  • Tooling: mypy 2.4.1, Pyright 1.1.414, NumPy 2.5.4; checks hosted on Python 3.12.13. The package requires Python 3.13+ for its native build. The native runtime test suite/build was not run locally; this PR does not claim those results.

AI assistance: OpenAI Codex assisted with reproduction, the two annotation changes and the static contract fixture.

@jorenham
jorenham self-requested a review November 12, 2026 09:35

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant