Skip to content

Commit

Permalink
chore: tests cleanup and add from __future__ import annotations (#1206
Browse files Browse the repository at this point in the history
)
  • Loading branch information
FBruzzesi authored Oct 17, 2024
1 parent 086166b commit 80aad57
Show file tree
Hide file tree
Showing 153 changed files with 493 additions and 98 deletions.
2 changes: 2 additions & 0 deletions narwhals/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from narwhals import dependencies
from narwhals import selectors
from narwhals import stable
Expand Down
2 changes: 2 additions & 0 deletions narwhals/stable/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from narwhals.stable import v1

__all__ = ["v1"]
2 changes: 2 additions & 0 deletions narwhals/stable/v1/_dtypes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from narwhals.dtypes import Array
from narwhals.dtypes import Boolean
from narwhals.dtypes import Categorical
Expand Down
2 changes: 2 additions & 0 deletions narwhals/stable/v1/dtypes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from narwhals.stable.v1._dtypes import Array
from narwhals.stable.v1._dtypes import Boolean
from narwhals.stable.v1._dtypes import Categorical
Expand Down
8 changes: 7 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import nox
from nox.sessions import Session

if TYPE_CHECKING:
from nox.sessions import Session

nox.options.default_venv_backend = "uv"
nox.options.reuse_venv = True
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ convention = "google"

[tool.ruff.lint.isort]
force-single-line = true
required-imports = ["from __future__ import annotations"]

[tool.ruff.format]
docstring-code-format = true
Expand Down
15 changes: 11 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import annotations

import contextlib
from typing import TYPE_CHECKING
from typing import Any
from typing import Callable

Expand All @@ -10,10 +13,12 @@
from narwhals.dependencies import get_cudf
from narwhals.dependencies import get_dask_dataframe
from narwhals.dependencies import get_modin
from narwhals.typing import IntoDataFrame
from narwhals.typing import IntoFrame
from narwhals.utils import parse_version
from tests.utils import Constructor

if TYPE_CHECKING:
from narwhals.typing import IntoDataFrame
from narwhals.typing import IntoFrame
from tests.utils import Constructor

with contextlib.suppress(ImportError):
import modin.pandas # noqa: F401
Expand Down Expand Up @@ -108,7 +113,9 @@ def pyarrow_table_constructor(obj: Any) -> IntoDataFrame:


@pytest.fixture(params=eager_constructors)
def constructor_eager(request: pytest.FixtureRequest) -> Callable[[Any], IntoDataFrame]:
def constructor_eager(
request: pytest.FixtureRequest,
) -> Callable[[Any], IntoDataFrame]:
return request.param # type: ignore[no-any-return]


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

import pandas as pd
import polars as pl

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

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
4 changes: 3 additions & 1 deletion tests/expr_and_series/all_horizontal_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

import polars as pl
Expand Down Expand Up @@ -66,7 +68,7 @@ def test_allh_nth(constructor: Constructor, request: pytest.FixtureRequest) -> N
compare_dicts(result, expected)


def test_horizontal_expressions_emtpy(constructor: Constructor) -> None:
def test_horizontal_expressions_empty(constructor: Constructor) -> None:
data = {
"a": [False, False, True],
"b": [False, True, True],
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/any_all_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/any_horizontal_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

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

import pytest

import narwhals.stable.v1 as nw
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/binary_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import compare_dicts
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/clip_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/count_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/cum_sum_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/diff_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pyarrow as pa
import pytest

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

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import compare_dicts
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/double_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import compare_dicts
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/fill_null_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/filter_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

import narwhals.stable.v1 as nw
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/gather_every_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

import narwhals.stable.v1 as nw
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/is_duplicated_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/is_first_distinct_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/is_in_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

import narwhals.stable.v1 as nw
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/is_last_distinct_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/is_null_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/is_unique_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/len_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

import narwhals.stable.v1 as nw
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/max_horizontal_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

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

from typing import Any

import pytest
Expand Down
8 changes: 5 additions & 3 deletions tests/expr_and_series/min_horizontal_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

import pytest
Expand All @@ -7,19 +9,19 @@
from tests.utils import compare_dicts

data = {"a": [1, 3, None, None], "b": [4, None, 6, None], "z": [3, 1, None, None]}
expcted_values = [1, 1, 6, float("nan")]
expected_values = [1, 1, 6, float("nan")]


@pytest.mark.parametrize("col_expr", [nw.col("a"), "a"])
def test_minh(constructor: Constructor, col_expr: Any) -> None:
df = nw.from_native(constructor(data))
result = df.select(horizontal_min=nw.min_horizontal(col_expr, nw.col("b"), "z"))
expected = {"horizontal_min": expcted_values}
expected = {"horizontal_min": expected_values}
compare_dicts(result, expected)


def test_minh_all(constructor: Constructor) -> None:
df = nw.from_native(constructor(data))
result = df.select(nw.min_horizontal(nw.all()), c=nw.min_horizontal(nw.all()))
expected = {"a": expcted_values, "c": expcted_values}
expected = {"a": expected_values, "c": expected_values}
compare_dicts(result, expected)
2 changes: 2 additions & 0 deletions tests/expr_and_series/mode_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import polars as pl
import pytest

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

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/null_count_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/over_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from contextlib import nullcontext as does_not_raise

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

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/sample_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

import narwhals.stable.v1 as nw
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/shift_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pyarrow as pa

import narwhals.stable.v1 as nw
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/sort_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

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

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/str/contains_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pandas as pd
import polars as pl
import pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/str/head_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/str/len_chars_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/str/tail_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/sum_horizontal_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

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

import pytest

import narwhals as nw
Expand Down
19 changes: 8 additions & 11 deletions tests/expr_and_series/unary_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
Expand All @@ -6,17 +8,12 @@

def test_unary(constructor: Constructor) -> None:
data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
result = (
nw.from_native(constructor(data))
.with_columns(
a_mean=nw.col("a").mean(),
a_sum=nw.col("a").sum(),
b_nunique=nw.col("b").n_unique(),
z_min=nw.col("z").min(),
z_max=nw.col("z").max(),
)
.unique(["a_mean", "a_sum", "b_nunique", "z_min", "z_max"])
.select(["a_mean", "a_sum", "b_nunique", "z_min", "z_max"])
result = nw.from_native(constructor(data)).select(
a_mean=nw.col("a").mean(),
a_sum=nw.col("a").sum(),
b_nunique=nw.col("b").n_unique(),
z_min=nw.col("z").min(),
z_max=nw.col("z").max(),
)
expected = {
"a_mean": [2],
Expand Down
2 changes: 2 additions & 0 deletions tests/expr_and_series/unique_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

import narwhals.stable.v1 as nw
Expand Down
2 changes: 2 additions & 0 deletions tests/frame/add_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import compare_dicts
Expand Down
Loading

0 comments on commit 80aad57

Please sign in to comment.