Skip to content

Commit

Permalink
chore: make module versions as constants for use in tests (#1235)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessandroMiola authored Oct 21, 2024
1 parent e91bb7f commit 220b9fa
Show file tree
Hide file tree
Showing 42 changed files with 283 additions and 300 deletions.
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from narwhals.dependencies import get_cudf
from narwhals.dependencies import get_dask_dataframe
from narwhals.dependencies import get_modin
from narwhals.utils import parse_version
from tests.utils import PANDAS_VERSION

if TYPE_CHECKING:
from narwhals.typing import IntoDataFrame
Expand Down Expand Up @@ -92,7 +92,7 @@ def pyarrow_table_constructor(obj: Any) -> IntoDataFrame:
return pa.table(obj) # type: ignore[no-any-return]


if parse_version(pd.__version__) >= parse_version("2.0.0"):
if PANDAS_VERSION >= (2, 0, 0):
eager_constructors = [
pandas_constructor,
pandas_nullable_constructor,
Expand Down
9 changes: 5 additions & 4 deletions tests/dtypes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import pytest

import narwhals.stable.v1 as nw
from narwhals.utils import parse_version
from tests.utils import PANDAS_VERSION
from tests.utils import POLARS_VERSION


@pytest.mark.parametrize("time_unit", ["us", "ns", "ms"])
Expand Down Expand Up @@ -125,7 +126,7 @@ def test_struct_hashes() -> None:


@pytest.mark.skipif(
parse_version(pl.__version__) < (1,) or parse_version(pd.__version__) < (2, 2),
POLARS_VERSION < (1,) or PANDAS_VERSION < (2, 2),
reason="`shape` is only available after 1.0",
)
def test_polars_2d_array() -> None:
Expand All @@ -144,7 +145,7 @@ def test_polars_2d_array() -> None:
def test_second_time_unit() -> None:
s = pd.Series(np.array([np.datetime64("2020-01-01", "s")]))
result = nw.from_native(s, series_only=True)
if parse_version(pd.__version__) < (2,): # pragma: no cover
if PANDAS_VERSION < (2,): # pragma: no cover
assert result.dtype == nw.Datetime("ns")
else:
assert result.dtype == nw.Datetime("s")
Expand All @@ -153,7 +154,7 @@ def test_second_time_unit() -> None:
assert result.dtype == nw.Datetime("s")
s = pd.Series(np.array([np.timedelta64(1, "s")]))
result = nw.from_native(s, series_only=True)
if parse_version(pd.__version__) < (2,): # pragma: no cover
if PANDAS_VERSION < (2,): # pragma: no cover
assert result.dtype == nw.Duration("ns")
else:
assert result.dtype == nw.Duration("s")
Expand Down
10 changes: 6 additions & 4 deletions tests/expr_and_series/all_horizontal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

from typing import Any

import polars as pl
import pytest

import narwhals.stable.v1 as nw
from narwhals.utils import parse_version
from tests.utils import POLARS_VERSION
from tests.utils import Constructor
from tests.utils import ConstructorEager
from tests.utils import assert_equal_data
Expand Down Expand Up @@ -52,8 +51,11 @@ def test_allh_all(constructor: Constructor) -> None:
assert_equal_data(result, expected)


def test_allh_nth(constructor: Constructor, request: pytest.FixtureRequest) -> None:
if "polars" in str(constructor) and parse_version(pl.__version__) < (1, 0):
def test_allh_nth(
constructor: Constructor,
request: pytest.FixtureRequest,
) -> None:
if "polars" in str(constructor) and POLARS_VERSION < (1, 0):
request.applymarker(pytest.mark.xfail)
data = {
"a": [False, False, True],
Expand Down
12 changes: 4 additions & 8 deletions tests/expr_and_series/arithmetic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from hypothesis import given

import narwhals.stable.v1 as nw
from narwhals.utils import parse_version
from tests.utils import PANDAS_VERSION
from tests.utils import Constructor
from tests.utils import ConstructorEager
from tests.utils import assert_equal_data
Expand Down Expand Up @@ -158,9 +158,7 @@ def test_truediv_same_dims(
left=st.integers(-100, 100),
right=st.integers(-100, 100),
)
@pytest.mark.skipif(
parse_version(pd.__version__) < (2, 0), reason="convert_dtypes not available"
)
@pytest.mark.skipif(PANDAS_VERSION < (2, 0), reason="convert_dtypes not available")
def test_floordiv(left: int, right: int) -> None:
# hypothesis complains if we add `constructor` as an argument, so this
# test is a bit manual unfortunately
Expand All @@ -170,7 +168,7 @@ def test_floordiv(left: int, right: int) -> None:
nw.col("a") // right
)
assert_equal_data(result, expected)
if parse_version(pd.__version__) < (2, 2): # pragma: no cover
if PANDAS_VERSION < (2, 2): # pragma: no cover
# Bug in old version of pandas
pass
else:
Expand Down Expand Up @@ -198,9 +196,7 @@ def test_floordiv(left: int, right: int) -> None:
left=st.integers(-100, 100),
right=st.integers(-100, 100),
)
@pytest.mark.skipif(
parse_version(pd.__version__) < (2, 0), reason="convert_dtypes not available"
)
@pytest.mark.skipif(PANDAS_VERSION < (2, 0), reason="convert_dtypes not available")
def test_mod(left: int, right: int) -> None:
# hypothesis complains if we add `constructor` as an argument, so this
# test is a bit manual unfortunately
Expand Down
40 changes: 22 additions & 18 deletions tests/expr_and_series/cast_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from datetime import timezone

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 PANDAS_VERSION
from tests.utils import PYARROW_VERSION
from tests.utils import Constructor
from tests.utils import assert_equal_data
from tests.utils import is_windows
Expand Down Expand Up @@ -53,10 +53,13 @@


@pytest.mark.filterwarnings("ignore:casting period[M] values to int64:FutureWarning")
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
def test_cast(
constructor: Constructor,
request: pytest.FixtureRequest,
) -> None:
if "pyarrow_table_constructor" in str(constructor) and PYARROW_VERSION <= (
15,
): # pragma: no cover
request.applymarker(pytest.mark.xfail)
if "modin" in str(constructor):
# TODO(unassigned): in modin, we end up with `'<U0'` dtype
Expand Down Expand Up @@ -103,10 +106,13 @@ def test_cast(constructor: Constructor, request: pytest.FixtureRequest) -> None:
assert dict(result.collect_schema()) == expected


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
def test_cast_series(
constructor: Constructor,
request: pytest.FixtureRequest,
) -> None:
if "pyarrow_table_constructor" in str(constructor) and PYARROW_VERSION <= (
15,
): # pragma: no cover
request.applymarker(pytest.mark.xfail)
if "modin" in str(constructor):
# TODO(unassigned): in modin, we end up with `'<U0'` dtype
Expand Down Expand Up @@ -157,10 +163,7 @@ def test_cast_series(constructor: Constructor, request: pytest.FixtureRequest) -
assert result.schema == expected


@pytest.mark.skipif(
parse_version(pd.__version__) < parse_version("1.0.0"),
reason="too old for convert_dtypes",
)
@pytest.mark.skipif(PANDAS_VERSION < (1, 0, 0), reason="too old for convert_dtypes")
def test_cast_string() -> None:
s_pd = pd.Series([1, 2]).convert_dtypes()
s = nw.from_native(s_pd, series_only=True)
Expand All @@ -170,11 +173,12 @@ def test_cast_string() -> None:


def test_cast_raises_for_unknown_dtype(
constructor: Constructor, request: pytest.FixtureRequest
constructor: Constructor,
request: pytest.FixtureRequest,
) -> None:
if "pyarrow_table_constructor" in str(constructor) and parse_version(
pa.__version__
) <= (15,): # pragma: no cover
if "pyarrow_table_constructor" in str(constructor) and PYARROW_VERSION <= (
15,
): # pragma: no cover
request.applymarker(pytest.mark.xfail)
if "polars" in str(constructor):
request.applymarker(pytest.mark.xfail)
Expand Down
9 changes: 4 additions & 5 deletions tests/expr_and_series/cat/get_categories_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
import pytest

import narwhals.stable.v1 as nw
from narwhals.utils import parse_version
from tests.utils import PYARROW_VERSION
from tests.utils import ConstructorEager
from tests.utils import assert_equal_data

data = {"a": ["one", "two", "two"]}


def test_get_categories(
request: pytest.FixtureRequest, constructor_eager: ConstructorEager
request: pytest.FixtureRequest,
constructor_eager: ConstructorEager,
) -> None:
if "pyarrow_table" in str(constructor_eager) and parse_version(
pa.__version__
) < parse_version("15.0.0"):
if "pyarrow_table" in str(constructor_eager) and PYARROW_VERSION < (15, 0, 0):
request.applymarker(pytest.mark.xfail)

df = nw.from_native(constructor_eager(data), eager_only=True)
Expand Down
32 changes: 12 additions & 20 deletions tests/expr_and_series/convert_time_zone_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
from datetime import timezone
from typing import TYPE_CHECKING

import pandas as pd
import polars as pl
import pyarrow as pa
import pytest

import narwhals.stable.v1 as nw
from narwhals.utils import parse_version
from tests.utils import PANDAS_VERSION
from tests.utils import POLARS_VERSION
from tests.utils import PYARROW_VERSION
from tests.utils import Constructor
from tests.utils import assert_equal_data
from tests.utils import is_windows
Expand All @@ -20,14 +19,12 @@


def test_convert_time_zone(
constructor: Constructor, request: pytest.FixtureRequest
constructor: Constructor,
request: pytest.FixtureRequest,
) -> None:
if (
(any(x in str(constructor) for x in ("pyarrow", "modin")) and is_windows())
or (
"pandas_pyarrow" in str(constructor)
and parse_version(pd.__version__) < (2, 1)
)
or ("pandas_pyarrow" in str(constructor) and PANDAS_VERSION < (2, 1))
or ("cudf" in str(constructor))
):
request.applymarker(pytest.mark.xfail)
Expand All @@ -48,14 +45,12 @@ def test_convert_time_zone(


def test_convert_time_zone_series(
constructor_eager: ConstructorEager, request: pytest.FixtureRequest
constructor_eager: ConstructorEager,
request: pytest.FixtureRequest,
) -> None:
if (
(any(x in str(constructor_eager) for x in ("pyarrow", "modin")) and is_windows())
or (
"pandas_pyarrow" in str(constructor_eager)
and parse_version(pd.__version__) < (2, 1)
)
or ("pandas_pyarrow" in str(constructor_eager) and PANDAS_VERSION < (2, 1))
or ("cudf" in str(constructor_eager))
):
request.applymarker(pytest.mark.xfail)
Expand All @@ -80,15 +75,12 @@ def test_convert_time_zone_from_none(
) -> None:
if (
(any(x in str(constructor) for x in ("pyarrow", "modin")) and is_windows())
or (
"pandas_pyarrow" in str(constructor)
and parse_version(pd.__version__) < (2, 1)
)
or ("pyarrow_table" in str(constructor) and parse_version(pa.__version__) < (12,))
or ("pandas_pyarrow" in str(constructor) and PANDAS_VERSION < (2, 1))
or ("pyarrow_table" in str(constructor) and PYARROW_VERSION < (12,))
or ("cudf" in str(constructor))
):
request.applymarker(pytest.mark.xfail)
if "polars" in str(constructor) and parse_version(pl.__version__) < (0, 20, 7):
if "polars" in str(constructor) and POLARS_VERSION < (0, 20, 7):
# polars used to disallow this
request.applymarker(pytest.mark.xfail)
data = {
Expand Down
19 changes: 9 additions & 10 deletions tests/expr_and_series/diff_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from __future__ import annotations

import pyarrow as pa
import pytest

import narwhals.stable.v1 as nw
from narwhals.utils import parse_version
from tests.utils import PYARROW_VERSION
from tests.utils import Constructor
from tests.utils import ConstructorEager
from tests.utils import assert_equal_data
Expand All @@ -16,10 +15,11 @@
}


def test_diff(constructor: Constructor, request: pytest.FixtureRequest) -> None:
if "pyarrow_table_constructor" in str(constructor) and parse_version(
pa.__version__
) < (13,):
def test_diff(
constructor: Constructor,
request: pytest.FixtureRequest,
) -> None:
if "pyarrow_table_constructor" in str(constructor) and PYARROW_VERSION < (13,):
# pc.pairwisediff is available since pyarrow 13.0.0
request.applymarker(pytest.mark.xfail)
df = nw.from_native(constructor(data))
Expand All @@ -34,11 +34,10 @@ def test_diff(constructor: Constructor, request: pytest.FixtureRequest) -> None:


def test_diff_series(
constructor_eager: ConstructorEager, request: pytest.FixtureRequest
constructor_eager: ConstructorEager,
request: pytest.FixtureRequest,
) -> None:
if "pyarrow_table_constructor" in str(constructor_eager) and parse_version(
pa.__version__
) < (13,):
if "pyarrow_table_constructor" in str(constructor_eager) and PYARROW_VERSION < (13,):
# pc.pairwisediff is available since pyarrow 13.0.0
request.applymarker(pytest.mark.xfail)
df = nw.from_native(constructor_eager(data), eager_only=True)
Expand Down
9 changes: 3 additions & 6 deletions tests/expr_and_series/dt/datetime_duration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
from datetime import timedelta

import numpy as np
import pandas as pd
import pyarrow as pa
import pyarrow.compute as pc
import pytest

import narwhals.stable.v1 as nw
from narwhals.utils import parse_version
from tests.utils import PANDAS_VERSION
from tests.utils import Constructor
from tests.utils import ConstructorEager
from tests.utils import assert_equal_data
Expand Down Expand Up @@ -45,7 +44,7 @@ def test_duration_attributes(
expected_b: list[int],
expected_c: list[int],
) -> None:
if parse_version(pd.__version__) < (2, 2) and "pandas_pyarrow" in str(constructor):
if PANDAS_VERSION < (2, 2) and "pandas_pyarrow" in str(constructor):
request.applymarker(pytest.mark.xfail)
if "cudf" in str(constructor):
request.applymarker(pytest.mark.xfail)
Expand Down Expand Up @@ -80,9 +79,7 @@ def test_duration_attributes_series(
expected_b: list[int],
expected_c: list[int],
) -> None:
if parse_version(pd.__version__) < (2, 2) and "pandas_pyarrow" in str(
constructor_eager
):
if PANDAS_VERSION < (2, 2) and "pandas_pyarrow" in str(constructor_eager):
request.applymarker(pytest.mark.xfail)
if "cudf" in str(constructor_eager):
request.applymarker(pytest.mark.xfail)
Expand Down
4 changes: 2 additions & 2 deletions tests/expr_and_series/dt/ordinal_day_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from hypothesis import given

import narwhals.stable.v1 as nw
from narwhals.utils import parse_version
from tests.utils import PANDAS_VERSION


@given(dates=st.datetimes(min_value=datetime(1960, 1, 1), max_value=datetime(1980, 1, 1))) # type: ignore[misc]
@pytest.mark.skipif(
parse_version(pd.__version__) < parse_version("2.0.0"),
PANDAS_VERSION < (2, 0, 0),
reason="pyarrow dtype not available",
)
@pytest.mark.slow
Expand Down
Loading

0 comments on commit 220b9fa

Please sign in to comment.