diff --git a/narwhals/_spark_like/expr.py b/narwhals/_spark_like/expr.py index 746cfa6b0e..e2ddd28ebf 100644 --- a/narwhals/_spark_like/expr.py +++ b/narwhals/_spark_like/expr.py @@ -10,7 +10,6 @@ from narwhals._spark_like.utils import maybe_evaluate from narwhals.typing import CompliantExpr from narwhals.utils import Implementation -from narwhals.utils import get_module_version_as_tuple from narwhals.utils import parse_version if TYPE_CHECKING: @@ -21,8 +20,6 @@ from narwhals._spark_like.namespace import SparkLikeNamespace from narwhals.utils import Version -PYSPARK_VERSION: tuple[int, ...] = get_module_version_as_tuple("pyspark") - class SparkLikeExpr(CompliantExpr["Column"]): _implementation = Implementation.PYSPARK @@ -226,9 +223,10 @@ def mean(self) -> Self: def median(self) -> Self: def _median(_input: Column) -> Column: + import pyspark # ignore-banned-import from pyspark.sql import functions as F # noqa: N812 - if PYSPARK_VERSION < (3, 4): + if parse_version(pyspark.__version__) < (3, 4): # Use percentile_approx with default accuracy parameter (10000) return F.percentile_approx(_input.cast("double"), 0.5) diff --git a/narwhals/utils.py b/narwhals/utils.py index 8d4899f2a5..be9f072331 100644 --- a/narwhals/utils.py +++ b/narwhals/utils.py @@ -1058,10 +1058,3 @@ def generate_repr(header: str, native_repr: str) -> str: "| Use `.to_native` to see native output |\nā””" f"{'ā”€' * 39}ā”˜" ) - - -def get_module_version_as_tuple(module_name: str) -> tuple[int, ...]: - try: - return parse_version(__import__(module_name).__version__) - except ImportError: - return (0, 0, 0) diff --git a/tests/utils.py b/tests/utils.py index 25707ca867..34f1bfa1ef 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -14,7 +14,7 @@ from narwhals.typing import IntoDataFrame from narwhals.typing import IntoFrame from narwhals.utils import Implementation -from narwhals.utils import get_module_version_as_tuple +from narwhals.utils import parse_version if sys.version_info >= (3, 10): from typing import TypeAlias # pragma: no cover @@ -22,6 +22,13 @@ from typing_extensions import TypeAlias # pragma: no cover +def get_module_version_as_tuple(module_name: str) -> tuple[int, ...]: + try: + return parse_version(__import__(module_name).__version__) + except ImportError: + return (0, 0, 0) + + IBIS_VERSION: tuple[int, ...] = get_module_version_as_tuple("ibis") NUMPY_VERSION: tuple[int, ...] = get_module_version_as_tuple("numpy") PANDAS_VERSION: tuple[int, ...] = get_module_version_as_tuple("pandas") diff --git a/tests/utils_test.py b/tests/utils_test.py index a026aa4954..26bd2ecf92 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -13,8 +13,8 @@ from pandas.testing import assert_series_equal import narwhals.stable.v1 as nw -from narwhals.utils import get_module_version_as_tuple from tests.utils import PANDAS_VERSION +from tests.utils import get_module_version_as_tuple if TYPE_CHECKING: from narwhals.series import Series