From 82b1d6e64471a3016aa68f06ee147f4fc502411e Mon Sep 17 00:00:00 2001 From: Luciano <66913960+lucianosrp@users.noreply.github.com> Date: Thu, 17 Oct 2024 12:56:22 +0200 Subject: [PATCH] feat: add ConstructorEager type (#1091) --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> --- tests/expr_and_series/abs_test.py | 5 ++- tests/expr_and_series/all_horizontal_test.py | 3 +- tests/expr_and_series/any_all_test.py | 5 ++- tests/expr_and_series/arg_true_test.py | 5 ++- tests/expr_and_series/arithmetic_test.py | 7 ++-- .../cat/get_categories_test.py | 7 ++-- tests/expr_and_series/clip_test.py | 5 ++- .../expr_and_series/convert_time_zone_test.py | 11 +++++-- tests/expr_and_series/count_test.py | 5 ++- tests/expr_and_series/cum_sum_test.py | 5 ++- tests/expr_and_series/diff_test.py | 7 ++-- tests/expr_and_series/drop_nulls_test.py | 5 ++- .../dt/datetime_attributes_test.py | 6 ++-- .../dt/datetime_duration_test.py | 4 +-- tests/expr_and_series/dt/to_string_test.py | 7 ++-- tests/expr_and_series/fill_null_test.py | 5 ++- tests/expr_and_series/filter_test.py | 5 ++- tests/expr_and_series/gather_every_test.py | 7 ++-- tests/expr_and_series/head_test.py | 5 ++- tests/expr_and_series/is_between_test.py | 5 ++- tests/expr_and_series/is_duplicated_test.py | 5 ++- .../expr_and_series/is_first_distinct_test.py | 5 ++- tests/expr_and_series/is_in_test.py | 5 ++- .../expr_and_series/is_last_distinct_test.py | 5 ++- tests/expr_and_series/is_null_test.py | 5 ++- tests/expr_and_series/is_unique_test.py | 5 ++- tests/expr_and_series/len_test.py | 5 ++- tests/expr_and_series/max_test.py | 7 ++-- tests/expr_and_series/mean_test.py | 7 ++-- tests/expr_and_series/min_test.py | 7 ++-- tests/expr_and_series/mode_test.py | 5 ++- tests/expr_and_series/n_unique_test.py | 5 ++- tests/expr_and_series/null_count_test.py | 5 ++- tests/expr_and_series/operators_test.py | 9 +++-- tests/expr_and_series/pipe_test.py | 5 ++- tests/expr_and_series/quantile_test.py | 4 +-- .../expr_and_series/replace_time_zone_test.py | 11 +++++-- tests/expr_and_series/round_test.py | 5 ++- tests/expr_and_series/shift_test.py | 5 ++- tests/expr_and_series/sort_test.py | 16 ++++----- tests/expr_and_series/std_test.py | 5 ++- tests/expr_and_series/str/contains_test.py | 7 ++-- tests/expr_and_series/str/head_test.py | 5 ++- tests/expr_and_series/str/len_chars_test.py | 5 ++- tests/expr_and_series/str/replace_test.py | 7 ++-- tests/expr_and_series/str/slice_test.py | 3 +- .../str/starts_with_ends_with_test.py | 7 ++-- tests/expr_and_series/str/strip_chars_test.py | 3 +- tests/expr_and_series/str/tail_test.py | 5 ++- tests/expr_and_series/str/to_datetime_test.py | 12 ++++--- .../str/to_uppercase_to_lowercase_test.py | 7 ++-- tests/expr_and_series/sum_test.py | 7 ++-- tests/expr_and_series/tail_test.py | 5 ++- tests/expr_and_series/unary_test.py | 5 ++- tests/expr_and_series/unique_test.py | 5 ++- tests/expr_and_series/when_test.py | 7 ++-- tests/frame/array_dunder_test.py | 11 ++++--- tests/frame/get_column_test.py | 5 ++- tests/frame/getitem_test.py | 33 +++++++++---------- tests/frame/is_duplicated_test.py | 5 ++- tests/frame/is_empty_test.py | 8 ++++- tests/frame/is_unique_test.py | 5 ++- tests/frame/item_test.py | 11 +++++-- tests/frame/lazy_test.py | 5 ++- tests/frame/len_test.py | 6 ++-- tests/frame/null_count_test.py | 5 ++- tests/frame/row_test.py | 3 +- tests/frame/rows_test.py | 6 +++- tests/frame/schema_test.py | 5 ++- tests/frame/shape_test.py | 5 ++- tests/frame/to_arrow_test.py | 9 +++-- tests/frame/to_dict_test.py | 7 ++-- tests/frame/to_native_test.py | 5 ++- tests/frame/to_numpy_test.py | 7 ++-- tests/frame/to_pandas_test.py | 9 +++-- tests/frame/write_csv_test.py | 6 ++-- tests/frame/write_parquet_test.py | 9 +++-- tests/group_by_test.py | 4 +-- tests/new_series_test.py | 7 ++-- tests/series_only/__iter___test.py | 9 +++-- tests/series_only/array_dunder_test.py | 11 ++++--- tests/series_only/dtype_test.py | 7 ++-- tests/series_only/is_empty_test.py | 5 ++- .../is_ordered_categorical_test.py | 5 ++- tests/series_only/is_sorted_test.py | 7 ++-- tests/series_only/item_test.py | 4 +-- tests/series_only/scatter_test.py | 11 ++++--- tests/series_only/shape_test.py | 5 ++- tests/series_only/slice_test.py | 5 ++- tests/series_only/to_arrow_test.py | 9 +++-- tests/series_only/to_dummy_test.py | 7 ++-- tests/series_only/to_frame_test.py | 5 ++- tests/series_only/to_list_test.py | 7 ++-- tests/series_only/to_native_test.py | 9 +++-- tests/series_only/to_numpy_test.py | 9 +++-- tests/series_only/to_pandas_test.py | 9 +++-- tests/series_only/value_counts_test.py | 3 +- tests/series_only/zip_with_test.py | 7 ++-- tests/translate/to_native_test.py | 3 +- tests/utils.py | 4 ++- 100 files changed, 348 insertions(+), 304 deletions(-) diff --git a/tests/expr_and_series/abs_test.py b/tests/expr_and_series/abs_test.py index 286bcca19..c883d7161 100644 --- a/tests/expr_and_series/abs_test.py +++ b/tests/expr_and_series/abs_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -12,7 +11,7 @@ def test_abs(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_abs_series(constructor_eager: Any) -> None: +def test_abs_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager({"a": [1, 2, 3, -4, 5]}), eager_only=True) result = {"b": df["a"].abs()} expected = {"b": [1, 2, 3, 4, 5]} diff --git a/tests/expr_and_series/all_horizontal_test.py b/tests/expr_and_series/all_horizontal_test.py index 01d53fe63..a5ba44600 100644 --- a/tests/expr_and_series/all_horizontal_test.py +++ b/tests/expr_and_series/all_horizontal_test.py @@ -6,6 +6,7 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -23,7 +24,7 @@ def test_allh(constructor: Constructor, expr1: Any, expr2: Any) -> None: compare_dicts(result, expected) -def test_allh_series(constructor_eager: Any) -> None: +def test_allh_series(constructor_eager: ConstructorEager) -> None: data = { "a": [False, False, True], "b": [False, True, True], diff --git a/tests/expr_and_series/any_all_test.py b/tests/expr_and_series/any_all_test.py index 834a91202..73294c708 100644 --- a/tests/expr_and_series/any_all_test.py +++ b/tests/expr_and_series/any_all_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -23,7 +22,7 @@ def test_any_all(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_any_all_series(constructor_eager: Any) -> None: +def test_any_all_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native( constructor_eager( { diff --git a/tests/expr_and_series/arg_true_test.py b/tests/expr_and_series/arg_true_test.py index 7e1262aa8..1f71e2c42 100644 --- a/tests/expr_and_series/arg_true_test.py +++ b/tests/expr_and_series/arg_true_test.py @@ -1,9 +1,8 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -16,7 +15,7 @@ def test_arg_true(constructor: Constructor, request: pytest.FixtureRequest) -> N compare_dicts(result, expected) -def test_arg_true_series(constructor_eager: Any) -> None: +def test_arg_true_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager({"a": [1, None, None, 3]}), eager_only=True) result = df.select(df["a"].is_null().arg_true()) expected = {"a": [1, 2]} diff --git a/tests/expr_and_series/arithmetic_test.py b/tests/expr_and_series/arithmetic_test.py index e431aebbe..eb283667f 100644 --- a/tests/expr_and_series/arithmetic_test.py +++ b/tests/expr_and_series/arithmetic_test.py @@ -13,6 +13,7 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -94,7 +95,7 @@ def test_arithmetic_series( attr: str, rhs: Any, expected: list[Any], - constructor_eager: Any, + constructor_eager: ConstructorEager, request: pytest.FixtureRequest, ) -> None: if attr == "__mod__" and any( @@ -124,7 +125,7 @@ def test_right_arithmetic_series( attr: str, rhs: Any, expected: list[Any], - constructor_eager: Any, + constructor_eager: ConstructorEager, request: pytest.FixtureRequest, ) -> None: if attr == "__rmod__" and any( @@ -139,7 +140,7 @@ def test_right_arithmetic_series( def test_truediv_same_dims( - constructor_eager: Any, request: pytest.FixtureRequest + constructor_eager: ConstructorEager, request: pytest.FixtureRequest ) -> None: if "polars" in str(constructor_eager): # https://github.com/pola-rs/polars/issues/17760 diff --git a/tests/expr_and_series/cat/get_categories_test.py b/tests/expr_and_series/cat/get_categories_test.py index 122f3c83e..11ba3ee58 100644 --- a/tests/expr_and_series/cat/get_categories_test.py +++ b/tests/expr_and_series/cat/get_categories_test.py @@ -1,18 +1,19 @@ from __future__ import annotations -from typing import Any - import pyarrow as pa import pytest import narwhals.stable.v1 as nw from narwhals.utils import parse_version +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": ["one", "two", "two"]} -def test_get_categories(request: pytest.FixtureRequest, constructor_eager: Any) -> None: +def test_get_categories( + request: pytest.FixtureRequest, constructor_eager: ConstructorEager +) -> None: if "pyarrow_table" in str(constructor_eager) and parse_version( pa.__version__ ) < parse_version("15.0.0"): diff --git a/tests/expr_and_series/clip_test.py b/tests/expr_and_series/clip_test.py index d3f90633c..2406f289f 100644 --- a/tests/expr_and_series/clip_test.py +++ b/tests/expr_and_series/clip_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -20,7 +19,7 @@ def test_clip(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_clip_series(constructor_eager: Any) -> None: +def test_clip_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager({"a": [1, 2, 3, -4, 5]}), eager_only=True) result = { "lower_only": df["a"].clip(lower_bound=3), diff --git a/tests/expr_and_series/convert_time_zone_test.py b/tests/expr_and_series/convert_time_zone_test.py index d85637a14..7914c8b56 100644 --- a/tests/expr_and_series/convert_time_zone_test.py +++ b/tests/expr_and_series/convert_time_zone_test.py @@ -1,6 +1,8 @@ +from __future__ import annotations + from datetime import datetime from datetime import timezone -from typing import Any +from typing import TYPE_CHECKING import pandas as pd import polars as pl @@ -13,6 +15,9 @@ from tests.utils import compare_dicts from tests.utils import is_windows +if TYPE_CHECKING: + from tests.utils import ConstructorEager + def test_convert_time_zone( constructor: Constructor, request: pytest.FixtureRequest @@ -43,7 +48,7 @@ def test_convert_time_zone( def test_convert_time_zone_series( - constructor_eager: Any, request: pytest.FixtureRequest + constructor_eager: ConstructorEager, request: pytest.FixtureRequest ) -> None: if ( (any(x in str(constructor_eager) for x in ("pyarrow", "modin")) and is_windows()) @@ -116,7 +121,7 @@ def test_convert_time_zone_to_none(constructor: Constructor) -> None: df.select(nw.col("a").dt.convert_time_zone(None)) # type: ignore[arg-type] -def test_convert_time_zone_to_none_series(constructor_eager: Any) -> None: +def test_convert_time_zone_to_none_series(constructor_eager: ConstructorEager) -> None: data = { "a": [ datetime(2020, 1, 1, tzinfo=timezone.utc), diff --git a/tests/expr_and_series/count_test.py b/tests/expr_and_series/count_test.py index 580bd202b..ec90e1fc1 100644 --- a/tests/expr_and_series/count_test.py +++ b/tests/expr_and_series/count_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -13,7 +12,7 @@ def test_count(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_count_series(constructor_eager: Any) -> None: +def test_count_series(constructor_eager: ConstructorEager) -> None: data = {"a": [1, 3, 2], "b": [4, None, 6], "z": [7.0, None, None]} df = nw.from_native(constructor_eager(data), eager_only=True) result = {"a": [df["a"].count()], "b": [df["b"].count()], "z": [df["z"].count()]} diff --git a/tests/expr_and_series/cum_sum_test.py b/tests/expr_and_series/cum_sum_test.py index 94897a850..a490b890e 100644 --- a/tests/expr_and_series/cum_sum_test.py +++ b/tests/expr_and_series/cum_sum_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -22,7 +21,7 @@ def test_cum_sum_simple(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_cum_sum_simple_series(constructor_eager: Any) -> None: +def test_cum_sum_simple_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) expected = { "a": [0, 1, 3, 6, 10], diff --git a/tests/expr_and_series/diff_test.py b/tests/expr_and_series/diff_test.py index 33445f763..ada3147ed 100644 --- a/tests/expr_and_series/diff_test.py +++ b/tests/expr_and_series/diff_test.py @@ -1,11 +1,10 @@ -from typing import Any - import pyarrow as pa import pytest import narwhals.stable.v1 as nw from narwhals.utils import parse_version from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -32,7 +31,9 @@ def test_diff(constructor: Constructor, request: pytest.FixtureRequest) -> None: compare_dicts(result, expected) -def test_diff_series(constructor_eager: Any, request: pytest.FixtureRequest) -> None: +def test_diff_series( + constructor_eager: ConstructorEager, request: pytest.FixtureRequest +) -> None: if "pyarrow_table_constructor" in str(constructor_eager) and parse_version( pa.__version__ ) < (13,): diff --git a/tests/expr_and_series/drop_nulls_test.py b/tests/expr_and_series/drop_nulls_test.py index bc06eec3a..4b15416ce 100644 --- a/tests/expr_and_series/drop_nulls_test.py +++ b/tests/expr_and_series/drop_nulls_test.py @@ -1,11 +1,10 @@ from __future__ import annotations -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -36,7 +35,7 @@ def test_drop_nulls(constructor: Constructor, request: pytest.FixtureRequest) -> compare_dicts(result_d, expected_d) -def test_drop_nulls_series(constructor_eager: Any) -> None: +def test_drop_nulls_series(constructor_eager: ConstructorEager) -> None: data = { "A": [1, 2, None, 4], "B": [5, 6, 7, 8], diff --git a/tests/expr_and_series/dt/datetime_attributes_test.py b/tests/expr_and_series/dt/datetime_attributes_test.py index 5b9519f57..757d226ff 100644 --- a/tests/expr_and_series/dt/datetime_attributes_test.py +++ b/tests/expr_and_series/dt/datetime_attributes_test.py @@ -2,12 +2,12 @@ from datetime import date from datetime import datetime -from typing import Any import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -72,7 +72,7 @@ def test_datetime_attributes( ) def test_datetime_attributes_series( request: pytest.FixtureRequest, - constructor_eager: Any, + constructor_eager: ConstructorEager, attribute: str, expected: list[int], ) -> None: @@ -91,7 +91,7 @@ def test_datetime_attributes_series( def test_datetime_chained_attributes( - request: pytest.FixtureRequest, constructor_eager: Any + request: pytest.FixtureRequest, constructor_eager: ConstructorEager ) -> None: if "pandas" in str(constructor_eager) and "pyarrow" not in str(constructor_eager): request.applymarker(pytest.mark.xfail) diff --git a/tests/expr_and_series/dt/datetime_duration_test.py b/tests/expr_and_series/dt/datetime_duration_test.py index da5ff325b..3e4894b0b 100644 --- a/tests/expr_and_series/dt/datetime_duration_test.py +++ b/tests/expr_and_series/dt/datetime_duration_test.py @@ -1,7 +1,6 @@ from __future__ import annotations from datetime import timedelta -from typing import Any import numpy as np import pandas as pd @@ -12,6 +11,7 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -74,7 +74,7 @@ def test_duration_attributes( ) def test_duration_attributes_series( request: pytest.FixtureRequest, - constructor_eager: Any, + constructor_eager: ConstructorEager, attribute: str, expected_a: list[int], expected_b: list[int], diff --git a/tests/expr_and_series/dt/to_string_test.py b/tests/expr_and_series/dt/to_string_test.py index 6017c33d2..a6261b78a 100644 --- a/tests/expr_and_series/dt/to_string_test.py +++ b/tests/expr_and_series/dt/to_string_test.py @@ -7,6 +7,7 @@ import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts from tests.utils import is_windows @@ -29,7 +30,7 @@ ], ) @pytest.mark.skipif(is_windows(), reason="pyarrow breaking on windows") -def test_dt_to_string_series(constructor_eager: Any, fmt: str) -> None: +def test_dt_to_string_series(constructor_eager: ConstructorEager, fmt: str) -> None: input_frame = nw.from_native(constructor_eager(data), eager_only=True) input_series = input_frame["a"] @@ -100,7 +101,7 @@ def _clean_string_expr(e: Any) -> Any: ) @pytest.mark.skipif(is_windows(), reason="pyarrow breaking on windows") def test_dt_to_string_iso_local_datetime_series( - constructor_eager: Any, data: datetime, expected: str + constructor_eager: ConstructorEager, data: datetime, expected: str ) -> None: df = constructor_eager({"a": [data]}) result = ( @@ -152,7 +153,7 @@ def test_dt_to_string_iso_local_datetime_expr( ) @pytest.mark.skipif(is_windows(), reason="pyarrow breaking on windows") def test_dt_to_string_iso_local_date_series( - constructor_eager: Any, data: datetime, expected: str + constructor_eager: ConstructorEager, data: datetime, expected: str ) -> None: df = constructor_eager({"a": [data]}) result = nw.from_native(df, eager_only=True)["a"].dt.to_string("%Y-%m-%d").item(0) diff --git a/tests/expr_and_series/fill_null_test.py b/tests/expr_and_series/fill_null_test.py index 6efde5ac0..9fa7afaf9 100644 --- a/tests/expr_and_series/fill_null_test.py +++ b/tests/expr_and_series/fill_null_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -23,7 +22,7 @@ def test_fill_null(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_fill_null_series(constructor_eager: Any) -> None: +def test_fill_null_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) expected = { diff --git a/tests/expr_and_series/filter_test.py b/tests/expr_and_series/filter_test.py index 80267d1d0..dff987ecb 100644 --- a/tests/expr_and_series/filter_test.py +++ b/tests/expr_and_series/filter_test.py @@ -1,9 +1,8 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -23,7 +22,7 @@ def test_filter(constructor: Constructor, request: pytest.FixtureRequest) -> Non compare_dicts(result, expected) -def test_filter_series(constructor_eager: Any) -> None: +def test_filter_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) result = df.select(df["a"].filter((df["i"] < 2) & (df["c"] == 5))) expected = {"a": [0]} diff --git a/tests/expr_and_series/gather_every_test.py b/tests/expr_and_series/gather_every_test.py index e01294ef9..e6f68be1d 100644 --- a/tests/expr_and_series/gather_every_test.py +++ b/tests/expr_and_series/gather_every_test.py @@ -1,9 +1,8 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": list(range(10))} @@ -26,7 +25,9 @@ def test_gather_every_expr( @pytest.mark.parametrize("n", [1, 2, 3]) @pytest.mark.parametrize("offset", [1, 2, 3]) -def test_gather_every_series(constructor_eager: Any, n: int, offset: int) -> None: +def test_gather_every_series( + constructor_eager: ConstructorEager, n: int, offset: int +) -> None: series = nw.from_native(constructor_eager(data), eager_only=True)["a"] result = series.gather_every(n=n, offset=offset) diff --git a/tests/expr_and_series/head_test.py b/tests/expr_and_series/head_test.py index 2a6326921..4c750fabf 100644 --- a/tests/expr_and_series/head_test.py +++ b/tests/expr_and_series/head_test.py @@ -1,11 +1,10 @@ from __future__ import annotations -from typing import Any - import pytest import narwhals as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -22,7 +21,7 @@ def test_head(constructor: Constructor, n: int, request: pytest.FixtureRequest) @pytest.mark.parametrize("n", [2, -1]) -def test_head_series(constructor_eager: Any, n: int) -> None: +def test_head_series(constructor_eager: ConstructorEager, n: int) -> None: df = nw.from_native(constructor_eager({"a": [1, 2, 3]}), eager_only=True) result = df.select(df["a"].head(n)) expected = {"a": [1, 2]} diff --git a/tests/expr_and_series/is_between_test.py b/tests/expr_and_series/is_between_test.py index 0a9e578ea..0550498b6 100644 --- a/tests/expr_and_series/is_between_test.py +++ b/tests/expr_and_series/is_between_test.py @@ -1,11 +1,10 @@ from __future__ import annotations -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -39,7 +38,7 @@ def test_is_between(constructor: Constructor, closed: str, expected: list[bool]) ], ) def test_is_between_series( - constructor_eager: Any, closed: str, expected: list[bool] + constructor_eager: ConstructorEager, closed: str, expected: list[bool] ) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) result = df.with_columns(a=df["a"].is_between(1, 5, closed=closed)) diff --git a/tests/expr_and_series/is_duplicated_test.py b/tests/expr_and_series/is_duplicated_test.py index 7859aed02..d0c5ae3dc 100644 --- a/tests/expr_and_series/is_duplicated_test.py +++ b/tests/expr_and_series/is_duplicated_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": [1, 1, 2], "b": [1, 2, 3], "index": [0, 1, 2]} @@ -14,7 +13,7 @@ def test_is_duplicated_expr(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_is_duplicated_series(constructor_eager: Any) -> None: +def test_is_duplicated_series(constructor_eager: ConstructorEager) -> None: series = nw.from_native(constructor_eager(data), eager_only=True)["a"] result = series.is_duplicated() expected = {"a": [True, True, False]} diff --git a/tests/expr_and_series/is_first_distinct_test.py b/tests/expr_and_series/is_first_distinct_test.py index 93ffc5d37..4f22d02f9 100644 --- a/tests/expr_and_series/is_first_distinct_test.py +++ b/tests/expr_and_series/is_first_distinct_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -20,7 +19,7 @@ def test_is_first_distinct_expr(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_is_first_distinct_series(constructor_eager: Any) -> None: +def test_is_first_distinct_series(constructor_eager: ConstructorEager) -> None: series = nw.from_native(constructor_eager(data), eager_only=True)["a"] result = series.is_first_distinct() expected = { diff --git a/tests/expr_and_series/is_in_test.py b/tests/expr_and_series/is_in_test.py index 085b1efbe..29d3cf56b 100644 --- a/tests/expr_and_series/is_in_test.py +++ b/tests/expr_and_series/is_in_test.py @@ -1,9 +1,8 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": [1, 4, 2, 5]} @@ -17,7 +16,7 @@ def test_expr_is_in(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_ser_is_in(constructor_eager: Any) -> None: +def test_ser_is_in(constructor_eager: ConstructorEager) -> None: ser = nw.from_native(constructor_eager(data), eager_only=True)["a"] result = {"a": ser.is_in([4, 5])} expected = {"a": [False, True, False, True]} diff --git a/tests/expr_and_series/is_last_distinct_test.py b/tests/expr_and_series/is_last_distinct_test.py index 00db7f735..e63c161b3 100644 --- a/tests/expr_and_series/is_last_distinct_test.py +++ b/tests/expr_and_series/is_last_distinct_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -20,7 +19,7 @@ def test_is_last_distinct_expr(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_is_last_distinct_series(constructor_eager: Any) -> None: +def test_is_last_distinct_series(constructor_eager: ConstructorEager) -> None: series = nw.from_native(constructor_eager(data), eager_only=True)["a"] result = series.is_last_distinct() expected = { diff --git a/tests/expr_and_series/is_null_test.py b/tests/expr_and_series/is_null_test.py index 85ba55dc4..a3d5d2bae 100644 --- a/tests/expr_and_series/is_null_test.py +++ b/tests/expr_and_series/is_null_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -14,7 +13,7 @@ def test_null(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_null_series(constructor_eager: Any) -> None: +def test_null_series(constructor_eager: ConstructorEager) -> None: data_na = {"a": [None, 3, 2], "z": [7.0, None, None]} expected = {"a": [True, False, False], "z": [True, False, False]} df = nw.from_native(constructor_eager(data_na), eager_only=True) diff --git a/tests/expr_and_series/is_unique_test.py b/tests/expr_and_series/is_unique_test.py index b10f7a68f..8d46db92d 100644 --- a/tests/expr_and_series/is_unique_test.py +++ b/tests/expr_and_series/is_unique_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -22,7 +21,7 @@ def test_is_unique_expr(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_is_unique_series(constructor_eager: Any) -> None: +def test_is_unique_series(constructor_eager: ConstructorEager) -> None: series = nw.from_native(constructor_eager(data), eager_only=True)["a"] result = series.is_unique() expected = { diff --git a/tests/expr_and_series/len_test.py b/tests/expr_and_series/len_test.py index b1e1674bf..535c7dc92 100644 --- a/tests/expr_and_series/len_test.py +++ b/tests/expr_and_series/len_test.py @@ -1,9 +1,8 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -46,7 +45,7 @@ def test_namespace_len(constructor: Constructor) -> None: compare_dicts(df, expected) -def test_len_series(constructor_eager: Any) -> None: +def test_len_series(constructor_eager: ConstructorEager) -> None: data = {"a": [1, 2, 1]} s = nw.from_native(constructor_eager(data), eager_only=True)["a"] diff --git a/tests/expr_and_series/max_test.py b/tests/expr_and_series/max_test.py index 1ea32531e..dcacc7d2e 100644 --- a/tests/expr_and_series/max_test.py +++ b/tests/expr_and_series/max_test.py @@ -1,11 +1,10 @@ from __future__ import annotations -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} @@ -20,7 +19,9 @@ def test_expr_max_expr(constructor: Constructor, expr: nw.Expr) -> None: @pytest.mark.parametrize(("col", "expected"), [("a", 3), ("b", 6), ("z", 9.0)]) -def test_expr_max_series(constructor_eager: Any, col: str, expected: float) -> None: +def test_expr_max_series( + constructor_eager: ConstructorEager, col: str, expected: float +) -> None: series = nw.from_native(constructor_eager(data), eager_only=True)[col] result = series.max() compare_dicts({col: [result]}, {col: [expected]}) diff --git a/tests/expr_and_series/mean_test.py b/tests/expr_and_series/mean_test.py index 50e6fd862..0d381286a 100644 --- a/tests/expr_and_series/mean_test.py +++ b/tests/expr_and_series/mean_test.py @@ -1,11 +1,10 @@ from __future__ import annotations -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": [1, 3, 2], "b": [4, 4, 7], "z": [7.0, 8, 9]} @@ -20,7 +19,9 @@ def test_expr_mean_expr(constructor: Constructor, expr: nw.Expr) -> None: @pytest.mark.parametrize(("col", "expected"), [("a", 2.0), ("b", 5.0), ("z", 8.0)]) -def test_expr_mean_series(constructor_eager: Any, col: str, expected: float) -> None: +def test_expr_mean_series( + constructor_eager: ConstructorEager, col: str, expected: float +) -> None: series = nw.from_native(constructor_eager(data), eager_only=True)[col] result = series.mean() compare_dicts({col: [result]}, {col: [expected]}) diff --git a/tests/expr_and_series/min_test.py b/tests/expr_and_series/min_test.py index f6e98e416..afd659df1 100644 --- a/tests/expr_and_series/min_test.py +++ b/tests/expr_and_series/min_test.py @@ -1,11 +1,10 @@ from __future__ import annotations -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} @@ -20,7 +19,9 @@ def test_expr_min_expr(constructor: Constructor, expr: nw.Expr) -> None: @pytest.mark.parametrize(("col", "expected"), [("a", 1), ("b", 4), ("z", 7.0)]) -def test_expr_min_series(constructor_eager: Any, col: str, expected: float) -> None: +def test_expr_min_series( + constructor_eager: ConstructorEager, col: str, expected: float +) -> None: series = nw.from_native(constructor_eager(data), eager_only=True)[col] result = series.min() compare_dicts({col: [result]}, {col: [expected]}) diff --git a/tests/expr_and_series/mode_test.py b/tests/expr_and_series/mode_test.py index 8e39405af..820e05ad8 100644 --- a/tests/expr_and_series/mode_test.py +++ b/tests/expr_and_series/mode_test.py @@ -1,11 +1,10 @@ -from typing import Any - import polars as pl import pytest import narwhals.stable.v1 as nw from narwhals.utils import parse_version from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -39,7 +38,7 @@ def test_mode_multi_expr( compare_dicts(result, expected) -def test_mode_series(constructor_eager: Any) -> None: +def test_mode_series(constructor_eager: ConstructorEager) -> None: series = nw.from_native(constructor_eager(data), eager_only=True)["a"] result = series.mode().sort() expected = {"a": [1, 2]} diff --git a/tests/expr_and_series/n_unique_test.py b/tests/expr_and_series/n_unique_test.py index 3790bb1f3..c4199eec1 100644 --- a/tests/expr_and_series/n_unique_test.py +++ b/tests/expr_and_series/n_unique_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -17,7 +16,7 @@ def test_n_unique(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_n_unique_series(constructor_eager: Any) -> None: +def test_n_unique_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) expected = {"a": [3], "b": [4]} result_series = {"a": [df["a"].n_unique()], "b": [df["b"].n_unique()]} diff --git a/tests/expr_and_series/null_count_test.py b/tests/expr_and_series/null_count_test.py index 6be15ab32..93d467cb3 100644 --- a/tests/expr_and_series/null_count_test.py +++ b/tests/expr_and_series/null_count_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -20,7 +19,7 @@ def test_null_count_expr(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_null_count_series(constructor_eager: Any) -> None: +def test_null_count_series(constructor_eager: ConstructorEager) -> None: data = [1, 2, None] series = nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"] result = series.null_count() diff --git a/tests/expr_and_series/operators_test.py b/tests/expr_and_series/operators_test.py index e3f39465c..b4c3677ef 100644 --- a/tests/expr_and_series/operators_test.py +++ b/tests/expr_and_series/operators_test.py @@ -1,11 +1,10 @@ from __future__ import annotations -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -78,7 +77,7 @@ def test_logic_operators_expr( ], ) def test_comparand_operators_scalar_series( - constructor_eager: Any, operator: str, expected: list[bool] + constructor_eager: ConstructorEager, operator: str, expected: list[bool] ) -> None: data = {"a": [0, 1, 2]} s = nw.from_native(constructor_eager(data), eager_only=True)["a"] @@ -98,7 +97,7 @@ def test_comparand_operators_scalar_series( ], ) def test_comparand_operators_series( - constructor_eager: Any, operator: str, expected: list[bool] + constructor_eager: ConstructorEager, operator: str, expected: list[bool] ) -> None: data = {"a": [0, 1, 1], "b": [0, 0, 2]} df = nw.from_native(constructor_eager(data), eager_only=True) @@ -115,7 +114,7 @@ def test_comparand_operators_series( ], ) def test_logic_operators_series( - constructor_eager: Any, operator: str, expected: list[bool] + constructor_eager: ConstructorEager, operator: str, expected: list[bool] ) -> None: data = {"a": [True, True, False, False], "b": [True, False, True, False]} df = nw.from_native(constructor_eager(data), eager_only=True) diff --git a/tests/expr_and_series/pipe_test.py b/tests/expr_and_series/pipe_test.py index 2134a931b..84b6006d7 100644 --- a/tests/expr_and_series/pipe_test.py +++ b/tests/expr_and_series/pipe_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts input_list = {"a": [2, 4, 6, 8]} @@ -15,7 +14,7 @@ def test_pipe_expr(constructor: Constructor) -> None: def test_pipe_series( - constructor_eager: Any, + constructor_eager: ConstructorEager, ) -> None: s = nw.from_native(constructor_eager(input_list), eager_only=True)["a"] result = s.pipe(lambda x: x**2) diff --git a/tests/expr_and_series/quantile_test.py b/tests/expr_and_series/quantile_test.py index aae2b3647..4fd5fa3f4 100644 --- a/tests/expr_and_series/quantile_test.py +++ b/tests/expr_and_series/quantile_test.py @@ -1,13 +1,13 @@ from __future__ import annotations from contextlib import nullcontext as does_not_raise -from typing import Any from typing import Literal import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -62,7 +62,7 @@ def test_quantile_expr( ) @pytest.mark.filterwarnings("ignore:the `interpolation=` argument to percentile") def test_quantile_series( - constructor_eager: Any, + constructor_eager: ConstructorEager, interpolation: Literal["nearest", "higher", "lower", "midpoint", "linear"], expected: float, ) -> None: diff --git a/tests/expr_and_series/replace_time_zone_test.py b/tests/expr_and_series/replace_time_zone_test.py index 4954875f0..1c029a478 100644 --- a/tests/expr_and_series/replace_time_zone_test.py +++ b/tests/expr_and_series/replace_time_zone_test.py @@ -1,6 +1,8 @@ +from __future__ import annotations + from datetime import datetime from datetime import timezone -from typing import Any +from typing import TYPE_CHECKING import pandas as pd import pyarrow as pa @@ -12,6 +14,9 @@ from tests.utils import compare_dicts from tests.utils import is_windows +if TYPE_CHECKING: + from tests.utils import ConstructorEager + def test_replace_time_zone( constructor: Constructor, request: pytest.FixtureRequest @@ -65,7 +70,7 @@ def test_replace_time_zone_none( def test_replace_time_zone_series( - constructor_eager: Any, request: pytest.FixtureRequest + constructor_eager: ConstructorEager, request: pytest.FixtureRequest ) -> None: if ( (any(x in str(constructor_eager) for x in ("pyarrow", "modin")) and is_windows()) @@ -97,7 +102,7 @@ def test_replace_time_zone_series( def test_replace_time_zone_none_series( - constructor_eager: Any, request: pytest.FixtureRequest + constructor_eager: ConstructorEager, request: pytest.FixtureRequest ) -> None: if ( (any(x in str(constructor_eager) for x in ("pyarrow", "modin")) and is_windows()) diff --git a/tests/expr_and_series/round_test.py b/tests/expr_and_series/round_test.py index 37d6ce131..613a82afe 100644 --- a/tests/expr_and_series/round_test.py +++ b/tests/expr_and_series/round_test.py @@ -1,11 +1,10 @@ from __future__ import annotations -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -21,7 +20,7 @@ def test_round(constructor: Constructor, decimals: int) -> None: @pytest.mark.parametrize("decimals", [0, 1, 2]) -def test_round_series(constructor_eager: Any, decimals: int) -> None: +def test_round_series(constructor_eager: ConstructorEager, decimals: int) -> None: data = {"a": [1.12345, 2.56789, 3.901234]} df_raw = constructor_eager(data) df = nw.from_native(df_raw, eager_only=True) diff --git a/tests/expr_and_series/shift_test.py b/tests/expr_and_series/shift_test.py index b165adf12..a665ff768 100644 --- a/tests/expr_and_series/shift_test.py +++ b/tests/expr_and_series/shift_test.py @@ -1,9 +1,8 @@ -from typing import Any - import pyarrow as pa import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -26,7 +25,7 @@ def test_shift(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_shift_series(constructor_eager: Any) -> None: +def test_shift_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) result = df.with_columns( df["a"].shift(2), diff --git a/tests/expr_and_series/sort_test.py b/tests/expr_and_series/sort_test.py index f06e21f74..2ea8cd145 100644 --- a/tests/expr_and_series/sort_test.py +++ b/tests/expr_and_series/sort_test.py @@ -3,6 +3,8 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager +from tests.utils import compare_dicts data = {"a": [0, 0, 2, -1], "b": [1, 3, 2, None]} @@ -17,16 +19,14 @@ ], ) def test_sort_expr( - constructor_eager: Any, descending: Any, nulls_last: Any, expected: Any + constructor_eager: ConstructorEager, descending: Any, nulls_last: Any, expected: Any ) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) - result = nw.to_native( - df.select( - "a", - nw.col("b").sort(descending=descending, nulls_last=nulls_last), - ) + result = df.select( + "a", + nw.col("b").sort(descending=descending, nulls_last=nulls_last), ) - assert result.equals(constructor_eager(expected)) + compare_dicts(result, expected) @pytest.mark.parametrize( @@ -39,7 +39,7 @@ def test_sort_expr( ], ) def test_sort_series( - constructor_eager: Any, descending: Any, nulls_last: Any, expected: Any + constructor_eager: ConstructorEager, descending: Any, nulls_last: Any, expected: Any ) -> None: series = nw.from_native(constructor_eager(data), eager_only=True)["b"] result = series.sort(descending=descending, nulls_last=nulls_last) diff --git a/tests/expr_and_series/std_test.py b/tests/expr_and_series/std_test.py index 400a6e0af..09779c109 100644 --- a/tests/expr_and_series/std_test.py +++ b/tests/expr_and_series/std_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} @@ -26,7 +25,7 @@ def test_std(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_std_series(constructor_eager: Any) -> None: +def test_std_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) result = { "a_ddof_default": [df["a"].std()], diff --git a/tests/expr_and_series/str/contains_test.py b/tests/expr_and_series/str/contains_test.py index 6b9e74b69..139b71eb8 100644 --- a/tests/expr_and_series/str/contains_test.py +++ b/tests/expr_and_series/str/contains_test.py @@ -1,11 +1,10 @@ -from typing import Any - import pandas as pd import polars as pl import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"pets": ["cat", "dog", "rabbit and parrot", "dove"]} @@ -32,7 +31,7 @@ def test_contains_case_insensitive( def test_contains_series_case_insensitive( - constructor_eager: Any, request: pytest.FixtureRequest + constructor_eager: ConstructorEager, request: pytest.FixtureRequest ) -> None: if "cudf" in str(constructor_eager): request.applymarker(pytest.mark.xfail) @@ -58,7 +57,7 @@ def test_contains_case_sensitive(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_contains_series_case_sensitive(constructor_eager: Any) -> None: +def test_contains_series_case_sensitive(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) result = df.with_columns(case_sensitive_match=df["pets"].str.contains("parrot|Dove")) expected = { diff --git a/tests/expr_and_series/str/head_test.py b/tests/expr_and_series/str/head_test.py index a4b3e7296..8da64553e 100644 --- a/tests/expr_and_series/str/head_test.py +++ b/tests/expr_and_series/str/head_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": ["foo", "bars"]} @@ -16,7 +15,7 @@ def test_str_head(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_str_head_series(constructor_eager: Any) -> None: +def test_str_head_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) expected = { "a": ["foo", "bar"], diff --git a/tests/expr_and_series/str/len_chars_test.py b/tests/expr_and_series/str/len_chars_test.py index ace145552..80a791c61 100644 --- a/tests/expr_and_series/str/len_chars_test.py +++ b/tests/expr_and_series/str/len_chars_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": ["foo", "foobar", "Café", "345", "東京"]} @@ -16,7 +15,7 @@ def test_str_len_chars(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_str_len_chars_series(constructor_eager: Any) -> None: +def test_str_len_chars_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) expected = { "a": [3, 6, 4, 3, 2], diff --git a/tests/expr_and_series/str/replace_test.py b/tests/expr_and_series/str/replace_test.py index b0cffb1b4..8db24c91e 100644 --- a/tests/expr_and_series/str/replace_test.py +++ b/tests/expr_and_series/str/replace_test.py @@ -1,11 +1,10 @@ from __future__ import annotations -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts replace_data = [ @@ -54,7 +53,7 @@ replace_data, ) def test_str_replace_series( - constructor_eager: Any, + constructor_eager: ConstructorEager, data: dict[str, list[str]], pattern: str, value: str, @@ -75,7 +74,7 @@ def test_str_replace_series( replace_all_data, ) def test_str_replace_all_series( - constructor_eager: Any, + constructor_eager: ConstructorEager, data: dict[str, list[str]], pattern: str, value: str, diff --git a/tests/expr_and_series/str/slice_test.py b/tests/expr_and_series/str/slice_test.py index e7fe0efa1..3b7bb90ce 100644 --- a/tests/expr_and_series/str/slice_test.py +++ b/tests/expr_and_series/str/slice_test.py @@ -6,6 +6,7 @@ import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": ["fdas", "edfas"]} @@ -28,7 +29,7 @@ def test_str_slice( [(1, 2, {"a": ["da", "df"]}), (-2, None, {"a": ["as", "as"]})], ) def test_str_slice_series( - constructor_eager: Any, offset: int, length: int | None, expected: Any + constructor_eager: ConstructorEager, offset: int, length: int | None, expected: Any ) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) diff --git a/tests/expr_and_series/str/starts_with_ends_with_test.py b/tests/expr_and_series/str/starts_with_ends_with_test.py index e8b0afaa9..3682c4182 100644 --- a/tests/expr_and_series/str/starts_with_ends_with_test.py +++ b/tests/expr_and_series/str/starts_with_ends_with_test.py @@ -1,9 +1,8 @@ from __future__ import annotations -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager # Don't move this into typechecking block, for coverage # purposes @@ -21,7 +20,7 @@ def test_ends_with(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_ends_with_series(constructor_eager: Any) -> None: +def test_ends_with_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) result = df.select(df["a"].str.ends_with("das")) expected = { @@ -39,7 +38,7 @@ def test_starts_with(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_starts_with_series(constructor_eager: Any) -> None: +def test_starts_with_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) result = df.select(df["a"].str.starts_with("fda")) expected = { diff --git a/tests/expr_and_series/str/strip_chars_test.py b/tests/expr_and_series/str/strip_chars_test.py index 3d5b74456..66b9cda0d 100644 --- a/tests/expr_and_series/str/strip_chars_test.py +++ b/tests/expr_and_series/str/strip_chars_test.py @@ -6,6 +6,7 @@ import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": ["foobar", "bar\n", " baz"]} @@ -34,7 +35,7 @@ def test_str_strip_chars( ], ) def test_str_strip_chars_series( - constructor_eager: Any, characters: str | None, expected: Any + constructor_eager: ConstructorEager, characters: str | None, expected: Any ) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) diff --git a/tests/expr_and_series/str/tail_test.py b/tests/expr_and_series/str/tail_test.py index 92d474262..260ab745c 100644 --- a/tests/expr_and_series/str/tail_test.py +++ b/tests/expr_and_series/str/tail_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": ["foo", "bars"]} @@ -15,7 +14,7 @@ def test_str_tail(constructor: Constructor) -> None: compare_dicts(result_frame, expected) -def test_str_tail_series(constructor_eager: Any) -> None: +def test_str_tail_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) expected = {"a": ["foo", "ars"]} diff --git a/tests/expr_and_series/str/to_datetime_test.py b/tests/expr_and_series/str/to_datetime_test.py index 8474357e0..62afda474 100644 --- a/tests/expr_and_series/str/to_datetime_test.py +++ b/tests/expr_and_series/str/to_datetime_test.py @@ -1,10 +1,14 @@ -from typing import Any +from __future__ import annotations + +from typing import TYPE_CHECKING import pytest import narwhals.stable.v1 as nw -from tests.utils import Constructor +if TYPE_CHECKING: + from tests.utils import Constructor + from tests.utils import ConstructorEager data = {"a": ["2020-01-01T12:34:56"]} @@ -24,7 +28,7 @@ def test_to_datetime(constructor: Constructor) -> None: assert str(result) == expected -def test_to_datetime_series(constructor_eager: Any) -> None: +def test_to_datetime_series(constructor_eager: ConstructorEager) -> None: if "cudf" in str(constructor_eager): # pragma: no cover expected = "2020-01-01T12:34:56.000000000" else: @@ -60,7 +64,7 @@ def test_to_datetime_infer_fmt( def test_to_datetime_series_infer_fmt( - request: pytest.FixtureRequest, constructor_eager: Any + request: pytest.FixtureRequest, constructor_eager: ConstructorEager ) -> None: if "pyarrow_table" in str(constructor_eager): request.applymarker(pytest.mark.xfail) diff --git a/tests/expr_and_series/str/to_uppercase_to_lowercase_test.py b/tests/expr_and_series/str/to_uppercase_to_lowercase_test.py index 877409138..e5b5832f6 100644 --- a/tests/expr_and_series/str/to_uppercase_to_lowercase_test.py +++ b/tests/expr_and_series/str/to_uppercase_to_lowercase_test.py @@ -1,13 +1,12 @@ from __future__ import annotations -from typing import Any - import pyarrow as pa import pytest import narwhals.stable.v1 as nw from narwhals.utils import parse_version from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -68,7 +67,7 @@ def test_str_to_uppercase( ], ) def test_str_to_uppercase_series( - constructor_eager: Any, + constructor_eager: ConstructorEager, data: dict[str, list[str]], expected: dict[str, list[str]], request: pytest.FixtureRequest, @@ -134,7 +133,7 @@ def test_str_to_lowercase( ], ) def test_str_to_lowercase_series( - constructor_eager: Any, + constructor_eager: ConstructorEager, data: dict[str, list[str]], expected: dict[str, list[str]], ) -> None: diff --git a/tests/expr_and_series/sum_test.py b/tests/expr_and_series/sum_test.py index 8059a097d..914d902f3 100644 --- a/tests/expr_and_series/sum_test.py +++ b/tests/expr_and_series/sum_test.py @@ -1,11 +1,10 @@ from __future__ import annotations -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} @@ -20,7 +19,9 @@ def test_expr_sum_expr(constructor: Constructor, expr: nw.Expr) -> None: @pytest.mark.parametrize(("col", "expected"), [("a", 6), ("b", 14), ("z", 24.0)]) -def test_expr_sum_series(constructor_eager: Any, col: str, expected: float) -> None: +def test_expr_sum_series( + constructor_eager: ConstructorEager, col: str, expected: float +) -> None: series = nw.from_native(constructor_eager(data), eager_only=True)[col] result = series.sum() compare_dicts({col: [result]}, {col: [expected]}) diff --git a/tests/expr_and_series/tail_test.py b/tests/expr_and_series/tail_test.py index fc3e6159a..73acb6848 100644 --- a/tests/expr_and_series/tail_test.py +++ b/tests/expr_and_series/tail_test.py @@ -1,9 +1,8 @@ -from typing import Any - import pytest import narwhals as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -20,7 +19,7 @@ def test_head(constructor: Constructor, n: int, request: pytest.FixtureRequest) @pytest.mark.parametrize("n", [2, -1]) -def test_head_series(constructor_eager: Any, n: int) -> None: +def test_head_series(constructor_eager: ConstructorEager, n: int) -> None: df = nw.from_native(constructor_eager({"a": [1, 2, 3]}), eager_only=True) result = df.select(df["a"].tail(n)) expected = {"a": [2, 3]} diff --git a/tests/expr_and_series/unary_test.py b/tests/expr_and_series/unary_test.py index 66afd22af..c1e1d007b 100644 --- a/tests/expr_and_series/unary_test.py +++ b/tests/expr_and_series/unary_test.py @@ -1,7 +1,6 @@ -from typing import Any - import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -29,7 +28,7 @@ def test_unary(constructor: Constructor) -> None: compare_dicts(result, expected) -def test_unary_series(constructor_eager: Any) -> None: +def test_unary_series(constructor_eager: ConstructorEager) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor_eager(data), eager_only=True) result = { diff --git a/tests/expr_and_series/unique_test.py b/tests/expr_and_series/unique_test.py index 5639179ba..5048d3250 100644 --- a/tests/expr_and_series/unique_test.py +++ b/tests/expr_and_series/unique_test.py @@ -1,9 +1,8 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": [1, 1, 2]} @@ -18,7 +17,7 @@ def test_unique_expr(constructor: Constructor, request: pytest.FixtureRequest) - compare_dicts(result, expected) -def test_unique_series(constructor_eager: Any) -> None: +def test_unique_series(constructor_eager: ConstructorEager) -> None: series = nw.from_native(constructor_eager(data), eager_only=True)["a"] result = series.unique() expected = {"a": [1, 2]} diff --git a/tests/expr_and_series/when_test.py b/tests/expr_and_series/when_test.py index 6fabaa68b..eb1ac9c41 100644 --- a/tests/expr_and_series/when_test.py +++ b/tests/expr_and_series/when_test.py @@ -1,12 +1,11 @@ from __future__ import annotations -from typing import Any - import numpy as np import pytest import narwhals.stable.v1 as nw from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -71,7 +70,7 @@ def test_value_numpy_array( compare_dicts(result, expected) -def test_value_series(constructor_eager: Any) -> None: +def test_value_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data)) s_data = {"s": [3, 4, 5]} s = nw.from_native(constructor_eager(s_data))["s"] @@ -110,7 +109,7 @@ def test_otherwise_numpy_array( compare_dicts(result, expected) -def test_otherwise_series(constructor_eager: Any) -> None: +def test_otherwise_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data)) s_data = {"s": [0, 9, 10]} s = nw.from_native(constructor_eager(s_data))["s"] diff --git a/tests/frame/array_dunder_test.py b/tests/frame/array_dunder_test.py index 8a082bb1f..ad3085f56 100644 --- a/tests/frame/array_dunder_test.py +++ b/tests/frame/array_dunder_test.py @@ -1,5 +1,3 @@ -from typing import Any - import numpy as np import pandas as pd import polars as pl @@ -8,10 +6,13 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +from tests.utils import ConstructorEager from tests.utils import compare_dicts -def test_array_dunder(request: pytest.FixtureRequest, constructor_eager: Any) -> None: +def test_array_dunder( + request: pytest.FixtureRequest, constructor_eager: ConstructorEager +) -> None: if "pyarrow_table" in str(constructor_eager) and parse_version( pa.__version__ ) < parse_version("16.0.0"): # pragma: no cover @@ -23,7 +24,7 @@ def test_array_dunder(request: pytest.FixtureRequest, constructor_eager: Any) -> def test_array_dunder_with_dtype( - request: pytest.FixtureRequest, constructor_eager: Any + request: pytest.FixtureRequest, constructor_eager: ConstructorEager ) -> None: if "pyarrow_table" in str(constructor_eager) and parse_version( pa.__version__ @@ -36,7 +37,7 @@ def test_array_dunder_with_dtype( def test_array_dunder_with_copy( - request: pytest.FixtureRequest, constructor_eager: Any + request: pytest.FixtureRequest, constructor_eager: ConstructorEager ) -> None: if "pyarrow_table" in str(constructor_eager) and parse_version(pa.__version__) < ( 16, diff --git a/tests/frame/get_column_test.py b/tests/frame/get_column_test.py index 58766ac31..ff4ebc506 100644 --- a/tests/frame/get_column_test.py +++ b/tests/frame/get_column_test.py @@ -1,13 +1,12 @@ -from typing import Any - import pandas as pd import pytest import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts -def test_get_column(constructor_eager: Any) -> None: +def test_get_column(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager({"a": [1, 2], "b": [3, 4]}), eager_only=True) result = df.get_column("a") compare_dicts({"a": result}, {"a": [1, 2]}) diff --git a/tests/frame/getitem_test.py b/tests/frame/getitem_test.py index ce96c1b24..a5397c7ef 100644 --- a/tests/frame/getitem_test.py +++ b/tests/frame/getitem_test.py @@ -1,7 +1,5 @@ from __future__ import annotations -from typing import Any - import numpy as np import pandas as pd import polars as pl @@ -9,6 +7,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = { @@ -17,13 +16,13 @@ } -def test_slice_column(constructor_eager: Any) -> None: +def test_slice_column(constructor_eager: ConstructorEager) -> None: result = nw.from_native(constructor_eager(data))["a"] assert isinstance(result, nw.Series) compare_dicts({"a": result}, {"a": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]}) -def test_slice_rows(constructor_eager: Any) -> None: +def test_slice_rows(constructor_eager: ConstructorEager) -> None: result = nw.from_native(constructor_eager(data))[1:] compare_dicts(result, {"a": [2.0, 3.0, 4.0, 5.0, 6.0], "b": [12, 13, 14, 15, 16]}) @@ -32,7 +31,7 @@ def test_slice_rows(constructor_eager: Any) -> None: def test_slice_rows_with_step( - request: pytest.FixtureRequest, constructor_eager: Any + request: pytest.FixtureRequest, constructor_eager: ConstructorEager ) -> None: if "pyarrow_table" in str(constructor_eager): request.applymarker(pytest.mark.xfail) @@ -53,19 +52,19 @@ def test_slice_lazy_fails() -> None: _ = nw.from_native(pl.LazyFrame(data))[1:] -def test_slice_int(constructor_eager: Any) -> None: +def test_slice_int(constructor_eager: ConstructorEager) -> None: result = nw.from_native(constructor_eager(data), eager_only=True)[1] # type: ignore[call-overload] compare_dicts(result, {"a": [2], "b": [12]}) -def test_slice_fails(constructor_eager: Any) -> None: +def test_slice_fails(constructor_eager: ConstructorEager) -> None: class Foo: ... with pytest.raises(TypeError, match="Expected str or slice, got:"): nw.from_native(constructor_eager(data), eager_only=True)[Foo()] # type: ignore[call-overload] -def test_gather(constructor_eager: Any) -> None: +def test_gather(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) result = df[[0, 3, 1]] expected = { @@ -89,7 +88,7 @@ def test_gather_pandas_index() -> None: compare_dicts(result, expected) -def test_gather_rows_cols(constructor_eager: Any) -> None: +def test_gather_rows_cols(constructor_eager: ConstructorEager) -> None: native_df = constructor_eager(data) df = nw.from_native(native_df, eager_only=True) @@ -102,7 +101,7 @@ def test_gather_rows_cols(constructor_eager: Any) -> None: compare_dicts(result, expected) -def test_slice_both_tuples_of_ints(constructor_eager: Any) -> None: +def test_slice_both_tuples_of_ints(constructor_eager: ConstructorEager) -> None: data = {"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]} df = nw.from_native(constructor_eager(data), eager_only=True) result = df[[0, 1], [0, 2]] @@ -110,7 +109,7 @@ def test_slice_both_tuples_of_ints(constructor_eager: Any) -> None: compare_dicts(result, expected) -def test_slice_int_rows_str_columns(constructor_eager: Any) -> None: +def test_slice_int_rows_str_columns(constructor_eager: ConstructorEager) -> None: data = {"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]} df = nw.from_native(constructor_eager(data), eager_only=True) result = df[[0, 1], ["a", "c"]] @@ -118,7 +117,7 @@ def test_slice_int_rows_str_columns(constructor_eager: Any) -> None: compare_dicts(result, expected) -def test_slice_slice_columns(constructor_eager: Any) -> None: # noqa: PLR0915 +def test_slice_slice_columns(constructor_eager: ConstructorEager) -> None: # noqa: PLR0915 data = {"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [1, 4, 2]} df = nw.from_native(constructor_eager(data), eager_only=True) result = df[[0, 1], "b":"c"] # type: ignore[misc] @@ -188,14 +187,14 @@ def test_slice_slice_columns(constructor_eager: Any) -> None: # noqa: PLR0915 compare_dicts(result, expected) -def test_slice_invalid(constructor_eager: Any) -> None: +def test_slice_invalid(constructor_eager: ConstructorEager) -> None: data = {"a": [1, 2], "b": [4, 5]} df = nw.from_native(constructor_eager(data), eager_only=True) with pytest.raises(TypeError, match="Hint:"): df[0, 0] -def test_slice_edge_cases(constructor_eager: Any) -> None: +def test_slice_edge_cases(constructor_eager: ConstructorEager) -> None: data = {"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [1, 4, 2]} df = nw.from_native(constructor_eager(data), eager_only=True) assert df[[], :].shape == (0, 4) @@ -219,7 +218,7 @@ def test_slice_edge_cases(constructor_eager: Any) -> None: ], ) def test_get_item_works_with_tuple_and_list_and_range_row_and_col_indexing( - constructor_eager: Any, + constructor_eager: ConstructorEager, row_idx: list[int] | tuple[int] | range, col_idx: list[int] | tuple[int] | range, ) -> None: @@ -236,7 +235,7 @@ def test_get_item_works_with_tuple_and_list_and_range_row_and_col_indexing( ], ) def test_get_item_works_with_tuple_and_list_and_range_row_indexing_and_slice_col_indexing( - constructor_eager: Any, + constructor_eager: ConstructorEager, row_idx: list[int] | tuple[int] | range, col: slice, ) -> None: @@ -253,7 +252,7 @@ def test_get_item_works_with_tuple_and_list_and_range_row_indexing_and_slice_col ], ) def test_get_item_works_with_tuple_and_list_indexing_and_str( - constructor_eager: Any, + constructor_eager: ConstructorEager, row_idx: list[int] | tuple[int] | range, col: str, ) -> None: diff --git a/tests/frame/is_duplicated_test.py b/tests/frame/is_duplicated_test.py index e1eb3f298..a4dbd97aa 100644 --- a/tests/frame/is_duplicated_test.py +++ b/tests/frame/is_duplicated_test.py @@ -1,12 +1,11 @@ from __future__ import annotations -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts -def test_is_duplicated(constructor_eager: Any) -> None: +def test_is_duplicated(constructor_eager: ConstructorEager) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df_raw = constructor_eager(data) df = nw.from_native(df_raw, eager_only=True) diff --git a/tests/frame/is_empty_test.py b/tests/frame/is_empty_test.py index a772abc8b..7ea6b22ad 100644 --- a/tests/frame/is_empty_test.py +++ b/tests/frame/is_empty_test.py @@ -1,14 +1,20 @@ from __future__ import annotations +from typing import TYPE_CHECKING from typing import Any import pytest import narwhals.stable.v1 as nw +if TYPE_CHECKING: + from tests.utils import ConstructorEager + @pytest.mark.parametrize(("threshold", "expected"), [(0, False), (10, True)]) -def test_is_empty(constructor_eager: Any, threshold: Any, expected: Any) -> None: +def test_is_empty( + constructor_eager: ConstructorEager, threshold: Any, expected: Any +) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df_raw = constructor_eager(data) df = nw.from_native(df_raw, eager_only=True) diff --git a/tests/frame/is_unique_test.py b/tests/frame/is_unique_test.py index 4259c8773..cb9d57ba2 100644 --- a/tests/frame/is_unique_test.py +++ b/tests/frame/is_unique_test.py @@ -1,12 +1,11 @@ from __future__ import annotations -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts -def test_is_unique(constructor_eager: Any) -> None: +def test_is_unique(constructor_eager: ConstructorEager) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df_raw = constructor_eager(data) df = nw.from_native(df_raw, eager_only=True) diff --git a/tests/frame/item_test.py b/tests/frame/item_test.py index 7afbee12d..4453ea611 100644 --- a/tests/frame/item_test.py +++ b/tests/frame/item_test.py @@ -6,6 +6,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts @@ -14,7 +15,10 @@ [(0, 2, 7), (1, "z", 8)], ) def test_item( - constructor_eager: Any, row: int | None, column: int | str | None, expected: Any + constructor_eager: ConstructorEager, + row: int | None, + column: int | str | None, + expected: Any, ) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor_eager(data), eager_only=True) @@ -43,7 +47,10 @@ def test_item( ], ) def test_item_value_error( - constructor_eager: Any, row: int | None, column: int | str | None, err_msg: str + constructor_eager: ConstructorEager, + row: int | None, + column: int | str | None, + err_msg: str, ) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} with pytest.raises(ValueError, match=err_msg): diff --git a/tests/frame/lazy_test.py b/tests/frame/lazy_test.py index 09ca734c2..8f1566e69 100644 --- a/tests/frame/lazy_test.py +++ b/tests/frame/lazy_test.py @@ -1,10 +1,9 @@ -from typing import Any - import narwhals as nw import narwhals.stable.v1 as nw_v1 +from tests.utils import ConstructorEager -def test_lazy(constructor_eager: Any) -> None: +def test_lazy(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager({"a": [1, 2, 3]}), eager_only=True) result = df.lazy() assert isinstance(result, nw.LazyFrame) diff --git a/tests/frame/len_test.py b/tests/frame/len_test.py index c06884e03..cd082ef2e 100644 --- a/tests/frame/len_test.py +++ b/tests/frame/len_test.py @@ -1,6 +1,5 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager data = { "a": [1.0, 2.0, None, 4.0], @@ -8,6 +7,7 @@ } -def test_len(constructor_eager: Any) -> None: +def test_len(constructor_eager: ConstructorEager) -> None: result = len(nw.from_native(constructor_eager(data), eager_only=True)) + assert result == 4 diff --git a/tests/frame/null_count_test.py b/tests/frame/null_count_test.py index d3bf7f25c..71ac965f8 100644 --- a/tests/frame/null_count_test.py +++ b/tests/frame/null_count_test.py @@ -1,12 +1,11 @@ from __future__ import annotations -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts -def test_null_count(constructor_eager: Any) -> None: +def test_null_count(constructor_eager: ConstructorEager) -> None: data = {"a": [None, 3, 2], "b": [4, 4, 6], "z": [7.0, None, 9]} df_raw = constructor_eager(data) df = nw.from_native(df_raw, eager_only=True) diff --git a/tests/frame/row_test.py b/tests/frame/row_test.py index 599dcaeaf..d977a81f1 100644 --- a/tests/frame/row_test.py +++ b/tests/frame/row_test.py @@ -3,9 +3,10 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager -def test_row_column(request: Any, constructor_eager: Any) -> None: +def test_row_column(request: Any, constructor_eager: ConstructorEager) -> None: if "cudf" in str(constructor_eager): request.applymarker(pytest.mark.xfail) diff --git a/tests/frame/rows_test.py b/tests/frame/rows_test.py index 744f66065..60e18658c 100644 --- a/tests/frame/rows_test.py +++ b/tests/frame/rows_test.py @@ -1,5 +1,6 @@ from __future__ import annotations +from typing import TYPE_CHECKING from typing import Any import pandas as pd @@ -10,6 +11,9 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +if TYPE_CHECKING: + from tests.utils import ConstructorEager + df_pandas = pd.DataFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}) df_pa = pa.table({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}) if parse_version(pd.__version__) >= parse_version("1.5.0"): @@ -56,7 +60,7 @@ ) def test_iter_rows( request: Any, - constructor_eager: Any, + constructor_eager: ConstructorEager, named: bool, # noqa: FBT001 expected: list[tuple[Any, ...]] | list[dict[str, Any]], ) -> None: diff --git a/tests/frame/schema_test.py b/tests/frame/schema_test.py index 8e1116997..97c3722a7 100644 --- a/tests/frame/schema_test.py +++ b/tests/frame/schema_test.py @@ -12,6 +12,7 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version from tests.utils import Constructor +from tests.utils import ConstructorEager data = { "a": [datetime(2020, 1, 1)], @@ -60,7 +61,9 @@ def test_string_disguised_as_object() -> None: assert result["a"] == nw.String -def test_actual_object(request: pytest.FixtureRequest, constructor_eager: Any) -> None: +def test_actual_object( + request: pytest.FixtureRequest, constructor_eager: ConstructorEager +) -> None: if any(x in str(constructor_eager) for x in ("modin", "pyarrow_table", "cudf")): request.applymarker(pytest.mark.xfail) diff --git a/tests/frame/shape_test.py b/tests/frame/shape_test.py index 2ab3a23bc..6930214f7 100644 --- a/tests/frame/shape_test.py +++ b/tests/frame/shape_test.py @@ -1,9 +1,8 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager -def test_shape(constructor_eager: Any) -> None: +def test_shape(constructor_eager: ConstructorEager) -> None: result = nw.from_native( constructor_eager({"a": [1, 2], "b": [4, 5], "c": [7, 8]}), eager_only=True ).shape diff --git a/tests/frame/to_arrow_test.py b/tests/frame/to_arrow_test.py index f20bdf28c..373f6310b 100644 --- a/tests/frame/to_arrow_test.py +++ b/tests/frame/to_arrow_test.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any +from typing import TYPE_CHECKING import pandas as pd import pyarrow as pa @@ -9,8 +9,13 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +if TYPE_CHECKING: + from tests.utils import ConstructorEager -def test_to_arrow(request: pytest.FixtureRequest, constructor_eager: Any) -> None: + +def test_to_arrow( + request: pytest.FixtureRequest, constructor_eager: ConstructorEager +) -> None: if "pandas" in str(constructor_eager) and parse_version(pd.__version__) < (1, 0, 0): # pyarrow requires pandas>=1.0.0 request.applymarker(pytest.mark.xfail) diff --git a/tests/frame/to_dict_test.py b/tests/frame/to_dict_test.py index 29c3d2270..b76003bd1 100644 --- a/tests/frame/to_dict_test.py +++ b/tests/frame/to_dict_test.py @@ -1,22 +1,21 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts @pytest.mark.filterwarnings( "ignore:.*all arguments of to_dict except for the argument:FutureWarning" ) -def test_to_dict(constructor_eager: Any) -> None: +def test_to_dict(constructor_eager: ConstructorEager) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "c": [7.0, 8, 9]} df = nw.from_native(constructor_eager(data), eager_only=True) result = df.to_dict(as_series=False) assert result == data -def test_to_dict_as_series(constructor_eager: Any) -> None: +def test_to_dict_as_series(constructor_eager: ConstructorEager) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "c": [7.0, 8, 9]} df = nw.from_native(constructor_eager(data), eager_only=True) result = df.to_dict(as_series=True) diff --git a/tests/frame/to_native_test.py b/tests/frame/to_native_test.py index d8f4132bf..c6de99a17 100644 --- a/tests/frame/to_native_test.py +++ b/tests/frame/to_native_test.py @@ -1,9 +1,8 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import Constructor -def test_to_native(constructor: Any) -> None: +def test_to_native(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.1, 8, 9]} df_raw = constructor(data) df = nw.from_native(df_raw) diff --git a/tests/frame/to_numpy_test.py b/tests/frame/to_numpy_test.py index d573f4322..aa3dfc2e4 100644 --- a/tests/frame/to_numpy_test.py +++ b/tests/frame/to_numpy_test.py @@ -1,13 +1,16 @@ from __future__ import annotations -from typing import Any +from typing import TYPE_CHECKING import numpy as np import narwhals.stable.v1 as nw +if TYPE_CHECKING: + from tests.utils import ConstructorEager -def test_to_numpy(constructor_eager: Any) -> None: + +def test_to_numpy(constructor_eager: ConstructorEager) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.1, 8, 9]} df_raw = constructor_eager(data) result = nw.from_native(df_raw, eager_only=True).to_numpy() diff --git a/tests/frame/to_pandas_test.py b/tests/frame/to_pandas_test.py index 671a5d857..d6370f02e 100644 --- a/tests/frame/to_pandas_test.py +++ b/tests/frame/to_pandas_test.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any +from typing import TYPE_CHECKING import pandas as pd import pytest @@ -8,13 +8,18 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +if TYPE_CHECKING: + from tests.utils import ConstructorEager + @pytest.mark.filterwarnings("ignore:.*Passing a BlockManager.*:DeprecationWarning") @pytest.mark.skipif( parse_version(pd.__version__) < parse_version("2.0.0"), reason="too old for pandas-pyarrow", ) -def test_convert_pandas(constructor_eager: Any, request: pytest.FixtureRequest) -> None: +def test_convert_pandas( + constructor_eager: ConstructorEager, request: pytest.FixtureRequest +) -> None: if "modin" in str(constructor_eager): request.applymarker(pytest.mark.xfail) data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} diff --git a/tests/frame/write_csv_test.py b/tests/frame/write_csv_test.py index ed9303604..84ce84f0d 100644 --- a/tests/frame/write_csv_test.py +++ b/tests/frame/write_csv_test.py @@ -1,16 +1,18 @@ from __future__ import annotations from typing import TYPE_CHECKING -from typing import Any import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import is_windows if TYPE_CHECKING: import pytest -def test_write_csv(constructor_eager: Any, tmpdir: pytest.TempdirFactory) -> None: +def test_write_csv( + constructor_eager: ConstructorEager, tmpdir: pytest.TempdirFactory +) -> None: data = {"a": [1, 2, 3]} path = tmpdir / "foo.csv" # type: ignore[operator] result = nw.from_native(constructor_eager(data), eager_only=True).write_csv(str(path)) diff --git a/tests/frame/write_parquet_test.py b/tests/frame/write_parquet_test.py index 8efaefb55..c616de198 100644 --- a/tests/frame/write_parquet_test.py +++ b/tests/frame/write_parquet_test.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any +from typing import TYPE_CHECKING import pandas as pd import pytest @@ -8,13 +8,18 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +if TYPE_CHECKING: + from tests.utils import ConstructorEager + data = {"a": [1, 2, 3]} @pytest.mark.skipif( parse_version(pd.__version__) < parse_version("2.0.0"), reason="too old for pyarrow" ) -def test_write_parquet(constructor_eager: Any, tmpdir: pytest.TempdirFactory) -> None: +def test_write_parquet( + constructor_eager: ConstructorEager, tmpdir: pytest.TempdirFactory +) -> None: path = tmpdir / "foo.parquet" # type: ignore[operator] nw.from_native(constructor_eager(data), eager_only=True).write_parquet(str(path)) assert path.exists() diff --git a/tests/group_by_test.py b/tests/group_by_test.py index 90cb48c26..27407a26a 100644 --- a/tests/group_by_test.py +++ b/tests/group_by_test.py @@ -1,7 +1,6 @@ from __future__ import annotations from contextlib import nullcontext -from typing import Any import pandas as pd import polars as pl @@ -11,6 +10,7 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version from tests.utils import Constructor +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = {"a": [1, 1, 3], "b": [4, 4, 6], "c": [7.0, 8, 9]} @@ -73,7 +73,7 @@ def test_invalid_group_by() -> None: ) -def test_group_by_iter(constructor_eager: Any) -> None: +def test_group_by_iter(constructor_eager: ConstructorEager) -> None: df = nw.from_native(constructor_eager(data), eager_only=True) expected_keys = [(1,), (3,)] keys = [] diff --git a/tests/new_series_test.py b/tests/new_series_test.py index fad4a7536..37e5d2633 100644 --- a/tests/new_series_test.py +++ b/tests/new_series_test.py @@ -1,14 +1,13 @@ -from typing import Any - import pandas as pd import pytest import narwhals as nw import narwhals.stable.v1 as nw_v1 +from tests.utils import ConstructorEager from tests.utils import compare_dicts -def test_new_series(constructor_eager: Any) -> None: +def test_new_series(constructor_eager: ConstructorEager) -> None: s = nw.from_native(constructor_eager({"a": [1, 2, 3]}), eager_only=True)["a"] result = nw.new_series("b", [4, 1, 2], native_namespace=nw.get_native_namespace(s)) expected = {"b": [4, 1, 2]} @@ -25,7 +24,7 @@ def test_new_series(constructor_eager: Any) -> None: compare_dicts(result.to_frame(), expected) -def test_new_series_v1(constructor_eager: Any) -> None: +def test_new_series_v1(constructor_eager: ConstructorEager) -> None: s = nw_v1.from_native(constructor_eager({"a": [1, 2, 3]}), eager_only=True)["a"] result = nw_v1.new_series( "b", [4, 1, 2], native_namespace=nw_v1.get_native_namespace(s) diff --git a/tests/series_only/__iter___test.py b/tests/series_only/__iter___test.py index a0a5c1189..06753917b 100644 --- a/tests/series_only/__iter___test.py +++ b/tests/series_only/__iter___test.py @@ -1,17 +1,22 @@ from __future__ import annotations from collections.abc import Iterable -from typing import Any +from typing import TYPE_CHECKING import pytest import narwhals.stable.v1 as nw from tests.utils import compare_dicts +if TYPE_CHECKING: + from tests.utils import ConstructorEager + data = [1, 2, 3] -def test_iter(constructor_eager: Any, request: pytest.FixtureRequest) -> None: +def test_iter( + constructor_eager: ConstructorEager, request: pytest.FixtureRequest +) -> None: if "cudf" in str(constructor_eager): request.applymarker(pytest.mark.xfail) s = nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"] diff --git a/tests/series_only/array_dunder_test.py b/tests/series_only/array_dunder_test.py index c09bea9ec..0d95e2db3 100644 --- a/tests/series_only/array_dunder_test.py +++ b/tests/series_only/array_dunder_test.py @@ -1,5 +1,3 @@ -from typing import Any - import numpy as np import pandas as pd import pyarrow as pa @@ -7,10 +5,13 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +from tests.utils import ConstructorEager from tests.utils import compare_dicts -def test_array_dunder(request: pytest.FixtureRequest, constructor_eager: Any) -> None: +def test_array_dunder( + request: pytest.FixtureRequest, constructor_eager: ConstructorEager +) -> None: if "pyarrow_table" in str(constructor_eager) and parse_version( pa.__version__ ) < parse_version("16.0.0"): # pragma: no cover @@ -22,7 +23,7 @@ def test_array_dunder(request: pytest.FixtureRequest, constructor_eager: Any) -> def test_array_dunder_with_dtype( - request: pytest.FixtureRequest, constructor_eager: Any + request: pytest.FixtureRequest, constructor_eager: ConstructorEager ) -> None: if "pyarrow_table" in str(constructor_eager) and parse_version( pa.__version__ @@ -35,7 +36,7 @@ def test_array_dunder_with_dtype( def test_array_dunder_with_copy( - request: pytest.FixtureRequest, constructor_eager: Any + request: pytest.FixtureRequest, constructor_eager: ConstructorEager ) -> None: if "pyarrow_table" in str(constructor_eager) and parse_version( pa.__version__ diff --git a/tests/series_only/dtype_test.py b/tests/series_only/dtype_test.py index 68d10fbca..8200150f0 100644 --- a/tests/series_only/dtype_test.py +++ b/tests/series_only/dtype_test.py @@ -1,13 +1,16 @@ from __future__ import annotations -from typing import Any +from typing import TYPE_CHECKING import narwhals.stable.v1 as nw +if TYPE_CHECKING: + from tests.utils import ConstructorEager + data = {"a": [1, 3, 2]} -def test_dtype(constructor_eager: Any) -> None: +def test_dtype(constructor_eager: ConstructorEager) -> None: series = nw.from_native(constructor_eager(data), eager_only=True)["a"] result = series.dtype assert result == nw.Int64 diff --git a/tests/series_only/is_empty_test.py b/tests/series_only/is_empty_test.py index 80b8ab799..390fa7f4f 100644 --- a/tests/series_only/is_empty_test.py +++ b/tests/series_only/is_empty_test.py @@ -1,9 +1,8 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager -def test_is_empty(constructor_eager: Any) -> None: +def test_is_empty(constructor_eager: ConstructorEager) -> None: series = nw.from_native(constructor_eager({"a": [1, 2, 3]}), eager_only=True)["a"] assert not series.is_empty() assert not series[:1].is_empty() diff --git a/tests/series_only/is_ordered_categorical_test.py b/tests/series_only/is_ordered_categorical_test.py index 26358f9a6..10251e362 100644 --- a/tests/series_only/is_ordered_categorical_test.py +++ b/tests/series_only/is_ordered_categorical_test.py @@ -1,5 +1,3 @@ -from typing import Any - import pandas as pd import polars as pl import pyarrow as pa @@ -7,6 +5,7 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +from tests.utils import ConstructorEager def test_is_ordered_categorical() -> None: @@ -39,7 +38,7 @@ def test_is_ordered_categorical_interchange_protocol() -> None: def test_is_definitely_not_ordered_categorical( - constructor_eager: Any, + constructor_eager: ConstructorEager, ) -> None: assert not nw.is_ordered_categorical( nw.from_native(constructor_eager({"a": [1, 2, 3]}), eager_only=True)["a"] diff --git a/tests/series_only/is_sorted_test.py b/tests/series_only/is_sorted_test.py index 3942b5619..23610ee56 100644 --- a/tests/series_only/is_sorted_test.py +++ b/tests/series_only/is_sorted_test.py @@ -1,10 +1,9 @@ from __future__ import annotations -from typing import Any - import pytest import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = [1, 3, 2] @@ -17,7 +16,7 @@ [(data, False, False), (data_sorted, False, True), (data_sorted, True, False)], ) def test_is_sorted( - constructor_eager: Any, + constructor_eager: ConstructorEager, input_data: str, descending: bool, # noqa: FBT001 expected: bool, # noqa: FBT001 @@ -27,7 +26,7 @@ def test_is_sorted( compare_dicts({"a": [result]}, {"a": [expected]}) -def test_is_sorted_invalid(constructor_eager: Any) -> None: +def test_is_sorted_invalid(constructor_eager: ConstructorEager) -> None: series = nw.from_native(constructor_eager({"a": data_sorted}), eager_only=True)["a"] with pytest.raises(TypeError): diff --git a/tests/series_only/item_test.py b/tests/series_only/item_test.py index 869bd7c38..4c199578b 100644 --- a/tests/series_only/item_test.py +++ b/tests/series_only/item_test.py @@ -1,18 +1,18 @@ from __future__ import annotations import re -from typing import Any import pytest import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = [1, 3, 2] @pytest.mark.parametrize(("index", "expected"), [(0, 1), (1, 3)]) -def test_item(constructor_eager: Any, index: int, expected: int) -> None: +def test_item(constructor_eager: ConstructorEager, index: int, expected: int) -> None: series = nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"] result = series.item(index) compare_dicts({"a": [result]}, {"a": [expected]}) diff --git a/tests/series_only/scatter_test.py b/tests/series_only/scatter_test.py index 0677a8dd8..9e4bb08af 100644 --- a/tests/series_only/scatter_test.py +++ b/tests/series_only/scatter_test.py @@ -1,14 +1,15 @@ from __future__ import annotations -from typing import Any - import pytest import narwhals as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts -def test_scatter(constructor_eager: Any, request: pytest.FixtureRequest) -> None: +def test_scatter( + constructor_eager: ConstructorEager, request: pytest.FixtureRequest +) -> None: if "modin" in str(constructor_eager): # https://github.com/modin-project/modin/issues/7392 request.applymarker(pytest.mark.xfail) @@ -26,7 +27,7 @@ def test_scatter(constructor_eager: Any, request: pytest.FixtureRequest) -> None compare_dicts(result, expected) -def test_scatter_unchanged(constructor_eager: Any) -> None: +def test_scatter_unchanged(constructor_eager: ConstructorEager) -> None: df = nw.from_native( constructor_eager({"a": [1, 2, 3], "b": [142, 124, 132]}), eager_only=True ) @@ -40,7 +41,7 @@ def test_scatter_unchanged(constructor_eager: Any) -> None: compare_dicts(df, expected) -def test_single_series(constructor_eager: Any) -> None: +def test_single_series(constructor_eager: ConstructorEager) -> None: df = nw.from_native( constructor_eager({"a": [1, 2, 3], "b": [142, 124, 132]}), eager_only=True ) diff --git a/tests/series_only/shape_test.py b/tests/series_only/shape_test.py index 4a1c0726d..d3e276bb2 100644 --- a/tests/series_only/shape_test.py +++ b/tests/series_only/shape_test.py @@ -1,9 +1,8 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager -def test_shape(constructor_eager: Any) -> None: +def test_shape(constructor_eager: ConstructorEager) -> None: result = nw.from_native(constructor_eager({"a": [1, 2]}), eager_only=True)["a"].shape expected = (2,) assert result == expected diff --git a/tests/series_only/slice_test.py b/tests/series_only/slice_test.py index 9ae194774..eba24fdbd 100644 --- a/tests/series_only/slice_test.py +++ b/tests/series_only/slice_test.py @@ -1,10 +1,9 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts -def test_slice(constructor_eager: Any) -> None: +def test_slice(constructor_eager: ConstructorEager) -> None: data = {"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [1, 4, 2]} df = nw.from_native(constructor_eager(data), eager_only=True) result = {"a": df["a"][[0, 1]]} diff --git a/tests/series_only/to_arrow_test.py b/tests/series_only/to_arrow_test.py index 5181a6786..ae6246e55 100644 --- a/tests/series_only/to_arrow_test.py +++ b/tests/series_only/to_arrow_test.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any +from typing import TYPE_CHECKING import pyarrow as pa import pyarrow.compute as pc @@ -8,8 +8,11 @@ import narwhals.stable.v1 as nw +if TYPE_CHECKING: + from tests.utils import ConstructorEager -def test_to_arrow(constructor_eager: Any) -> None: + +def test_to_arrow(constructor_eager: ConstructorEager) -> None: data = [1, 2, 3] result = nw.from_native(constructor_eager({"a": data}), eager_only=True)[ "a" @@ -20,7 +23,7 @@ def test_to_arrow(constructor_eager: Any) -> None: def test_to_arrow_with_nulls( - constructor_eager: Any, request: pytest.FixtureRequest + constructor_eager: ConstructorEager, request: pytest.FixtureRequest ) -> None: if "pandas_constructor" in str(constructor_eager) or "modin_constructor" in str( constructor_eager diff --git a/tests/series_only/to_dummy_test.py b/tests/series_only/to_dummy_test.py index 2cf7f59c7..938b8d04e 100644 --- a/tests/series_only/to_dummy_test.py +++ b/tests/series_only/to_dummy_test.py @@ -1,15 +1,14 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = [1, 2, 3] @pytest.mark.parametrize("sep", ["_", "-"]) -def test_to_dummies(constructor_eager: Any, sep: str) -> None: +def test_to_dummies(constructor_eager: ConstructorEager, sep: str) -> None: s = nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"].alias("a") result = s.to_dummies(separator=sep) expected = {f"a{sep}1": [1, 0, 0], f"a{sep}2": [0, 1, 0], f"a{sep}3": [0, 0, 1]} @@ -18,7 +17,7 @@ def test_to_dummies(constructor_eager: Any, sep: str) -> None: @pytest.mark.parametrize("sep", ["_", "-"]) -def test_to_dummies_drop_first(constructor_eager: Any, sep: str) -> None: +def test_to_dummies_drop_first(constructor_eager: ConstructorEager, sep: str) -> None: s = nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"].alias("a") result = s.to_dummies(drop_first=True, separator=sep) expected = {f"a{sep}2": [0, 1, 0], f"a{sep}3": [0, 0, 1]} diff --git a/tests/series_only/to_frame_test.py b/tests/series_only/to_frame_test.py index 890036183..065da1414 100644 --- a/tests/series_only/to_frame_test.py +++ b/tests/series_only/to_frame_test.py @@ -1,12 +1,11 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = [1, 2, 3] -def test_to_frame(constructor_eager: Any) -> None: +def test_to_frame(constructor_eager: ConstructorEager) -> None: df = ( nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"] .alias("") diff --git a/tests/series_only/to_list_test.py b/tests/series_only/to_list_test.py index 11d02d0d2..0f91b9879 100644 --- a/tests/series_only/to_list_test.py +++ b/tests/series_only/to_list_test.py @@ -1,14 +1,15 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = [1, 2, 3] -def test_to_list(constructor_eager: Any, request: pytest.FixtureRequest) -> None: +def test_to_list( + constructor_eager: ConstructorEager, request: pytest.FixtureRequest +) -> None: if "cudf" in str(constructor_eager): # pragma: no cover request.applymarker(pytest.mark.xfail) s = nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"] diff --git a/tests/series_only/to_native_test.py b/tests/series_only/to_native_test.py index 269348ea3..e6955b4c3 100644 --- a/tests/series_only/to_native_test.py +++ b/tests/series_only/to_native_test.py @@ -1,14 +1,17 @@ from __future__ import annotations -from typing import Any +from typing import TYPE_CHECKING import narwhals.stable.v1 as nw +if TYPE_CHECKING: + from tests.utils import ConstructorEager + data = [4, 4, 4, 1, 6, 6, 4, 4, 1, 1] -def test_to_native(constructor_eager: Any) -> None: - orig_series = constructor_eager({"a": data})["a"] +def test_to_native(constructor_eager: ConstructorEager) -> None: + orig_series = constructor_eager({"a": data})["a"] # type: ignore[index] nw_series = nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"] result = nw_series.to_native() assert isinstance(result, orig_series.__class__) diff --git a/tests/series_only/to_numpy_test.py b/tests/series_only/to_numpy_test.py index 2f1464a57..966a44449 100644 --- a/tests/series_only/to_numpy_test.py +++ b/tests/series_only/to_numpy_test.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any +from typing import TYPE_CHECKING import numpy as np import pytest @@ -8,8 +8,13 @@ import narwhals.stable.v1 as nw +if TYPE_CHECKING: + from tests.utils import ConstructorEager -def test_to_numpy(constructor_eager: Any, request: pytest.FixtureRequest) -> None: + +def test_to_numpy( + constructor_eager: ConstructorEager, request: pytest.FixtureRequest +) -> None: if ( "pandas_constructor" in str(constructor_eager) or "modin_constructor" in str(constructor_eager) diff --git a/tests/series_only/to_pandas_test.py b/tests/series_only/to_pandas_test.py index 30c7906c7..46d7df6da 100644 --- a/tests/series_only/to_pandas_test.py +++ b/tests/series_only/to_pandas_test.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any +from typing import TYPE_CHECKING import pandas as pd import pytest @@ -9,13 +9,18 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +if TYPE_CHECKING: + from tests.utils import ConstructorEager + data = [1, 3, 2] @pytest.mark.skipif( parse_version(pd.__version__) < parse_version("2.0.0"), reason="too old for pyarrow" ) -def test_convert(request: pytest.FixtureRequest, constructor_eager: Any) -> None: +def test_convert( + request: pytest.FixtureRequest, constructor_eager: ConstructorEager +) -> None: if any( cname in str(constructor_eager) for cname in ("pandas_nullable", "pandas_pyarrow", "modin") diff --git a/tests/series_only/value_counts_test.py b/tests/series_only/value_counts_test.py index d19a1440b..342ad7272 100644 --- a/tests/series_only/value_counts_test.py +++ b/tests/series_only/value_counts_test.py @@ -7,6 +7,7 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +from tests.utils import ConstructorEager from tests.utils import compare_dicts data = [4, 4, 4, 1, 6, 6, 4, 4, 1, 1] @@ -16,7 +17,7 @@ @pytest.mark.parametrize("name", [None, "count_name"]) def test_value_counts( request: pytest.FixtureRequest, - constructor_eager: Any, + constructor_eager: ConstructorEager, normalize: Any, name: str | None, ) -> None: diff --git a/tests/series_only/zip_with_test.py b/tests/series_only/zip_with_test.py index 5d1461da3..2de31c060 100644 --- a/tests/series_only/zip_with_test.py +++ b/tests/series_only/zip_with_test.py @@ -1,12 +1,11 @@ from __future__ import annotations -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager from tests.utils import compare_dicts -def test_zip_with(constructor_eager: Any) -> None: +def test_zip_with(constructor_eager: ConstructorEager) -> None: series1 = nw.from_native(constructor_eager({"a": [1, 3, 2]}), eager_only=True)["a"] series2 = nw.from_native(constructor_eager({"a": [4, 4, 6]}), eager_only=True)["a"] mask = nw.from_native(constructor_eager({"a": [True, False, True]}), eager_only=True)[ @@ -18,7 +17,7 @@ def test_zip_with(constructor_eager: Any) -> None: compare_dicts({"a": result}, {"a": expected}) -def test_zip_with_length_1(constructor_eager: Any) -> None: +def test_zip_with_length_1(constructor_eager: ConstructorEager) -> None: series1 = nw.from_native(constructor_eager({"a": [1]}), eager_only=True)["a"] series2 = nw.from_native(constructor_eager({"a": [4]}), eager_only=True)["a"] mask = nw.from_native(constructor_eager({"a": [False]}), eager_only=True)["a"] diff --git a/tests/translate/to_native_test.py b/tests/translate/to_native_test.py index 03d7704ec..90ec11ab1 100644 --- a/tests/translate/to_native_test.py +++ b/tests/translate/to_native_test.py @@ -4,6 +4,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import ConstructorEager @pytest.mark.parametrize( @@ -20,7 +21,7 @@ ], ) def test_to_native( - constructor_eager: Any, method: str, strict: Any, context: Any + constructor_eager: ConstructorEager, method: str, strict: Any, context: Any ) -> None: df = nw.from_native(constructor_eager({"a": [1, 2, 3]})) diff --git a/tests/utils.py b/tests/utils.py index 15ce25140..302f26f1d 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -10,6 +10,7 @@ import pandas as pd +from narwhals.typing import IntoDataFrame from narwhals.typing import IntoFrame from narwhals.utils import Implementation @@ -19,6 +20,7 @@ from typing_extensions import TypeAlias # pragma: no cover Constructor: TypeAlias = Callable[[Any], IntoFrame] +ConstructorEager: TypeAlias = Callable[[Any], IntoDataFrame] def zip_strict(left: Sequence[Any], right: Sequence[Any]) -> Iterator[Any]: @@ -52,7 +54,7 @@ def compare_dicts(result: Any, expected: dict[str, Any]) -> None: rhs = rhs.item() # noqa: PLW2901 if isinstance(lhs, float) and not math.isnan(lhs): assert math.isclose(lhs, rhs, rel_tol=0, abs_tol=1e-6), (lhs, rhs) - elif isinstance(lhs, float) and math.isnan(lhs): + elif isinstance(lhs, float) and math.isnan(lhs) and rhs is not None: assert math.isnan(rhs), (lhs, rhs) # pragma: no cover elif pd.isna(lhs): assert pd.isna(rhs), (lhs, rhs)