From 7214060bbee59a788f37a42f9163406de1c3a410 Mon Sep 17 00:00:00 2001 From: raisadz <34237447+raisadz@users.noreply.github.com> Date: Fri, 13 Sep 2024 11:29:04 +0100 Subject: [PATCH] test: replace type `Any` for constructor in all tests (#962) --- requirements-dev.txt | 1 + tests/conftest.py | 3 +- tests/expr_and_series/abs_test.py | 3 +- tests/expr_and_series/all_horizontal_test.py | 3 +- tests/expr_and_series/any_all_test.py | 3 +- tests/expr_and_series/any_horizontal_test.py | 3 +- tests/expr_and_series/arg_true_test.py | 3 +- tests/expr_and_series/arithmetic_test.py | 5 ++- tests/expr_and_series/binary_test.py | 5 +-- tests/expr_and_series/cast_test.py | 9 ++--- tests/expr_and_series/clip_test.py | 3 +- tests/expr_and_series/count_test.py | 3 +- tests/expr_and_series/cum_sum_test.py | 3 +- tests/expr_and_series/diff_test.py | 3 +- tests/expr_and_series/double_selected_test.py | 5 +-- tests/expr_and_series/double_test.py | 7 ++-- tests/expr_and_series/drop_nulls_test.py | 3 +- .../dt/datetime_attributes_test.py | 6 ++- .../dt/datetime_duration_test.py | 3 +- tests/expr_and_series/dt/to_string_test.py | 7 ++-- tests/expr_and_series/fill_null_test.py | 3 +- tests/expr_and_series/filter_test.py | 3 +- tests/expr_and_series/gather_every_test.py | 3 +- tests/expr_and_series/head_test.py | 3 +- tests/expr_and_series/is_between_test.py | 3 +- tests/expr_and_series/is_duplicated_test.py | 3 +- .../expr_and_series/is_first_distinct_test.py | 3 +- tests/expr_and_series/is_in_test.py | 5 ++- .../expr_and_series/is_last_distinct_test.py | 3 +- tests/expr_and_series/is_null_test.py | 3 +- tests/expr_and_series/is_unique_test.py | 3 +- tests/expr_and_series/len_test.py | 7 ++-- tests/expr_and_series/max_test.py | 3 +- tests/expr_and_series/mean_horizontal_test.py | 3 +- tests/expr_and_series/mean_test.py | 3 +- tests/expr_and_series/min_test.py | 3 +- tests/expr_and_series/mode_test.py | 9 ++++- tests/expr_and_series/n_unique_test.py | 3 +- tests/expr_and_series/name/keep_test.py | 8 ++-- tests/expr_and_series/name/map_test.py | 8 ++-- tests/expr_and_series/name/prefix_test.py | 8 ++-- tests/expr_and_series/name/suffix_test.py | 8 ++-- .../expr_and_series/name/to_lowercase_test.py | 8 ++-- .../expr_and_series/name/to_uppercase_test.py | 8 ++-- tests/expr_and_series/null_count_test.py | 3 +- tests/expr_and_series/operators_test.py | 7 ++-- tests/expr_and_series/over_test.py | 8 ++-- tests/expr_and_series/pipe_test.py | 3 +- tests/expr_and_series/quantile_test.py | 3 +- tests/expr_and_series/reduction_test.py | 5 ++- tests/expr_and_series/round_test.py | 3 +- tests/expr_and_series/sample_test.py | 9 +++-- tests/expr_and_series/shift_test.py | 3 +- tests/expr_and_series/std_test.py | 3 +- tests/expr_and_series/str/contains_test.py | 3 +- tests/expr_and_series/str/head_test.py | 3 +- tests/expr_and_series/str/replace_test.py | 5 ++- tests/expr_and_series/str/slice_test.py | 3 +- .../str/starts_with_ends_with_test.py | 5 ++- tests/expr_and_series/str/strip_chars_test.py | 5 ++- tests/expr_and_series/str/tail_test.py | 3 +- tests/expr_and_series/str/to_datetime_test.py | 5 +-- .../str/to_uppercase_to_lowercase_test.py | 5 ++- tests/expr_and_series/sum_horizontal_test.py | 5 ++- tests/expr_and_series/sum_test.py | 3 +- tests/expr_and_series/tail_test.py | 5 +-- tests/expr_and_series/unary_test.py | 3 +- tests/expr_and_series/unique_test.py | 3 +- tests/expr_and_series/when_test.py | 25 +++++++----- tests/frame/add_test.py | 5 +-- tests/frame/clone_test.py | 5 +-- tests/frame/columns_test.py | 5 +-- tests/frame/concat_test.py | 7 ++-- tests/frame/double_test.py | 5 +-- tests/frame/drop_nulls_test.py | 7 ++-- tests/frame/drop_test.py | 12 +++++- tests/frame/filter_test.py | 6 +-- tests/frame/gather_every_test.py | 5 +-- tests/frame/head_test.py | 5 +-- tests/frame/join_test.py | 39 ++++++++++--------- tests/frame/lit_test.py | 7 +++- tests/frame/pipe_test.py | 5 +-- tests/frame/rename_test.py | 5 +-- tests/frame/schema_test.py | 5 ++- tests/frame/select_test.py | 7 ++-- tests/frame/sort_test.py | 5 +-- tests/frame/tail_test.py | 4 +- tests/frame/unique_test.py | 7 ++-- tests/frame/with_columns_sequence_test.py | 5 +-- tests/frame/with_columns_test.py | 9 ++--- tests/frame/with_row_index_test.py | 5 +-- tests/from_dict_test.py | 19 +++++---- tests/selectors_test.py | 17 ++++---- tests/stable_api_test.py | 5 +-- tests/test_group_by.py | 19 ++++----- tests/utils.py | 10 +++++ 96 files changed, 318 insertions(+), 240 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 23ff1757e..44d57530d 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -10,4 +10,5 @@ pytest-cov pytest-env hypothesis scikit-learn +typing_extensions dask[dataframe]; python_version >= '3.9' diff --git a/tests/conftest.py b/tests/conftest.py index b53be51f0..85c296daf 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,6 +13,7 @@ from narwhals.typing import IntoDataFrame from narwhals.typing import IntoFrame from narwhals.utils import parse_version +from tests.utils import Constructor with contextlib.suppress(ImportError): import modin.pandas # noqa: F401 @@ -112,5 +113,5 @@ def constructor_eager(request: pytest.FixtureRequest) -> Callable[[Any], IntoDat @pytest.fixture(params=[*eager_constructors, *lazy_constructors]) -def constructor(request: pytest.FixtureRequest) -> Callable[[Any], Any]: +def constructor(request: pytest.FixtureRequest) -> Constructor: return request.param # type: ignore[no-any-return] diff --git a/tests/expr_and_series/abs_test.py b/tests/expr_and_series/abs_test.py index e684528b8..286bcca19 100644 --- a/tests/expr_and_series/abs_test.py +++ b/tests/expr_and_series/abs_test.py @@ -1,10 +1,11 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_abs(constructor: Any) -> None: +def test_abs(constructor: Constructor) -> None: df = nw.from_native(constructor({"a": [1, 2, 3, -4, 5]})) result = df.select(b=nw.col("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 256c45deb..bc9f80358 100644 --- a/tests/expr_and_series/all_horizontal_test.py +++ b/tests/expr_and_series/all_horizontal_test.py @@ -3,12 +3,13 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts @pytest.mark.parametrize("expr1", ["a", nw.col("a")]) @pytest.mark.parametrize("expr2", ["b", nw.col("b")]) -def test_allh(constructor: Any, expr1: Any, expr2: Any) -> None: +def test_allh(constructor: Constructor, expr1: Any, expr2: Any) -> 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 09cc8c9e3..834a91202 100644 --- a/tests/expr_and_series/any_all_test.py +++ b/tests/expr_and_series/any_all_test.py @@ -1,10 +1,11 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_any_all(constructor: Any) -> None: +def test_any_all(constructor: Constructor) -> None: df = nw.from_native( constructor( { diff --git a/tests/expr_and_series/any_horizontal_test.py b/tests/expr_and_series/any_horizontal_test.py index 1f19aa304..1b6dfd48d 100644 --- a/tests/expr_and_series/any_horizontal_test.py +++ b/tests/expr_and_series/any_horizontal_test.py @@ -3,12 +3,13 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts @pytest.mark.parametrize("expr1", ["a", nw.col("a")]) @pytest.mark.parametrize("expr2", ["b", nw.col("b")]) -def test_anyh(constructor: Any, expr1: Any, expr2: Any) -> None: +def test_anyh(constructor: Constructor, expr1: Any, expr2: Any) -> None: data = { "a": [False, False, True], "b": [False, True, True], diff --git a/tests/expr_and_series/arg_true_test.py b/tests/expr_and_series/arg_true_test.py index fd5d67a27..7e1262aa8 100644 --- a/tests/expr_and_series/arg_true_test.py +++ b/tests/expr_and_series/arg_true_test.py @@ -3,10 +3,11 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_arg_true(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_arg_true(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) df = nw.from_native(constructor({"a": [1, None, None, 3]})) diff --git a/tests/expr_and_series/arithmetic_test.py b/tests/expr_and_series/arithmetic_test.py index 5278ba8c1..e431aebbe 100644 --- a/tests/expr_and_series/arithmetic_test.py +++ b/tests/expr_and_series/arithmetic_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 compare_dicts @@ -32,7 +33,7 @@ def test_arithmetic_expr( attr: str, rhs: Any, expected: list[Any], - constructor: Any, + constructor: Constructor, request: pytest.FixtureRequest, ) -> None: if attr == "__mod__" and any( @@ -62,7 +63,7 @@ def test_right_arithmetic_expr( attr: str, rhs: Any, expected: list[Any], - constructor: Any, + constructor: Constructor, request: pytest.FixtureRequest, ) -> None: if attr == "__rmod__" and any( diff --git a/tests/expr_and_series/binary_test.py b/tests/expr_and_series/binary_test.py index 2d55af228..1ce76d9d2 100644 --- a/tests/expr_and_series/binary_test.py +++ b/tests/expr_and_series/binary_test.py @@ -1,10 +1,9 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_expr_binary(constructor: Any) -> None: +def test_expr_binary(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df_raw = constructor(data) result = nw.from_native(df_raw).with_columns( diff --git a/tests/expr_and_series/cast_test.py b/tests/expr_and_series/cast_test.py index a81a99508..00f242148 100644 --- a/tests/expr_and_series/cast_test.py +++ b/tests/expr_and_series/cast_test.py @@ -1,11 +1,10 @@ -from typing import Any - import pandas as pd import pyarrow as pa import pytest import narwhals.stable.v1 as nw from narwhals.utils import parse_version +from tests.utils import Constructor data = { "a": [1], @@ -46,7 +45,7 @@ @pytest.mark.filterwarnings("ignore:casting period[M] values to int64:FutureWarning") -def test_cast(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_cast(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "pyarrow_table_constructor" in str(constructor) and parse_version( pa.__version__ ) <= (15,): # pragma: no cover @@ -96,7 +95,7 @@ def test_cast(constructor: Any, request: pytest.FixtureRequest) -> None: assert dict(result.collect_schema()) == expected -def test_cast_series(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_cast_series(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "pyarrow_table_constructor" in str(constructor) and parse_version( pa.__version__ ) <= (15,): # pragma: no cover @@ -163,7 +162,7 @@ def test_cast_string() -> None: def test_cast_raises_for_unknown_dtype( - constructor: Any, request: pytest.FixtureRequest + constructor: Constructor, request: pytest.FixtureRequest ) -> None: if "pyarrow_table_constructor" in str(constructor) and parse_version( pa.__version__ diff --git a/tests/expr_and_series/clip_test.py b/tests/expr_and_series/clip_test.py index 909b153b7..d3f90633c 100644 --- a/tests/expr_and_series/clip_test.py +++ b/tests/expr_and_series/clip_test.py @@ -1,10 +1,11 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_clip(constructor: Any) -> None: +def test_clip(constructor: Constructor) -> None: df = nw.from_native(constructor({"a": [1, 2, 3, -4, 5]})) result = df.select( lower_only=nw.col("a").clip(lower_bound=3), diff --git a/tests/expr_and_series/count_test.py b/tests/expr_and_series/count_test.py index 208df3bc1..580bd202b 100644 --- a/tests/expr_and_series/count_test.py +++ b/tests/expr_and_series/count_test.py @@ -1,10 +1,11 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_count(constructor: Any) -> None: +def test_count(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, None, 6], "z": [7.0, None, None]} df = nw.from_native(constructor(data)) result = df.select(nw.col("a", "b", "z").count()) diff --git a/tests/expr_and_series/cum_sum_test.py b/tests/expr_and_series/cum_sum_test.py index e169b28f9..94897a850 100644 --- a/tests/expr_and_series/cum_sum_test.py +++ b/tests/expr_and_series/cum_sum_test.py @@ -1,6 +1,7 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -10,7 +11,7 @@ } -def test_cum_sum_simple(constructor: Any) -> None: +def test_cum_sum_simple(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.col("a", "b", "c").cum_sum()) expected = { diff --git a/tests/expr_and_series/diff_test.py b/tests/expr_and_series/diff_test.py index 508a2060c..33445f763 100644 --- a/tests/expr_and_series/diff_test.py +++ b/tests/expr_and_series/diff_test.py @@ -5,6 +5,7 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -14,7 +15,7 @@ } -def test_diff(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_diff(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "pyarrow_table_constructor" in str(constructor) and parse_version( pa.__version__ ) < (13,): diff --git a/tests/expr_and_series/double_selected_test.py b/tests/expr_and_series/double_selected_test.py index 7b8fd6703..88826fb40 100644 --- a/tests/expr_and_series/double_selected_test.py +++ b/tests/expr_and_series/double_selected_test.py @@ -1,10 +1,9 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_double_selected(constructor: Any) -> None: +def test_double_selected(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) diff --git a/tests/expr_and_series/double_test.py b/tests/expr_and_series/double_test.py index 3a6b622b8..8f19e0202 100644 --- a/tests/expr_and_series/double_test.py +++ b/tests/expr_and_series/double_test.py @@ -1,10 +1,9 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_double(constructor: Any) -> None: +def test_double(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) result = df.with_columns(nw.all() * 2) @@ -12,7 +11,7 @@ def test_double(constructor: Any) -> None: compare_dicts(result, expected) -def test_double_alias(constructor: Any) -> None: +def test_double_alias(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) result = df.with_columns(nw.col("a").alias("o"), nw.all() * 2) diff --git a/tests/expr_and_series/drop_nulls_test.py b/tests/expr_and_series/drop_nulls_test.py index 1752d68d9..bc06eec3a 100644 --- a/tests/expr_and_series/drop_nulls_test.py +++ b/tests/expr_and_series/drop_nulls_test.py @@ -5,10 +5,11 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_drop_nulls(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_drop_nulls(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) data = { diff --git a/tests/expr_and_series/dt/datetime_attributes_test.py b/tests/expr_and_series/dt/datetime_attributes_test.py index 99cb66ebf..5b9519f57 100644 --- a/tests/expr_and_series/dt/datetime_attributes_test.py +++ b/tests/expr_and_series/dt/datetime_attributes_test.py @@ -7,6 +7,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -34,7 +35,10 @@ ], ) def test_datetime_attributes( - request: pytest.FixtureRequest, constructor: Any, attribute: str, expected: list[int] + request: pytest.FixtureRequest, + constructor: Constructor, + attribute: str, + expected: list[int], ) -> None: if ( attribute == "date" diff --git a/tests/expr_and_series/dt/datetime_duration_test.py b/tests/expr_and_series/dt/datetime_duration_test.py index 26e091f13..da5ff325b 100644 --- a/tests/expr_and_series/dt/datetime_duration_test.py +++ b/tests/expr_and_series/dt/datetime_duration_test.py @@ -11,6 +11,7 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -38,7 +39,7 @@ ) def test_duration_attributes( request: pytest.FixtureRequest, - constructor: Any, + constructor: Constructor, 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 735017c42..6017c33d2 100644 --- a/tests/expr_and_series/dt/to_string_test.py +++ b/tests/expr_and_series/dt/to_string_test.py @@ -6,6 +6,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts from tests.utils import is_windows @@ -57,7 +58,7 @@ def test_dt_to_string_series(constructor_eager: Any, fmt: str) -> None: ], ) @pytest.mark.skipif(is_windows(), reason="pyarrow breaking on windows") -def test_dt_to_string_expr(constructor: Any, fmt: str) -> None: +def test_dt_to_string_expr(constructor: Constructor, fmt: str) -> None: input_frame = nw.from_native(constructor(data)) expected_col = [datetime.strftime(d, fmt) for d in data["a"]] @@ -130,7 +131,7 @@ def test_dt_to_string_iso_local_datetime_series( ) @pytest.mark.skipif(is_windows(), reason="pyarrow breaking on windows") def test_dt_to_string_iso_local_datetime_expr( - constructor: Any, data: datetime, expected: str + constructor: Constructor, data: datetime, expected: str ) -> None: df = constructor({"a": [data]}) @@ -164,7 +165,7 @@ def test_dt_to_string_iso_local_date_series( ) @pytest.mark.skipif(is_windows(), reason="pyarrow breaking on windows") def test_dt_to_string_iso_local_date_expr( - constructor: Any, data: datetime, expected: str + constructor: Constructor, data: datetime, expected: str ) -> None: df = constructor({"a": [data]}) result = nw.from_native(df).with_columns( diff --git a/tests/expr_and_series/fill_null_test.py b/tests/expr_and_series/fill_null_test.py index 04d6d076f..6efde5ac0 100644 --- a/tests/expr_and_series/fill_null_test.py +++ b/tests/expr_and_series/fill_null_test.py @@ -1,6 +1,7 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -10,7 +11,7 @@ } -def test_fill_null(constructor: Any) -> None: +def test_fill_null(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.with_columns(nw.col("a", "b", "c").fill_null(99)) diff --git a/tests/expr_and_series/filter_test.py b/tests/expr_and_series/filter_test.py index 5fcda22e9..80267d1d0 100644 --- a/tests/expr_and_series/filter_test.py +++ b/tests/expr_and_series/filter_test.py @@ -3,6 +3,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -13,7 +14,7 @@ } -def test_filter(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_filter(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) df = nw.from_native(constructor(data)) diff --git a/tests/expr_and_series/gather_every_test.py b/tests/expr_and_series/gather_every_test.py index 00e798ae5..e01294ef9 100644 --- a/tests/expr_and_series/gather_every_test.py +++ b/tests/expr_and_series/gather_every_test.py @@ -3,6 +3,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": list(range(10))} @@ -11,7 +12,7 @@ @pytest.mark.parametrize("n", [1, 2, 3]) @pytest.mark.parametrize("offset", [1, 2, 3]) def test_gather_every_expr( - constructor: Any, n: int, offset: int, request: pytest.FixtureRequest + constructor: Constructor, n: int, offset: int, request: pytest.FixtureRequest ) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) diff --git a/tests/expr_and_series/head_test.py b/tests/expr_and_series/head_test.py index daa81c479..2a6326921 100644 --- a/tests/expr_and_series/head_test.py +++ b/tests/expr_and_series/head_test.py @@ -5,11 +5,12 @@ import pytest import narwhals as nw +from tests.utils import Constructor from tests.utils import compare_dicts @pytest.mark.parametrize("n", [2, -1]) -def test_head(constructor: Any, n: int, request: pytest.FixtureRequest) -> None: +def test_head(constructor: Constructor, n: int, request: pytest.FixtureRequest) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) if "polars" in str(constructor) and n < 0: diff --git a/tests/expr_and_series/is_between_test.py b/tests/expr_and_series/is_between_test.py index 10c61e9e1..0a9e578ea 100644 --- a/tests/expr_and_series/is_between_test.py +++ b/tests/expr_and_series/is_between_test.py @@ -5,6 +5,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -21,7 +22,7 @@ ("none", [False, True, True, False]), ], ) -def test_is_between(constructor: Any, closed: str, expected: list[bool]) -> None: +def test_is_between(constructor: Constructor, closed: str, expected: list[bool]) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.col("a").is_between(1, 5, closed=closed)) expected_dict = {"a": expected} diff --git a/tests/expr_and_series/is_duplicated_test.py b/tests/expr_and_series/is_duplicated_test.py index 71d165749..7859aed02 100644 --- a/tests/expr_and_series/is_duplicated_test.py +++ b/tests/expr_and_series/is_duplicated_test.py @@ -1,12 +1,13 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": [1, 1, 2], "b": [1, 2, 3], "index": [0, 1, 2]} -def test_is_duplicated_expr(constructor: Any) -> None: +def test_is_duplicated_expr(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.col("a", "b").is_duplicated(), "index").sort("index") expected = {"a": [True, True, False], "b": [False, False, False], "index": [0, 1, 2]} diff --git a/tests/expr_and_series/is_first_distinct_test.py b/tests/expr_and_series/is_first_distinct_test.py index 8521661d6..93ffc5d37 100644 --- a/tests/expr_and_series/is_first_distinct_test.py +++ b/tests/expr_and_series/is_first_distinct_test.py @@ -1,6 +1,7 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -9,7 +10,7 @@ } -def test_is_first_distinct_expr(constructor: Any) -> None: +def test_is_first_distinct_expr(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.all().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 40c7b2718..085b1efbe 100644 --- a/tests/expr_and_series/is_in_test.py +++ b/tests/expr_and_series/is_in_test.py @@ -3,12 +3,13 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": [1, 4, 2, 5]} -def test_expr_is_in(constructor: Any) -> None: +def test_expr_is_in(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.col("a").is_in([4, 5])) expected = {"a": [False, True, False, True]} @@ -24,7 +25,7 @@ def test_ser_is_in(constructor_eager: Any) -> None: compare_dicts(result, expected) -def test_is_in_other(constructor: Any) -> None: +def test_is_in_other(constructor: Constructor) -> None: df_raw = constructor(data) with pytest.raises( NotImplementedError, diff --git a/tests/expr_and_series/is_last_distinct_test.py b/tests/expr_and_series/is_last_distinct_test.py index 2e4709efd..00db7f735 100644 --- a/tests/expr_and_series/is_last_distinct_test.py +++ b/tests/expr_and_series/is_last_distinct_test.py @@ -1,6 +1,7 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -9,7 +10,7 @@ } -def test_is_last_distinct_expr(constructor: Any) -> None: +def test_is_last_distinct_expr(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.all().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 07465fd9b..85ba55dc4 100644 --- a/tests/expr_and_series/is_null_test.py +++ b/tests/expr_and_series/is_null_test.py @@ -1,10 +1,11 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_null(constructor: Any) -> None: +def test_null(constructor: Constructor) -> 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(data_na)) diff --git a/tests/expr_and_series/is_unique_test.py b/tests/expr_and_series/is_unique_test.py index d203c1635..b10f7a68f 100644 --- a/tests/expr_and_series/is_unique_test.py +++ b/tests/expr_and_series/is_unique_test.py @@ -1,6 +1,7 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -10,7 +11,7 @@ } -def test_is_unique_expr(constructor: Any) -> None: +def test_is_unique_expr(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.col("a", "b").is_unique(), "index").sort("index") expected = { diff --git a/tests/expr_and_series/len_test.py b/tests/expr_and_series/len_test.py index d186fc528..b1e1674bf 100644 --- a/tests/expr_and_series/len_test.py +++ b/tests/expr_and_series/len_test.py @@ -3,10 +3,11 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_len_no_filter(constructor: Any) -> None: +def test_len_no_filter(constructor: Constructor) -> None: data = {"a": list("xyz"), "b": [1, 2, 1]} expected = {"l": [3], "l2": [6]} df = nw.from_native(constructor(data)).select( @@ -17,7 +18,7 @@ def test_len_no_filter(constructor: Any) -> None: compare_dicts(df, expected) -def test_len_chaining(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_len_chaining(constructor: Constructor, request: pytest.FixtureRequest) -> None: data = {"a": list("xyz"), "b": [1, 2, 1]} expected = {"a1": [2], "a2": [1]} if "dask" in str(constructor): @@ -30,7 +31,7 @@ def test_len_chaining(constructor: Any, request: pytest.FixtureRequest) -> None: compare_dicts(df, expected) -def test_namespace_len(constructor: Any) -> None: +def test_namespace_len(constructor: Constructor) -> None: df = nw.from_native(constructor({"a": [1, 2, 3], "b": [4, 5, 6]})).select( nw.len(), a=nw.len() ) diff --git a/tests/expr_and_series/max_test.py b/tests/expr_and_series/max_test.py index 83f24dcfe..1ea32531e 100644 --- a/tests/expr_and_series/max_test.py +++ b/tests/expr_and_series/max_test.py @@ -5,13 +5,14 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} @pytest.mark.parametrize("expr", [nw.col("a", "b", "z").max(), nw.max("a", "b", "z")]) -def test_expr_max_expr(constructor: Any, expr: nw.Expr) -> None: +def test_expr_max_expr(constructor: Constructor, expr: nw.Expr) -> None: df = nw.from_native(constructor(data)) result = df.select(expr) expected = {"a": [3], "b": [6], "z": [9.0]} diff --git a/tests/expr_and_series/mean_horizontal_test.py b/tests/expr_and_series/mean_horizontal_test.py index d42d5e324..f4ad35b92 100644 --- a/tests/expr_and_series/mean_horizontal_test.py +++ b/tests/expr_and_series/mean_horizontal_test.py @@ -3,11 +3,12 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts @pytest.mark.parametrize("col_expr", [nw.col("a"), "a"]) -def test_meanh(constructor: Any, col_expr: Any) -> None: +def test_meanh(constructor: Constructor, col_expr: Any) -> None: data = {"a": [1, 3, None, None], "b": [4, None, 6, None]} df = nw.from_native(constructor(data)) result = df.select(horizontal_mean=nw.mean_horizontal(col_expr, nw.col("b"))) diff --git a/tests/expr_and_series/mean_test.py b/tests/expr_and_series/mean_test.py index f648c9b32..50e6fd862 100644 --- a/tests/expr_and_series/mean_test.py +++ b/tests/expr_and_series/mean_test.py @@ -5,13 +5,14 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": [1, 3, 2], "b": [4, 4, 7], "z": [7.0, 8, 9]} @pytest.mark.parametrize("expr", [nw.col("a", "b", "z").mean(), nw.mean("a", "b", "z")]) -def test_expr_mean_expr(constructor: Any, expr: nw.Expr) -> None: +def test_expr_mean_expr(constructor: Constructor, expr: nw.Expr) -> None: df = nw.from_native(constructor(data)) result = df.select(expr) expected = {"a": [2.0], "b": [5.0], "z": [8.0]} diff --git a/tests/expr_and_series/min_test.py b/tests/expr_and_series/min_test.py index 460e5646b..f6e98e416 100644 --- a/tests/expr_and_series/min_test.py +++ b/tests/expr_and_series/min_test.py @@ -5,13 +5,14 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} @pytest.mark.parametrize("expr", [nw.col("a", "b", "z").min(), nw.min("a", "b", "z")]) -def test_expr_min_expr(constructor: Any, expr: nw.Expr) -> None: +def test_expr_min_expr(constructor: Constructor, expr: nw.Expr) -> None: df = nw.from_native(constructor(data)) result = df.select(expr) expected = {"a": [1], "b": [4], "z": [7.0]} diff --git a/tests/expr_and_series/mode_test.py b/tests/expr_and_series/mode_test.py index 7774fab07..8e39405af 100644 --- a/tests/expr_and_series/mode_test.py +++ b/tests/expr_and_series/mode_test.py @@ -5,6 +5,7 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -13,7 +14,9 @@ } -def test_mode_single_expr(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_mode_single_expr( + constructor: Constructor, request: pytest.FixtureRequest +) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) @@ -23,7 +26,9 @@ def test_mode_single_expr(constructor: Any, request: pytest.FixtureRequest) -> N compare_dicts(result, expected) -def test_mode_multi_expr(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_mode_multi_expr( + constructor: Constructor, request: pytest.FixtureRequest +) -> None: if "dask" in str(constructor) or ( "polars" in str(constructor) and parse_version(pl.__version__) >= (1, 7, 0) ): diff --git a/tests/expr_and_series/n_unique_test.py b/tests/expr_and_series/n_unique_test.py index f11be2b1c..3790bb1f3 100644 --- a/tests/expr_and_series/n_unique_test.py +++ b/tests/expr_and_series/n_unique_test.py @@ -1,6 +1,7 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -9,7 +10,7 @@ } -def test_n_unique(constructor: Any) -> None: +def test_n_unique(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.all().n_unique()) expected = {"a": [3], "b": [4]} diff --git a/tests/expr_and_series/name/keep_test.py b/tests/expr_and_series/name/keep_test.py index 0b43abe40..be112d716 100644 --- a/tests/expr_and_series/name/keep_test.py +++ b/tests/expr_and_series/name/keep_test.py @@ -1,32 +1,32 @@ from __future__ import annotations from contextlib import nullcontext as does_not_raise -from typing import Any import polars as pl import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"foo": [1, 2, 3], "BAR": [4, 5, 6]} -def test_keep(constructor: Any) -> None: +def test_keep(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select((nw.col("foo", "BAR") * 2).name.keep()) expected = {k: [e * 2 for e in v] for k, v in data.items()} compare_dicts(result, expected) -def test_keep_after_alias(constructor: Any) -> None: +def test_keep_after_alias(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select((nw.col("foo")).alias("alias_for_foo").name.keep()) expected = {"foo": data["foo"]} compare_dicts(result, expected) -def test_keep_raise_anonymous(constructor: Any) -> None: +def test_keep_raise_anonymous(constructor: Constructor) -> None: df_raw = constructor(data) df = nw.from_native(df_raw) diff --git a/tests/expr_and_series/name/map_test.py b/tests/expr_and_series/name/map_test.py index ff039e30d..5fad9f930 100644 --- a/tests/expr_and_series/name/map_test.py +++ b/tests/expr_and_series/name/map_test.py @@ -1,12 +1,12 @@ from __future__ import annotations from contextlib import nullcontext as does_not_raise -from typing import Any import polars as pl import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"foo": [1, 2, 3], "BAR": [4, 5, 6]} @@ -16,21 +16,21 @@ def map_func(s: str | None) -> str: return str(s)[::-1].lower() -def test_map(constructor: Any) -> None: +def test_map(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select((nw.col("foo", "BAR") * 2).name.map(function=map_func)) expected = {map_func(k): [e * 2 for e in v] for k, v in data.items()} compare_dicts(result, expected) -def test_map_after_alias(constructor: Any) -> None: +def test_map_after_alias(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select((nw.col("foo")).alias("alias_for_foo").name.map(function=map_func)) expected = {map_func("foo"): data["foo"]} compare_dicts(result, expected) -def test_map_raise_anonymous(constructor: Any) -> None: +def test_map_raise_anonymous(constructor: Constructor) -> None: df_raw = constructor(data) df = nw.from_native(df_raw) diff --git a/tests/expr_and_series/name/prefix_test.py b/tests/expr_and_series/name/prefix_test.py index f538d4136..95d72914f 100644 --- a/tests/expr_and_series/name/prefix_test.py +++ b/tests/expr_and_series/name/prefix_test.py @@ -1,33 +1,33 @@ from __future__ import annotations from contextlib import nullcontext as does_not_raise -from typing import Any import polars as pl import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"foo": [1, 2, 3], "BAR": [4, 5, 6]} prefix = "with_prefix_" -def test_prefix(constructor: Any) -> None: +def test_prefix(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select((nw.col("foo", "BAR") * 2).name.prefix(prefix)) expected = {prefix + str(k): [e * 2 for e in v] for k, v in data.items()} compare_dicts(result, expected) -def test_suffix_after_alias(constructor: Any) -> None: +def test_suffix_after_alias(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select((nw.col("foo")).alias("alias_for_foo").name.prefix(prefix)) expected = {prefix + "foo": data["foo"]} compare_dicts(result, expected) -def test_prefix_raise_anonymous(constructor: Any) -> None: +def test_prefix_raise_anonymous(constructor: Constructor) -> None: df_raw = constructor(data) df = nw.from_native(df_raw) diff --git a/tests/expr_and_series/name/suffix_test.py b/tests/expr_and_series/name/suffix_test.py index 0e952449b..1802f26f6 100644 --- a/tests/expr_and_series/name/suffix_test.py +++ b/tests/expr_and_series/name/suffix_test.py @@ -1,33 +1,33 @@ from __future__ import annotations from contextlib import nullcontext as does_not_raise -from typing import Any import polars as pl import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"foo": [1, 2, 3], "BAR": [4, 5, 6]} suffix = "_with_suffix" -def test_suffix(constructor: Any) -> None: +def test_suffix(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select((nw.col("foo", "BAR") * 2).name.suffix(suffix)) expected = {str(k) + suffix: [e * 2 for e in v] for k, v in data.items()} compare_dicts(result, expected) -def test_suffix_after_alias(constructor: Any) -> None: +def test_suffix_after_alias(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select((nw.col("foo")).alias("alias_for_foo").name.suffix(suffix)) expected = {"foo" + suffix: data["foo"]} compare_dicts(result, expected) -def test_suffix_raise_anonymous(constructor: Any) -> None: +def test_suffix_raise_anonymous(constructor: Constructor) -> None: df_raw = constructor(data) df = nw.from_native(df_raw) diff --git a/tests/expr_and_series/name/to_lowercase_test.py b/tests/expr_and_series/name/to_lowercase_test.py index a9e8bfcfd..fedac9cd3 100644 --- a/tests/expr_and_series/name/to_lowercase_test.py +++ b/tests/expr_and_series/name/to_lowercase_test.py @@ -1,32 +1,32 @@ from __future__ import annotations from contextlib import nullcontext as does_not_raise -from typing import Any import polars as pl import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"foo": [1, 2, 3], "BAR": [4, 5, 6]} -def test_to_lowercase(constructor: Any) -> None: +def test_to_lowercase(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select((nw.col("foo", "BAR") * 2).name.to_lowercase()) expected = {k.lower(): [e * 2 for e in v] for k, v in data.items()} compare_dicts(result, expected) -def test_to_lowercase_after_alias(constructor: Any) -> None: +def test_to_lowercase_after_alias(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select((nw.col("BAR")).alias("ALIAS_FOR_BAR").name.to_lowercase()) expected = {"bar": data["BAR"]} compare_dicts(result, expected) -def test_to_lowercase_raise_anonymous(constructor: Any) -> None: +def test_to_lowercase_raise_anonymous(constructor: Constructor) -> None: df_raw = constructor(data) df = nw.from_native(df_raw) diff --git a/tests/expr_and_series/name/to_uppercase_test.py b/tests/expr_and_series/name/to_uppercase_test.py index 035dfeff2..29b70bd99 100644 --- a/tests/expr_and_series/name/to_uppercase_test.py +++ b/tests/expr_and_series/name/to_uppercase_test.py @@ -1,32 +1,32 @@ from __future__ import annotations from contextlib import nullcontext as does_not_raise -from typing import Any import polars as pl import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"foo": [1, 2, 3], "BAR": [4, 5, 6]} -def test_to_uppercase(constructor: Any) -> None: +def test_to_uppercase(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select((nw.col("foo", "BAR") * 2).name.to_uppercase()) expected = {k.upper(): [e * 2 for e in v] for k, v in data.items()} compare_dicts(result, expected) -def test_to_uppercase_after_alias(constructor: Any) -> None: +def test_to_uppercase_after_alias(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select((nw.col("foo")).alias("alias_for_foo").name.to_uppercase()) expected = {"FOO": data["foo"]} compare_dicts(result, expected) -def test_to_uppercase_raise_anonymous(constructor: Any) -> None: +def test_to_uppercase_raise_anonymous(constructor: Constructor) -> None: df_raw = constructor(data) df = nw.from_native(df_raw) diff --git a/tests/expr_and_series/null_count_test.py b/tests/expr_and_series/null_count_test.py index a6cb58f71..6be15ab32 100644 --- a/tests/expr_and_series/null_count_test.py +++ b/tests/expr_and_series/null_count_test.py @@ -1,6 +1,7 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -9,7 +10,7 @@ } -def test_null_count_expr(constructor: Any) -> None: +def test_null_count_expr(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.all().null_count()) expected = { diff --git a/tests/expr_and_series/operators_test.py b/tests/expr_and_series/operators_test.py index 113824a94..e3f39465c 100644 --- a/tests/expr_and_series/operators_test.py +++ b/tests/expr_and_series/operators_test.py @@ -5,6 +5,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts @@ -20,7 +21,7 @@ ], ) def test_comparand_operators_scalar_expr( - constructor: Any, operator: str, expected: list[bool] + constructor: Constructor, operator: str, expected: list[bool] ) -> None: data = {"a": [0, 1, 2]} df = nw.from_native(constructor(data)) @@ -40,7 +41,7 @@ def test_comparand_operators_scalar_expr( ], ) def test_comparand_operators_expr( - constructor: Any, operator: str, expected: list[bool] + constructor: Constructor, operator: str, expected: list[bool] ) -> None: data = {"a": [0, 1, 1], "b": [0, 0, 2]} df = nw.from_native(constructor(data)) @@ -56,7 +57,7 @@ def test_comparand_operators_expr( ], ) def test_logic_operators_expr( - constructor: Any, operator: str, expected: list[bool] + constructor: Constructor, operator: str, expected: list[bool] ) -> None: data = {"a": [True, True, False, False], "b": [True, False, True, False]} df = nw.from_native(constructor(data)) diff --git a/tests/expr_and_series/over_test.py b/tests/expr_and_series/over_test.py index cdb4eb3e9..2abc9a699 100644 --- a/tests/expr_and_series/over_test.py +++ b/tests/expr_and_series/over_test.py @@ -1,9 +1,9 @@ from contextlib import nullcontext as does_not_raise -from typing import Any import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -13,7 +13,7 @@ } -def test_over_single(constructor: Any) -> None: +def test_over_single(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) expected = { "a": ["a", "a", "b", "b", "b"], @@ -36,7 +36,7 @@ def test_over_single(constructor: Any) -> None: compare_dicts(result, expected) -def test_over_multiple(constructor: Any) -> None: +def test_over_multiple(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) expected = { "a": ["a", "a", "b", "b", "b"], @@ -59,7 +59,7 @@ def test_over_multiple(constructor: Any) -> None: compare_dicts(result, expected) -def test_over_invalid(request: pytest.FixtureRequest, constructor: Any) -> None: +def test_over_invalid(request: pytest.FixtureRequest, constructor: Constructor) -> None: if "polars" in str(constructor): request.applymarker(pytest.mark.xfail) diff --git a/tests/expr_and_series/pipe_test.py b/tests/expr_and_series/pipe_test.py index 55de3548b..2134a931b 100644 --- a/tests/expr_and_series/pipe_test.py +++ b/tests/expr_and_series/pipe_test.py @@ -1,13 +1,14 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts input_list = {"a": [2, 4, 6, 8]} expected = [4, 16, 36, 64] -def test_pipe_expr(constructor: Any) -> None: +def test_pipe_expr(constructor: Constructor) -> None: df = nw.from_native(constructor(input_list)) e = df.select(nw.col("a").pipe(lambda x: x**2)) compare_dicts(e, {"a": expected}) diff --git a/tests/expr_and_series/quantile_test.py b/tests/expr_and_series/quantile_test.py index 6530532f0..aae2b3647 100644 --- a/tests/expr_and_series/quantile_test.py +++ b/tests/expr_and_series/quantile_test.py @@ -7,6 +7,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts @@ -22,7 +23,7 @@ ) @pytest.mark.filterwarnings("ignore:the `interpolation=` argument to percentile") def test_quantile_expr( - constructor: Any, + constructor: Constructor, interpolation: Literal["nearest", "higher", "lower", "midpoint", "linear"], expected: dict[str, list[float]], request: pytest.FixtureRequest, diff --git a/tests/expr_and_series/reduction_test.py b/tests/expr_and_series/reduction_test.py index 60750444e..e22080e62 100644 --- a/tests/expr_and_series/reduction_test.py +++ b/tests/expr_and_series/reduction_test.py @@ -5,6 +5,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts @@ -26,7 +27,7 @@ ids=range(5), ) def test_scalar_reduction_select( - constructor: Any, expr: list[Any], expected: dict[str, list[Any]] + constructor: Constructor, expr: list[Any], expected: dict[str, list[Any]] ) -> None: data = {"a": [1, 2, 3], "b": [4, 5, 6]} df = nw.from_native(constructor(data)) @@ -52,7 +53,7 @@ def test_scalar_reduction_select( ids=range(5), ) def test_scalar_reduction_with_columns( - constructor: Any, expr: list[Any], expected: dict[str, list[Any]] + constructor: Constructor, expr: list[Any], expected: dict[str, list[Any]] ) -> None: data = {"a": [1, 2, 3], "b": [4, 5, 6]} df = nw.from_native(constructor(data)) diff --git a/tests/expr_and_series/round_test.py b/tests/expr_and_series/round_test.py index 769e4be11..37d6ce131 100644 --- a/tests/expr_and_series/round_test.py +++ b/tests/expr_and_series/round_test.py @@ -5,11 +5,12 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts @pytest.mark.parametrize("decimals", [0, 1, 2]) -def test_round(constructor: Any, decimals: int) -> None: +def test_round(constructor: Constructor, decimals: int) -> None: data = {"a": [2.12345, 2.56789, 3.901234]} df_raw = constructor(data) df = nw.from_native(df_raw) diff --git a/tests/expr_and_series/sample_test.py b/tests/expr_and_series/sample_test.py index d0e1ea235..039bd31b5 100644 --- a/tests/expr_and_series/sample_test.py +++ b/tests/expr_and_series/sample_test.py @@ -1,11 +1,10 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor -def test_expr_sample(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_expr_sample(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) df = nw.from_native(constructor({"a": [1, 2, 3], "b": [4, 5, 6]})).lazy() @@ -19,7 +18,9 @@ def test_expr_sample(constructor: Any, request: pytest.FixtureRequest) -> None: assert result_series == expected_series -def test_expr_sample_fraction(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_expr_sample_fraction( + constructor: Constructor, request: pytest.FixtureRequest +) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) df = nw.from_native(constructor({"a": [1, 2, 3] * 10, "b": [4, 5, 6] * 10})).lazy() diff --git a/tests/expr_and_series/shift_test.py b/tests/expr_and_series/shift_test.py index 02dbed6b0..b165adf12 100644 --- a/tests/expr_and_series/shift_test.py +++ b/tests/expr_and_series/shift_test.py @@ -3,6 +3,7 @@ import pyarrow as pa import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -13,7 +14,7 @@ } -def test_shift(constructor: Any) -> None: +def test_shift(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.with_columns(nw.col("a", "b", "c").shift(2)).filter(nw.col("i") > 1) expected = { diff --git a/tests/expr_and_series/std_test.py b/tests/expr_and_series/std_test.py index 0c7e2f338..400a6e0af 100644 --- a/tests/expr_and_series/std_test.py +++ b/tests/expr_and_series/std_test.py @@ -1,12 +1,13 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} -def test_std(constructor: Any) -> None: +def test_std(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select( nw.col("a").std().alias("a_ddof_default"), diff --git a/tests/expr_and_series/str/contains_test.py b/tests/expr_and_series/str/contains_test.py index 5cc90f4ad..4dec60fef 100644 --- a/tests/expr_and_series/str/contains_test.py +++ b/tests/expr_and_series/str/contains_test.py @@ -4,6 +4,7 @@ import polars as pl import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"pets": ["cat", "dog", "rabbit and parrot", "dove"]} @@ -12,7 +13,7 @@ df_polars = pl.DataFrame(data) -def test_contains(constructor: Any) -> None: +def test_contains(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.with_columns( nw.col("pets").str.contains("(?i)parrot|Dove").alias("result") diff --git a/tests/expr_and_series/str/head_test.py b/tests/expr_and_series/str/head_test.py index 1160920fd..a4b3e7296 100644 --- a/tests/expr_and_series/str/head_test.py +++ b/tests/expr_and_series/str/head_test.py @@ -1,12 +1,13 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": ["foo", "bars"]} -def test_str_head(constructor: Any) -> None: +def test_str_head(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.col("a").str.head(3)) expected = { diff --git a/tests/expr_and_series/str/replace_test.py b/tests/expr_and_series/str/replace_test.py index 95b5bd87c..b0cffb1b4 100644 --- a/tests/expr_and_series/str/replace_test.py +++ b/tests/expr_and_series/str/replace_test.py @@ -5,6 +5,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts replace_data = [ @@ -92,7 +93,7 @@ def test_str_replace_all_series( replace_data, ) def test_str_replace_expr( - constructor: Any, + constructor: Constructor, data: dict[str, list[str]], pattern: str, value: str, @@ -113,7 +114,7 @@ def test_str_replace_expr( replace_all_data, ) def test_str_replace_all_expr( - constructor: Any, + constructor: Constructor, 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 e4e7905f2..e7fe0efa1 100644 --- a/tests/expr_and_series/str/slice_test.py +++ b/tests/expr_and_series/str/slice_test.py @@ -5,6 +5,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": ["fdas", "edfas"]} @@ -15,7 +16,7 @@ [(1, 2, {"a": ["da", "df"]}), (-2, None, {"a": ["as", "as"]})], ) def test_str_slice( - constructor: Any, offset: int, length: int | None, expected: Any + constructor: Constructor, offset: int, length: int | None, expected: Any ) -> None: df = nw.from_native(constructor(data)) result_frame = df.select(nw.col("a").str.slice(offset, length)) 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 a5101edcb..e8b0afaa9 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 @@ -3,6 +3,7 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor # Don't move this into typechecking block, for coverage # purposes @@ -11,7 +12,7 @@ data = {"a": ["fdas", "edfas"]} -def test_ends_with(constructor: Any) -> None: +def test_ends_with(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.col("a").str.ends_with("das")) expected = { @@ -29,7 +30,7 @@ def test_ends_with_series(constructor_eager: Any) -> None: compare_dicts(result, expected) -def test_starts_with(constructor: Any) -> None: +def test_starts_with(constructor: Constructor) -> None: df = nw.from_native(constructor(data)).lazy() result = df.select(nw.col("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 f6cbcc4fa..3d5b74456 100644 --- a/tests/expr_and_series/str/strip_chars_test.py +++ b/tests/expr_and_series/str/strip_chars_test.py @@ -5,6 +5,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": ["foobar", "bar\n", " baz"]} @@ -17,7 +18,9 @@ ("foo", {"a": ["bar", "bar\n", " baz"]}), ], ) -def test_str_strip_chars(constructor: Any, characters: str | None, expected: Any) -> None: +def test_str_strip_chars( + constructor: Constructor, characters: str | None, expected: Any +) -> None: df = nw.from_native(constructor(data)) result_frame = df.select(nw.col("a").str.strip_chars(characters)) compare_dicts(result_frame, expected) diff --git a/tests/expr_and_series/str/tail_test.py b/tests/expr_and_series/str/tail_test.py index c863cca0e..92d474262 100644 --- a/tests/expr_and_series/str/tail_test.py +++ b/tests/expr_and_series/str/tail_test.py @@ -1,12 +1,13 @@ from typing import Any import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": ["foo", "bars"]} -def test_str_tail(constructor: Any) -> None: +def test_str_tail(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) 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 8c3d1a51a..ad666aa8a 100644 --- a/tests/expr_and_series/str/to_datetime_test.py +++ b/tests/expr_and_series/str/to_datetime_test.py @@ -1,11 +1,10 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import Constructor data = {"a": ["2020-01-01T12:34:56"]} -def test_to_datetime(constructor: Any) -> None: +def test_to_datetime(constructor: Constructor) -> None: result = ( nw.from_native(constructor(data)) .lazy() 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 7aecb4ebd..a2c04ffa7 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 @@ -7,6 +7,7 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +from tests.utils import Constructor from tests.utils import compare_dicts @@ -26,7 +27,7 @@ ], ) def test_str_to_uppercase( - constructor: Any, + constructor: Constructor, data: dict[str, list[str]], expected: dict[str, list[str]], request: pytest.FixtureRequest, @@ -107,7 +108,7 @@ def test_str_to_uppercase_series( ], ) def test_str_to_lowercase( - constructor: Any, + constructor: Constructor, data: dict[str, list[str]], expected: dict[str, list[str]], ) -> None: diff --git a/tests/expr_and_series/sum_horizontal_test.py b/tests/expr_and_series/sum_horizontal_test.py index 4c4ab924c..bfaab7238 100644 --- a/tests/expr_and_series/sum_horizontal_test.py +++ b/tests/expr_and_series/sum_horizontal_test.py @@ -3,11 +3,12 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts @pytest.mark.parametrize("col_expr", [nw.col("a"), "a"]) -def test_sumh(constructor: Any, col_expr: Any) -> None: +def test_sumh(constructor: Constructor, col_expr: Any) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) result = df.with_columns(horizontal_sum=nw.sum_horizontal(col_expr, nw.col("b"))) @@ -20,7 +21,7 @@ def test_sumh(constructor: Any, col_expr: Any) -> None: compare_dicts(result, expected) -def test_sumh_nullable(constructor: Any) -> None: +def test_sumh_nullable(constructor: Constructor) -> None: data = {"a": [1, 8, 3], "b": [4, 5, None]} expected = {"hsum": [5, 13, 3]} diff --git a/tests/expr_and_series/sum_test.py b/tests/expr_and_series/sum_test.py index c61a9ed79..8059a097d 100644 --- a/tests/expr_and_series/sum_test.py +++ b/tests/expr_and_series/sum_test.py @@ -5,13 +5,14 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} @pytest.mark.parametrize("expr", [nw.col("a", "b", "z").sum(), nw.sum("a", "b", "z")]) -def test_expr_sum_expr(constructor: Any, expr: nw.Expr) -> None: +def test_expr_sum_expr(constructor: Constructor, expr: nw.Expr) -> None: df = nw.from_native(constructor(data)) result = df.select(expr) expected = {"a": [6], "b": [14], "z": [24.0]} diff --git a/tests/expr_and_series/tail_test.py b/tests/expr_and_series/tail_test.py index d6065dac0..fc3e6159a 100644 --- a/tests/expr_and_series/tail_test.py +++ b/tests/expr_and_series/tail_test.py @@ -1,15 +1,14 @@ -from __future__ import annotations - from typing import Any import pytest import narwhals as nw +from tests.utils import Constructor from tests.utils import compare_dicts @pytest.mark.parametrize("n", [2, -1]) -def test_head(constructor: Any, n: int, request: pytest.FixtureRequest) -> None: +def test_head(constructor: Constructor, n: int, request: pytest.FixtureRequest) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) if "polars" in str(constructor) and n < 0: diff --git a/tests/expr_and_series/unary_test.py b/tests/expr_and_series/unary_test.py index 54c4f771e..dabab0c03 100644 --- a/tests/expr_and_series/unary_test.py +++ b/tests/expr_and_series/unary_test.py @@ -3,10 +3,11 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_unary(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_unary(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} diff --git a/tests/expr_and_series/unique_test.py b/tests/expr_and_series/unique_test.py index 6cb391a85..5639179ba 100644 --- a/tests/expr_and_series/unique_test.py +++ b/tests/expr_and_series/unique_test.py @@ -3,12 +3,13 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": [1, 1, 2]} -def test_unique_expr(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_unique_expr(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) df = nw.from_native(constructor(data)) diff --git a/tests/expr_and_series/when_test.py b/tests/expr_and_series/when_test.py index f7e26e31b..bcd796a4a 100644 --- a/tests/expr_and_series/when_test.py +++ b/tests/expr_and_series/when_test.py @@ -6,6 +6,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -17,7 +18,7 @@ } -def test_when(constructor: Any) -> None: +def test_when(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.when(nw.col("a") == 1).then(value=3).alias("a_when")) expected = { @@ -26,7 +27,7 @@ def test_when(constructor: Any) -> None: compare_dicts(result, expected) -def test_when_otherwise(constructor: Any) -> None: +def test_when_otherwise(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.when(nw.col("a") == 1).then(3).otherwise(6).alias("a_when")) expected = { @@ -35,7 +36,7 @@ def test_when_otherwise(constructor: Any) -> None: compare_dicts(result, expected) -def test_multiple_conditions(constructor: Any) -> None: +def test_multiple_conditions(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select( nw.when(nw.col("a") < 3, nw.col("c") < 5.0).then(3).alias("a_when") @@ -46,13 +47,15 @@ def test_multiple_conditions(constructor: Any) -> None: compare_dicts(result, expected) -def test_no_arg_when_fail(constructor: Any) -> None: +def test_no_arg_when_fail(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) with pytest.raises((TypeError, ValueError)): df.select(nw.when().then(value=3).alias("a_when")) -def test_value_numpy_array(request: pytest.FixtureRequest, constructor: Any) -> None: +def test_value_numpy_array( + request: pytest.FixtureRequest, constructor: Constructor +) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) @@ -80,7 +83,7 @@ def test_value_series(constructor_eager: Any) -> None: compare_dicts(result, expected) -def test_value_expression(constructor: Any) -> None: +def test_value_expression(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(nw.when(nw.col("a") == 1).then(nw.col("a") + 9).alias("a_when")) expected = { @@ -89,7 +92,9 @@ def test_value_expression(constructor: Any) -> None: compare_dicts(result, expected) -def test_otherwise_numpy_array(request: pytest.FixtureRequest, constructor: Any) -> None: +def test_otherwise_numpy_array( + request: pytest.FixtureRequest, constructor: Constructor +) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) @@ -117,7 +122,9 @@ def test_otherwise_series(constructor_eager: Any) -> None: compare_dicts(result, expected) -def test_otherwise_expression(request: pytest.FixtureRequest, constructor: Any) -> None: +def test_otherwise_expression( + request: pytest.FixtureRequest, constructor: Constructor +) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) @@ -132,7 +139,7 @@ def test_otherwise_expression(request: pytest.FixtureRequest, constructor: Any) def test_when_then_otherwise_into_expr( - request: pytest.FixtureRequest, constructor: Any + request: pytest.FixtureRequest, constructor: Constructor ) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) diff --git a/tests/frame/add_test.py b/tests/frame/add_test.py index b885bb05d..c95fbae97 100644 --- a/tests/frame/add_test.py +++ b/tests/frame/add_test.py @@ -1,10 +1,9 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_add(constructor: Any) -> None: +def test_add(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) result = df.with_columns( diff --git a/tests/frame/clone_test.py b/tests/frame/clone_test.py index a0d105a76..e94183e2e 100644 --- a/tests/frame/clone_test.py +++ b/tests/frame/clone_test.py @@ -1,12 +1,11 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_clone(request: pytest.FixtureRequest, constructor: Any) -> None: +def test_clone(request: pytest.FixtureRequest, constructor: Constructor) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) if "pyarrow_table" in str(constructor): diff --git a/tests/frame/columns_test.py b/tests/frame/columns_test.py index 157051ba3..90a9c922d 100644 --- a/tests/frame/columns_test.py +++ b/tests/frame/columns_test.py @@ -1,12 +1,11 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor @pytest.mark.filterwarnings("ignore:Determining|Resolving.*") -def test_columns(constructor: Any) -> None: +def test_columns(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) result = df.columns diff --git a/tests/frame/concat_test.py b/tests/frame/concat_test.py index a52759128..926f3f988 100644 --- a/tests/frame/concat_test.py +++ b/tests/frame/concat_test.py @@ -1,12 +1,11 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_concat_horizontal(constructor: Any) -> None: +def test_concat_horizontal(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df_left = nw.from_native(constructor(data)).lazy() @@ -27,7 +26,7 @@ def test_concat_horizontal(constructor: Any) -> None: nw.concat([]) -def test_concat_vertical(constructor: Any) -> None: +def test_concat_vertical(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df_left = ( nw.from_native(constructor(data)).lazy().rename({"a": "c", "b": "d"}).drop("z") diff --git a/tests/frame/double_test.py b/tests/frame/double_test.py index 5d52d0d26..6840145ec 100644 --- a/tests/frame/double_test.py +++ b/tests/frame/double_test.py @@ -1,10 +1,9 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_double(constructor: Any) -> None: +def test_double(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) diff --git a/tests/frame/drop_nulls_test.py b/tests/frame/drop_nulls_test.py index 58c9486ed..4c2276030 100644 --- a/tests/frame/drop_nulls_test.py +++ b/tests/frame/drop_nulls_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 Constructor from tests.utils import compare_dicts data = { @@ -13,7 +12,7 @@ } -def test_drop_nulls(constructor: Any) -> None: +def test_drop_nulls(constructor: Constructor) -> None: result = nw.from_native(constructor(data)).drop_nulls() expected = { "a": [2.0, 4.0], @@ -23,7 +22,7 @@ def test_drop_nulls(constructor: Any) -> None: @pytest.mark.parametrize("subset", ["a", ["a"]]) -def test_drop_nulls_subset(constructor: Any, subset: str | list[str]) -> None: +def test_drop_nulls_subset(constructor: Constructor, subset: str | list[str]) -> None: result = nw.from_native(constructor(data)).drop_nulls(subset=subset) expected = { "a": [1, 2.0, 4.0], diff --git a/tests/frame/drop_test.py b/tests/frame/drop_test.py index f1fd6fd87..f8fc33254 100644 --- a/tests/frame/drop_test.py +++ b/tests/frame/drop_test.py @@ -1,6 +1,7 @@ from __future__ import annotations from contextlib import nullcontext as does_not_raise +from typing import TYPE_CHECKING from typing import Any import polars as pl @@ -11,6 +12,9 @@ from narwhals._exceptions import ColumnNotFoundError from narwhals.utils import parse_version +if TYPE_CHECKING: + from tests.utils import Constructor + @pytest.mark.parametrize( ("to_drop", "expected"), @@ -20,7 +24,7 @@ (["abc", "b"], ["z"]), ], ) -def test_drop(constructor: Any, to_drop: list[str], expected: list[str]) -> None: +def test_drop(constructor: Constructor, to_drop: list[str], expected: list[str]) -> None: data = {"abc": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) assert df.drop(to_drop).collect_schema().names() == expected @@ -39,7 +43,11 @@ def test_drop(constructor: Any, to_drop: list[str], expected: list[str]) -> None ], ) def test_drop_strict( - request: pytest.FixtureRequest, constructor: Any, context: Any, *, strict: bool + request: pytest.FixtureRequest, + constructor: Constructor, + context: Any, + *, + strict: bool, ) -> None: if ( "polars_lazy" in str(request) diff --git a/tests/frame/filter_test.py b/tests/frame/filter_test.py index e7a289feb..9c9b1b6fd 100644 --- a/tests/frame/filter_test.py +++ b/tests/frame/filter_test.py @@ -1,13 +1,13 @@ from contextlib import nullcontext as does_not_raise -from typing import Any import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_filter(constructor: Any) -> None: +def test_filter(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) result = df.filter(nw.col("a") > 1) @@ -15,7 +15,7 @@ def test_filter(constructor: Any) -> None: compare_dicts(result, expected) -def test_filter_with_boolean_list(constructor: Any) -> None: +def test_filter_with_boolean_list(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) diff --git a/tests/frame/gather_every_test.py b/tests/frame/gather_every_test.py index 90b06e3d6..40e18a30b 100644 --- a/tests/frame/gather_every_test.py +++ b/tests/frame/gather_every_test.py @@ -1,8 +1,7 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": list(range(10))} @@ -10,7 +9,7 @@ @pytest.mark.parametrize("n", [1, 2, 3]) @pytest.mark.parametrize("offset", [1, 2, 3]) -def test_gather_every(constructor: Any, n: int, offset: int) -> None: +def test_gather_every(constructor: Constructor, n: int, offset: int) -> None: df = nw.from_native(constructor(data)) result = df.gather_every(n=n, offset=offset) expected = {"a": data["a"][offset::n]} diff --git a/tests/frame/head_test.py b/tests/frame/head_test.py index e4b762f48..7234828b0 100644 --- a/tests/frame/head_test.py +++ b/tests/frame/head_test.py @@ -1,12 +1,11 @@ from __future__ import annotations -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_head(constructor: Any) -> None: +def test_head(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} expected = {"a": [1, 3], "b": [4, 4], "z": [7.0, 8.0]} diff --git a/tests/frame/join_test.py b/tests/frame/join_test.py index 78c7c8cd0..85c76eba7 100644 --- a/tests/frame/join_test.py +++ b/tests/frame/join_test.py @@ -11,10 +11,11 @@ import narwhals.stable.v1 as nw from narwhals.utils import Implementation from narwhals.utils import parse_version +from tests.utils import Constructor from tests.utils import compare_dicts -def test_inner_join_two_keys(constructor: Any) -> None: +def test_inner_join_two_keys(constructor: Constructor) -> None: data = { "antananarivo": [1, 3, 2], "bob": [4, 4, 6], @@ -43,7 +44,7 @@ def test_inner_join_two_keys(constructor: Any) -> None: compare_dicts(result_on, expected) -def test_inner_join_single_key(constructor: Any) -> None: +def test_inner_join_single_key(constructor: Constructor) -> None: data = { "antananarivo": [1, 3, 2], "bob": [4, 4, 6], @@ -73,7 +74,7 @@ def test_inner_join_single_key(constructor: Any) -> None: compare_dicts(result_on, expected) -def test_cross_join(constructor: Any) -> None: +def test_cross_join(constructor: Constructor) -> None: data = {"antananarivo": [1, 3, 2]} df = nw.from_native(constructor(data)) result = df.join(df, how="cross").sort("antananarivo", "antananarivo_right") # type: ignore[arg-type] @@ -91,7 +92,7 @@ def test_cross_join(constructor: Any) -> None: @pytest.mark.parametrize("how", ["inner", "left"]) @pytest.mark.parametrize("suffix", ["_right", "_custom_suffix"]) -def test_suffix(constructor: Any, how: str, suffix: str) -> None: +def test_suffix(constructor: Constructor, how: str, suffix: str) -> None: data = { "antananarivo": [1, 3, 2], "bob": [4, 4, 6], @@ -111,7 +112,7 @@ def test_suffix(constructor: Any, how: str, suffix: str) -> None: @pytest.mark.parametrize("suffix", ["_right", "_custom_suffix"]) -def test_cross_join_suffix(constructor: Any, suffix: str) -> None: +def test_cross_join_suffix(constructor: Constructor, suffix: str) -> None: data = {"antananarivo": [1, 3, 2]} df = nw.from_native(constructor(data)) result = df.join(df, how="cross", suffix=suffix).sort( # type: ignore[arg-type] @@ -154,7 +155,7 @@ def test_cross_join_non_pandas() -> None: ], ) def test_anti_join( - constructor: Any, + constructor: Constructor, join_key: list[str], filter_expr: nw.Expr, expected: dict[str, list[Any]], @@ -192,7 +193,7 @@ def test_anti_join( ], ) def test_semi_join( - constructor: Any, + constructor: Constructor, join_key: list[str], filter_expr: nw.Expr, expected: dict[str, list[Any]], @@ -207,7 +208,7 @@ def test_semi_join( @pytest.mark.parametrize("how", ["right", "full"]) -def test_join_not_implemented(constructor: Any, how: str) -> None: +def test_join_not_implemented(constructor: Constructor, how: str) -> None: data = {"antananarivo": [1, 3, 2], "bob": [4, 4, 6], "zorro": [7.0, 8, 9]} df = nw.from_native(constructor(data)) @@ -221,7 +222,7 @@ def test_join_not_implemented(constructor: Any, how: str) -> None: @pytest.mark.filterwarnings("ignore:the default coalesce behavior") -def test_left_join(constructor: Any) -> None: +def test_left_join(constructor: Constructor) -> None: data_left = { "antananarivo": [1.0, 2, 3], "bob": [4.0, 5, 6], @@ -262,7 +263,7 @@ def test_left_join(constructor: Any) -> None: @pytest.mark.filterwarnings("ignore: the default coalesce behavior") -def test_left_join_multiple_column(constructor: Any) -> None: +def test_left_join_multiple_column(constructor: Constructor) -> None: data_left = {"antananarivo": [1, 2, 3], "bob": [4, 5, 6], "index": [0, 1, 2]} data_right = {"antananarivo": [1, 2, 3], "c": [4, 5, 6], "index": [0, 1, 2]} df_left = nw.from_native(constructor(data_left)) @@ -280,7 +281,7 @@ def test_left_join_multiple_column(constructor: Any) -> None: @pytest.mark.filterwarnings("ignore: the default coalesce behavior") -def test_left_join_overlapping_column(constructor: Any) -> None: +def test_left_join_overlapping_column(constructor: Constructor) -> None: data_left = { "antananarivo": [1.0, 2, 3], "bob": [4.0, 5, 6], @@ -326,7 +327,7 @@ def test_left_join_overlapping_column(constructor: Any) -> None: @pytest.mark.parametrize("how", ["inner", "left", "semi", "anti"]) -def test_join_keys_exceptions(constructor: Any, how: str) -> None: +def test_join_keys_exceptions(constructor: Constructor, how: str) -> None: data = {"antananarivo": [1, 3, 2], "bob": [4, 4, 6], "zorro": [7.0, 8, 9]} df = nw.from_native(constructor(data)) @@ -352,7 +353,9 @@ def test_join_keys_exceptions(constructor: Any, how: str) -> None: df.join(df, how=how, on="antananarivo", right_on="antananarivo") # type: ignore[arg-type] -def test_joinasof_numeric(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_joinasof_numeric( + constructor: Constructor, request: pytest.FixtureRequest +) -> None: if "pyarrow_table" in str(constructor) or "cudf" in str(constructor): request.applymarker(pytest.mark.xfail) if parse_version(pd.__version__) < (2, 1) and ( @@ -408,7 +411,7 @@ def test_joinasof_numeric(constructor: Any, request: pytest.FixtureRequest) -> N compare_dicts(result_nearest_on, expected_nearest) -def test_joinasof_time(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_joinasof_time(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "pyarrow_table" in str(constructor) or "cudf" in str(constructor): request.applymarker(pytest.mark.xfail) if parse_version(pd.__version__) < (2, 1) and ("pandas_pyarrow" in str(constructor)): @@ -486,7 +489,7 @@ def test_joinasof_time(constructor: Any, request: pytest.FixtureRequest) -> None compare_dicts(result_nearest_on, expected_nearest) -def test_joinasof_by(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_joinasof_by(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "pyarrow_table" in str(constructor) or "cudf" in str(constructor): request.applymarker(pytest.mark.xfail) if parse_version(pd.__version__) < (2, 1) and ( @@ -521,7 +524,7 @@ def test_joinasof_by(constructor: Any, request: pytest.FixtureRequest) -> None: @pytest.mark.parametrize("strategy", ["back", "furthest"]) def test_joinasof_not_implemented( - constructor: Any, strategy: Literal["backward", "forward"] + constructor: Constructor, strategy: Literal["backward", "forward"] ) -> None: data = {"antananarivo": [1, 3, 2], "bob": [4, 4, 6], "zorro": [7.0, 8, 9]} df = nw.from_native(constructor(data)) @@ -538,7 +541,7 @@ def test_joinasof_not_implemented( ) -def test_joinasof_keys_exceptions(constructor: Any) -> None: +def test_joinasof_keys_exceptions(constructor: Constructor) -> None: data = {"antananarivo": [1, 3, 2], "bob": [4, 4, 6], "zorro": [7.0, 8, 9]} df = nw.from_native(constructor(data)) @@ -579,7 +582,7 @@ def test_joinasof_keys_exceptions(constructor: Any) -> None: df.join_asof(df, right_on="antananarivo", on="antananarivo") # type: ignore[arg-type] -def test_joinasof_by_exceptions(constructor: Any) -> None: +def test_joinasof_by_exceptions(constructor: Constructor) -> None: data = {"antananarivo": [1, 3, 2], "bob": [4, 4, 6], "zorro": [7.0, 8, 9]} df = nw.from_native(constructor(data)) with pytest.raises( diff --git a/tests/frame/lit_test.py b/tests/frame/lit_test.py index e5756e035..aa18edb40 100644 --- a/tests/frame/lit_test.py +++ b/tests/frame/lit_test.py @@ -7,6 +7,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts if TYPE_CHECKING: @@ -17,7 +18,9 @@ ("dtype", "expected_lit"), [(None, [2, 2, 2]), (nw.String, ["2", "2", "2"]), (nw.Float32, [2.0, 2.0, 2.0])], ) -def test_lit(constructor: Any, dtype: DType | None, expected_lit: list[Any]) -> None: +def test_lit( + constructor: Constructor, dtype: DType | None, expected_lit: list[Any] +) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df_raw = constructor(data) df = nw.from_native(df_raw).lazy() @@ -31,7 +34,7 @@ def test_lit(constructor: Any, dtype: DType | None, expected_lit: list[Any]) -> compare_dicts(result, expected) -def test_lit_error(constructor: Any) -> None: +def test_lit_error(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df_raw = constructor(data) df = nw.from_native(df_raw).lazy() diff --git a/tests/frame/pipe_test.py b/tests/frame/pipe_test.py index a9a50133f..b7b57e0a1 100644 --- a/tests/frame/pipe_test.py +++ b/tests/frame/pipe_test.py @@ -1,6 +1,5 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -9,7 +8,7 @@ } -def test_pipe(constructor: Any) -> None: +def test_pipe(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) columns = df.collect_schema().names() result = df.pipe(lambda _df: _df.select([x for x in columns if len(x) == 2])) diff --git a/tests/frame/rename_test.py b/tests/frame/rename_test.py index c58eccd4c..79cf3f243 100644 --- a/tests/frame/rename_test.py +++ b/tests/frame/rename_test.py @@ -1,10 +1,9 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_rename(constructor: Any) -> None: +def test_rename(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) result = df.rename({"a": "x", "b": "y"}) diff --git a/tests/frame/schema_test.py b/tests/frame/schema_test.py index dc8231739..d7ba69ab2 100644 --- a/tests/frame/schema_test.py +++ b/tests/frame/schema_test.py @@ -10,6 +10,7 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +from tests.utils import Constructor data = { "a": [datetime(2020, 1, 1)], @@ -18,7 +19,7 @@ @pytest.mark.filterwarnings("ignore:Determining|Resolving.*") -def test_schema(constructor: Any) -> None: +def test_schema(constructor: Constructor) -> None: df = nw.from_native(constructor({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.1, 8, 9]})) result = df.schema expected = {"a": nw.Int64, "b": nw.Int64, "z": nw.Float64} @@ -29,7 +30,7 @@ def test_schema(constructor: Any) -> None: assert result == expected -def test_collect_schema(constructor: Any) -> None: +def test_collect_schema(constructor: Constructor) -> None: df = nw.from_native(constructor({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.1, 8, 9]})) expected = {"a": nw.Int64, "b": nw.Int64, "z": nw.Float64} diff --git a/tests/frame/select_test.py b/tests/frame/select_test.py index 450e91066..8c01be407 100644 --- a/tests/frame/select_test.py +++ b/tests/frame/select_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 Constructor from tests.utils import compare_dicts -def test_select(constructor: Any) -> None: +def test_select(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) result = df.select("a") @@ -15,7 +14,7 @@ def test_select(constructor: Any) -> None: compare_dicts(result, expected) -def test_empty_select(constructor: Any) -> None: +def test_empty_select(constructor: Constructor) -> None: result = nw.from_native(constructor({"a": [1, 2, 3]})).lazy().select() assert result.collect().shape == (0, 0) diff --git a/tests/frame/sort_test.py b/tests/frame/sort_test.py index 9e583f8ba..06f5d079f 100644 --- a/tests/frame/sort_test.py +++ b/tests/frame/sort_test.py @@ -1,10 +1,9 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_sort(constructor: Any) -> None: +def test_sort(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) result = df.sort("a", "b") diff --git a/tests/frame/tail_test.py b/tests/frame/tail_test.py index b64d9fa6c..f7e06475c 100644 --- a/tests/frame/tail_test.py +++ b/tests/frame/tail_test.py @@ -1,15 +1,15 @@ from __future__ import annotations from contextlib import nullcontext as does_not_raise -from typing import Any import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_tail(constructor: Any) -> None: +def test_tail(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} expected = {"a": [3, 2], "b": [4, 6], "z": [8.0, 9]} diff --git a/tests/frame/unique_test.py b/tests/frame/unique_test.py index af61fe82b..40589c545 100644 --- a/tests/frame/unique_test.py +++ b/tests/frame/unique_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 Constructor from tests.utils import compare_dicts data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} @@ -21,7 +20,7 @@ ], ) def test_unique( - constructor: Any, + constructor: Constructor, subset: str | list[str] | None, keep: str, expected: dict[str, list[float]], @@ -33,7 +32,7 @@ def test_unique( compare_dicts(result, expected) -def test_unique_none(constructor: Any) -> None: +def test_unique_none(constructor: Constructor) -> None: df_raw = constructor(data) df = nw.from_native(df_raw) diff --git a/tests/frame/with_columns_sequence_test.py b/tests/frame/with_columns_sequence_test.py index b749ac0ca..49db7820b 100644 --- a/tests/frame/with_columns_sequence_test.py +++ b/tests/frame/with_columns_sequence_test.py @@ -1,9 +1,8 @@ -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 compare_dicts data = { @@ -12,7 +11,7 @@ } -def test_with_columns(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_with_columns(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) result = ( diff --git a/tests/frame/with_columns_test.py b/tests/frame/with_columns_test.py index 864e689e8..44bcd39a5 100644 --- a/tests/frame/with_columns_test.py +++ b/tests/frame/with_columns_test.py @@ -1,9 +1,8 @@ -from typing import Any - import numpy as np import pandas as pd import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts @@ -18,7 +17,7 @@ def test_with_columns_int_col_name_pandas() -> None: pd.testing.assert_frame_equal(result, expected) -def test_with_columns_order(constructor: Any) -> None: +def test_with_columns_order(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) result = df.with_columns(nw.col("a") + 1, d=nw.col("a") - 1) @@ -27,14 +26,14 @@ def test_with_columns_order(constructor: Any) -> None: compare_dicts(result, expected) -def test_with_columns_empty(constructor: Any) -> None: +def test_with_columns_empty(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]} df = nw.from_native(constructor(data)) result = df.select().with_columns() compare_dicts(result, {}) -def test_with_columns_order_single_row(constructor: Any) -> None: +def test_with_columns_order_single_row(constructor: Constructor) -> None: data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9], "i": [0, 1, 2]} df = nw.from_native(constructor(data)).filter(nw.col("i") < 1).drop("i") result = df.with_columns(nw.col("a") + 1, d=nw.col("a") - 1) diff --git a/tests/frame/with_row_index_test.py b/tests/frame/with_row_index_test.py index bc1c2fe0a..8f802de0a 100644 --- a/tests/frame/with_row_index_test.py +++ b/tests/frame/with_row_index_test.py @@ -1,6 +1,5 @@ -from typing import Any - import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -9,7 +8,7 @@ } -def test_with_row_index(constructor: Any) -> None: +def test_with_row_index(constructor: Constructor) -> None: result = nw.from_native(constructor(data)).with_row_index() expected = {"a": ["foo", "bars"], "ab": ["foo", "bars"], "index": [0, 1]} compare_dicts(result, expected) diff --git a/tests/from_dict_test.py b/tests/from_dict_test.py index c3221d071..a1332908a 100644 --- a/tests/from_dict_test.py +++ b/tests/from_dict_test.py @@ -1,12 +1,11 @@ -from typing import Any - import pytest import narwhals.stable.v1 as nw +from tests.utils import Constructor from tests.utils import compare_dicts -def test_from_dict(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_from_dict(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) df = nw.from_native(constructor({"a": [1, 2, 3], "b": [4, 5, 6]})) @@ -17,7 +16,9 @@ def test_from_dict(constructor: Any, request: pytest.FixtureRequest) -> None: assert isinstance(result, nw.DataFrame) -def test_from_dict_schema(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_from_dict_schema( + constructor: Constructor, request: pytest.FixtureRequest +) -> None: if "dask" in str(constructor): request.applymarker(pytest.mark.xfail) schema = {"c": nw.Int16(), "d": nw.Float32()} @@ -31,19 +32,23 @@ def test_from_dict_schema(constructor: Any, request: pytest.FixtureRequest) -> N assert result.collect_schema() == schema -def test_from_dict_without_namespace(constructor: Any) -> None: +def test_from_dict_without_namespace(constructor: Constructor) -> None: df = nw.from_native(constructor({"a": [1, 2, 3], "b": [4, 5, 6]})).lazy().collect() result = nw.from_dict({"c": df["a"], "d": df["b"]}) compare_dicts(result, {"c": [1, 2, 3], "d": [4, 5, 6]}) -def test_from_dict_without_namespace_invalid(constructor: Any) -> None: +def test_from_dict_without_namespace_invalid( + constructor: Constructor, +) -> None: df = nw.from_native(constructor({"a": [1, 2, 3], "b": [4, 5, 6]})).lazy().collect() with pytest.raises(TypeError, match="namespace"): nw.from_dict({"c": nw.to_native(df["a"]), "d": nw.to_native(df["b"])}) -def test_from_dict_one_native_one_narwhals(constructor: Any) -> None: +def test_from_dict_one_native_one_narwhals( + constructor: Constructor, +) -> None: df = nw.from_native(constructor({"a": [1, 2, 3], "b": [4, 5, 6]})).lazy().collect() result = nw.from_dict({"c": nw.to_native(df["a"]), "d": df["b"]}) expected = {"c": [1, 2, 3], "d": [4, 5, 6]} diff --git a/tests/selectors_test.py b/tests/selectors_test.py index b136980b5..c78a9eac4 100644 --- a/tests/selectors_test.py +++ b/tests/selectors_test.py @@ -1,7 +1,5 @@ from __future__ import annotations -from typing import Any - import pandas as pd import pyarrow as pa import pytest @@ -14,6 +12,7 @@ from narwhals.selectors import numeric from narwhals.selectors import string from narwhals.utils import parse_version +from tests.utils import Constructor from tests.utils import compare_dicts data = { @@ -24,28 +23,28 @@ } -def test_selectors(constructor: Any) -> None: +def test_selectors(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(by_dtype([nw.Int64, nw.Float64]) + 1) expected = {"a": [2, 2, 3], "c": [5.1, 6.0, 7.0]} compare_dicts(result, expected) -def test_numeric(constructor: Any) -> None: +def test_numeric(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(numeric() + 1) expected = {"a": [2, 2, 3], "c": [5.1, 6.0, 7.0]} compare_dicts(result, expected) -def test_boolean(constructor: Any) -> None: +def test_boolean(constructor: Constructor) -> None: df = nw.from_native(constructor(data)) result = df.select(boolean()) expected = {"d": [True, False, True]} compare_dicts(result, expected) -def test_string(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_string(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "dask" in str(constructor) and parse_version(pa.__version__) < (12,): # Dask doesn't infer `'b'` as String for old PyArrow versions request.applymarker(pytest.mark.xfail) @@ -55,7 +54,7 @@ def test_string(constructor: Any, request: pytest.FixtureRequest) -> None: compare_dicts(result, expected) -def test_categorical(request: pytest.FixtureRequest, constructor: Any) -> None: +def test_categorical(request: pytest.FixtureRequest, constructor: Constructor) -> None: if "pyarrow_table_constructor" in str(constructor) and parse_version( pa.__version__ ) <= (15,): # pragma: no cover @@ -82,7 +81,7 @@ def test_categorical(request: pytest.FixtureRequest, constructor: Any) -> None: ], ) def test_set_ops( - constructor: Any, selector: nw.selectors.Selector, expected: list[str] + constructor: Constructor, selector: nw.selectors.Selector, expected: list[str] ) -> None: df = nw.from_native(constructor(data)) result = df.select(selector).collect_schema().names() @@ -90,7 +89,7 @@ def test_set_ops( @pytest.mark.parametrize("invalid_constructor", [pd.DataFrame, pa.table]) -def test_set_ops_invalid(invalid_constructor: Any) -> None: +def test_set_ops_invalid(invalid_constructor: Constructor) -> None: df = nw.from_native(invalid_constructor(data)) with pytest.raises(NotImplementedError): df.select(1 - numeric()) diff --git a/tests/stable_api_test.py b/tests/stable_api_test.py index 211ba8652..375af4f4d 100644 --- a/tests/stable_api_test.py +++ b/tests/stable_api_test.py @@ -1,14 +1,13 @@ -from typing import Any - import polars as pl import pytest import narwhals as nw import narwhals.stable.v1 as nw_v1 +from tests.utils import Constructor from tests.utils import compare_dicts -def test_renamed_taxicab_norm(constructor: Any) -> None: +def test_renamed_taxicab_norm(constructor: Constructor) -> None: # Suppose we need to rename `_l1_norm` to `_taxicab_norm`. # We need `narwhals.stable.v1` to stay stable. So, we # make the change in `narwhals`, and then add the new method diff --git a/tests/test_group_by.py b/tests/test_group_by.py index 44e1acd42..fa9c05f4b 100644 --- a/tests/test_group_by.py +++ b/tests/test_group_by.py @@ -10,6 +10,7 @@ import narwhals.stable.v1 as nw from narwhals.utils import parse_version +from tests.utils import Constructor from tests.utils import compare_dicts data = {"a": [1, 1, 3], "b": [4, 4, 6], "c": [7.0, 8, 9]} @@ -94,7 +95,7 @@ def test_group_by_iter(constructor_eager: Any) -> None: assert sorted(keys) == sorted(expected_keys) -def test_group_by_len(constructor: Any) -> None: +def test_group_by_len(constructor: Constructor) -> None: result = ( nw.from_native(constructor(data)).group_by("a").agg(nw.col("b").len()).sort("a") ) @@ -102,7 +103,7 @@ def test_group_by_len(constructor: Any) -> None: compare_dicts(result, expected) -def test_group_by_n_unique(constructor: Any) -> None: +def test_group_by_n_unique(constructor: Constructor) -> None: result = ( nw.from_native(constructor(data)) .group_by("a") @@ -113,7 +114,7 @@ def test_group_by_n_unique(constructor: Any) -> None: compare_dicts(result, expected) -def test_group_by_std(constructor: Any) -> None: +def test_group_by_std(constructor: Constructor) -> None: data = {"a": [1, 1, 2, 2], "b": [5, 4, 3, 2]} result = ( nw.from_native(constructor(data)).group_by("a").agg(nw.col("b").std()).sort("a") @@ -122,7 +123,7 @@ def test_group_by_std(constructor: Any) -> None: compare_dicts(result, expected) -def test_group_by_n_unique_w_missing(constructor: Any) -> None: +def test_group_by_n_unique_w_missing(constructor: Constructor) -> None: data = {"a": [1, 1, 2], "b": [4, None, 5], "c": [None, None, 7], "d": [1, 1, 3]} result = ( nw.from_native(constructor(data)) @@ -162,7 +163,7 @@ def test_group_by_empty_result_pandas() -> None: ) -def test_group_by_simple_named(constructor: Any) -> None: +def test_group_by_simple_named(constructor: Constructor) -> None: data = {"a": [1, 1, 2], "b": [4, 5, 6], "c": [7, 2, 1]} df = nw.from_native(constructor(data)).lazy() result = ( @@ -182,7 +183,7 @@ def test_group_by_simple_named(constructor: Any) -> None: compare_dicts(result, expected) -def test_group_by_simple_unnamed(constructor: Any) -> None: +def test_group_by_simple_unnamed(constructor: Constructor) -> None: data = {"a": [1, 1, 2], "b": [4, 5, 6], "c": [7, 2, 1]} df = nw.from_native(constructor(data)).lazy() result = ( @@ -202,7 +203,7 @@ def test_group_by_simple_unnamed(constructor: Any) -> None: compare_dicts(result, expected) -def test_group_by_multiple_keys(constructor: Any) -> None: +def test_group_by_multiple_keys(constructor: Constructor) -> None: data = {"a": [1, 1, 2], "b": [4, 4, 6], "c": [7, 2, 1]} df = nw.from_native(constructor(data)).lazy() result = ( @@ -223,7 +224,7 @@ def test_group_by_multiple_keys(constructor: Any) -> None: compare_dicts(result, expected) -def test_key_with_nulls(constructor: Any, request: pytest.FixtureRequest) -> None: +def test_key_with_nulls(constructor: Constructor, request: pytest.FixtureRequest) -> None: if "modin" in str(constructor): # TODO(unassigned): Modin flaky here? request.applymarker(pytest.mark.skip) @@ -248,7 +249,7 @@ def test_key_with_nulls(constructor: Any, request: pytest.FixtureRequest) -> Non compare_dicts(result, expected) -def test_no_agg(constructor: Any) -> None: +def test_no_agg(constructor: Constructor) -> None: result = nw.from_native(constructor(data)).group_by(["a", "b"]).agg().sort("a", "b") expected = {"a": [1, 3], "b": [4, 6]} diff --git a/tests/utils.py b/tests/utils.py index 6ab703c3b..b13bec192 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -4,11 +4,21 @@ import sys import warnings from typing import Any +from typing import Callable from typing import Iterator from typing import Sequence import pandas as pd +from narwhals.typing import IntoFrame + +if sys.version_info >= (3, 10): + from typing import TypeAlias # pragma: no cover +else: + from typing_extensions import TypeAlias # pragma: no cover + +Constructor: TypeAlias = Callable[[Any], IntoFrame] + def zip_strict(left: Sequence[Any], right: Sequence[Any]) -> Iterator[Any]: if len(left) != len(right):