Skip to content

Commit

Permalink
Improve typing
Browse files Browse the repository at this point in the history
  • Loading branch information
dustalov committed Sep 6, 2024
1 parent b1f6346 commit 8c6e24b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion python/evalica/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from collections.abc import Collection, Hashable
from dataclasses import dataclass
from types import MappingProxyType
from typing import Generic, Literal, Protocol, TypeVar
from typing import Generic, Literal, Protocol, TypeVar, runtime_checkable

import numpy as np
import numpy.typing as npt
Expand Down Expand Up @@ -158,6 +158,7 @@ def matrices(
)


@runtime_checkable
class Result(Protocol[T]):
"""
The result protocol.
Expand Down
9 changes: 9 additions & 0 deletions python/evalica/test_evalica.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,12 @@ def test_counting(comparison: Comparison, win_weight: float, tie_weight: float)
solver="naive",
)


for result in (result_pyo3, result_naive):
assert len(result.scores) == len(set(xs) | set(ys))
assert np.isfinite(result.scores).all()
assert result.scores.is_monotonic_decreasing
assert isinstance(result, evalica.Result)

assert_series_equal(result_pyo3.scores, result_naive.scores, check_like=True)

Expand Down Expand Up @@ -158,6 +160,7 @@ def test_average_win_rate(comparison: Comparison, win_weight: float, tie_weight:
assert len(result.scores) == len(set(xs) | set(ys))
assert np.isfinite(result.scores).all()
assert result.scores.is_monotonic_decreasing
assert isinstance(result, evalica.Result)

assert_series_equal(result_pyo3.scores, result_naive.scores, rtol=1e-4, check_like=True)

Expand Down Expand Up @@ -188,6 +191,7 @@ def test_bradley_terry(comparison: Comparison, win_weight: float, tie_weight: fl
assert result.scores.is_monotonic_decreasing
assert not xs or result.iterations > 0
assert result.limit > 0
assert isinstance(result, evalica.Result)

assert_series_equal(result_pyo3.scores, result_naive.scores, check_like=True)

Expand Down Expand Up @@ -223,6 +227,8 @@ def test_newman(comparison: Comparison, v_init: float) -> None:
else:
assert result.v_init is v_init

assert isinstance(result, evalica.Result)

assert_series_equal(result_pyo3.scores, result_naive.scores, check_like=True)

assert not np.isfinite(v_init) or result_pyo3.v == pytest.approx(result_naive.v)
Expand Down Expand Up @@ -276,6 +282,7 @@ def test_elo(
assert len(result.scores) == len(set(xs) | set(ys))
assert np.isfinite(result.scores).all()
assert result.scores.is_monotonic_decreasing
assert isinstance(result, evalica.Result)

assert_series_equal(result_pyo3.scores, result_naive.scores, check_like=True)

Expand Down Expand Up @@ -306,6 +313,7 @@ def test_eigen(comparison: Comparison, win_weight: float, tie_weight: float) ->
assert result.scores.is_monotonic_decreasing
assert not xs or result.iterations > 0
assert result.limit > 0
assert isinstance(result, evalica.Result)

assert_series_equal(result_pyo3.scores, result_naive.scores, check_like=True)

Expand Down Expand Up @@ -346,6 +354,7 @@ def test_pagerank(comparison: Comparison, damping: float, win_weight: float, tie
assert np.isfinite(result.tie_weight)
assert not xs or result.iterations > 0
assert result.limit > 0
assert isinstance(result, evalica.Result)

assert_series_equal(result_pyo3.scores, result_naive.scores, check_like=True)

Expand Down

0 comments on commit 8c6e24b

Please sign in to comment.