Skip to content

Commit

Permalink
Make the code compatible with Python 3.8+
Browse files Browse the repository at this point in the history
  • Loading branch information
dustalov committed Jul 9, 2024
1 parent 097584c commit 32184d2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
19 changes: 12 additions & 7 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
from collections.abc import Callable
from __future__ import annotations

from pathlib import Path
from typing import Any, NamedTuple, cast
from typing import TYPE_CHECKING, Any, NamedTuple, cast

import evalica
import numpy as np
import numpy.typing as npt
import pandas as pd
import pytest
from _pytest.fixtures import TopRequest
from hypothesis import strategies as st

if TYPE_CHECKING:
from collections.abc import Callable

from _pytest.fixtures import TopRequest


class Example(NamedTuple):
"""A tuple holding example data."""

xs: "list[str] | pd.Series[str]"
ys: "list[str] | pd.Series[str]"
ws: "list[evalica.Winner] | pd.Series[evalica.Winner]" # type: ignore[type-var]
xs: list[str] | pd.Series[str]
ys: list[str] | pd.Series[str]
ws: list[evalica.Winner] | pd.Series[evalica.Winner] # type: ignore[type-var]


@st.composite
Expand Down Expand Up @@ -151,7 +156,7 @@ def example(request: TopRequest, dataset: str) -> Example:


@pytest.fixture()
def example_golden(request: TopRequest, dataset: str, algorithm: str) -> "pd.Series[str]":
def example_golden(request: TopRequest, dataset: str, algorithm: str) -> pd.Series[str]:
assert dataset in DATASETS, f"unknown dataset: {dataset}"

df_golden = cast(pd.DataFrame, request.getfixturevalue(f"{dataset}_golden"))
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ ruff = [

[tool.ruff]
line-length = 120
target-version = "py312"
target-version = "py38"

[tool.ruff.lint]
select = ["ALL"]
Expand Down
26 changes: 14 additions & 12 deletions python/evalica/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import dataclasses
from collections import OrderedDict
from collections.abc import Hashable, Iterable
Expand Down Expand Up @@ -45,7 +47,7 @@ def enumerate_elements(xs: Iterable[T], *yss: Iterable[T]) -> dict[T, int]:

@dataclass
class IndexedElements(Generic[T]):
index: "pd.Index[T]" # type: ignore[type-var]
index: pd.Index[T] # type: ignore[type-var]
xs: list[int]
ys: list[int]

Expand All @@ -67,7 +69,7 @@ def index_elements(xs: Iterable[T], ys: Iterable[T]) -> IndexedElements[T]:
class MatricesResult(Generic[T]):
win_matrix: npt.NDArray[np.int64]
tie_matrix: npt.NDArray[np.int64]
index: "pd.Index[T]" # type: ignore[type-var]
index: pd.Index[T] # type: ignore[type-var]


def matrices(
Expand All @@ -88,7 +90,7 @@ def matrices(

@dataclass(frozen=True)
class CountingResult(Generic[T]):
scores: "pd.Series[T]" # type: ignore[type-var]
scores: pd.Series[T] # type: ignore[type-var]
win_weight: float
tie_weight: float

Expand All @@ -113,9 +115,9 @@ def counting(

@dataclass(frozen=True)
class BradleyTerryResult(Generic[T]):
scores: "pd.Series[T]" # type: ignore[type-var]
scores: pd.Series[T] # type: ignore[type-var]
matrix: npt.NDArray[np.float64]
index: "pd.Index[T]" # type: ignore[type-var]
index: pd.Index[T] # type: ignore[type-var]
win_weight: float
tie_weight: float
solver: str
Expand Down Expand Up @@ -158,10 +160,10 @@ def bradley_terry(

@dataclass(frozen=True)
class NewmanResult(Generic[T]):
scores: "pd.Series[T]" # type: ignore[type-var]
scores: pd.Series[T] # type: ignore[type-var]
win_matrix: npt.NDArray[np.float64]
tie_matrix: npt.NDArray[np.float64]
index: "pd.Index[T]" # type: ignore[type-var]
index: pd.Index[T] # type: ignore[type-var]
v: float
v_init: float
solver: str
Expand Down Expand Up @@ -205,7 +207,7 @@ def newman(

@dataclass(frozen=True)
class EloResult(Generic[T]):
scores: "pd.Series[T]" # type: ignore[type-var]
scores: pd.Series[T] # type: ignore[type-var]
initial: float
base: float
scale: float
Expand Down Expand Up @@ -236,9 +238,9 @@ def elo(

@dataclass(frozen=True)
class EigenResult(Generic[T]):
scores: "pd.Series[T]" # type: ignore[type-var]
scores: pd.Series[T] # type: ignore[type-var]
matrix: npt.NDArray[np.float64]
index: "pd.Index[T]" # type: ignore[type-var]
index: pd.Index[T] # type: ignore[type-var]
win_weight: float
tie_weight: float
solver: str
Expand Down Expand Up @@ -279,7 +281,7 @@ def eigen(

@dataclass(frozen=True)
class PageRankResult(Generic[T]):
scores: "pd.Series[T]" # type: ignore[type-var]
scores: pd.Series[T] # type: ignore[type-var]
damping: float
win_weight: float
tie_weight: float
Expand Down Expand Up @@ -315,7 +317,7 @@ def _pairwise_ndarray(scores: npt.NDArray[np.float64]) -> npt.NDArray[np.float64
return scores[:, np.newaxis] / (scores + scores[:, np.newaxis])


def pairwise(scores: "pd.Series[T]") -> npt.NDArray[np.float64]: # type: ignore[type-var]
def pairwise(scores: pd.Series[T]) -> npt.NDArray[np.float64]: # type: ignore[type-var]
scores = scores.sort_values(ascending=False)
return _pairwise_ndarray(scores.to_numpy())

Expand Down
2 changes: 2 additions & 0 deletions python/evalica/naive.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import numpy as np
import numpy.typing as npt

Expand Down

0 comments on commit 32184d2

Please sign in to comment.