Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions numpy_financial/_financial.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ def rate(
return rn


def _irr_default_selection(eirr: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
def _irr_default_selection(eirr: npt.NDArray[np.float64]) -> np.float64:
""" default selection logic for IRR function when there are > 1 real solutions """
# check sign of all IRR solutions
same_sign = np.all(eirr > 0) if eirr[0] > 0 else np.all(eirr < 0)
Expand All @@ -1029,7 +1029,7 @@ def _irr_default_selection(eirr: npt.NDArray[np.float64]) -> npt.NDArray[np.floa

_SelectionFunc: TypeAlias = Callable[
[npt.NDArray[np.float64]],
npt.NDArray[np.float64],
float | np.floating[Any],
]


Expand Down
29 changes: 29 additions & 0 deletions typing_tests/irr_selection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Static IRR selector contract, checked by the repository-wide mypy job."""
import numpy as np
import numpy.typing as npt

import numpy_financial as npf


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


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


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


def select_array(candidates: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
return candidates


cashflows = [-5, 10.5, 1, -8, 1]
default_result: float = npf.irr(cashflows)
python_result: float = npf.irr(cashflows, selection_logic=select_python_float)
numpy_result: float = npf.irr(cashflows, selection_logic=select_numpy_float)
float32_result: float = npf.irr(cashflows, selection_logic=select_numpy_float32)
npf.irr(cashflows, selection_logic=select_array) # type: ignore[arg-type]