Skip to content

Commit

Permalink
add has_operation test
Browse files Browse the repository at this point in the history
  • Loading branch information
camriddell committed Jan 13, 2025
1 parent 333ccb6 commit c9ae6c1
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import re
import string
from contextlib import contextmanager
from typing import TYPE_CHECKING
from typing import Any
from typing import Callable
from typing import Generator

import hypothesis.strategies as st
import pandas as pd
Expand All @@ -15,7 +19,9 @@

import narwhals.stable.v1 as nw
from narwhals.exceptions import ColumnNotFoundError
from narwhals.stable.v1 import has_operation
from narwhals.utils import check_column_exists
from narwhals.utils import get_class_that_defines_method
from narwhals.utils import parse_version
from tests.utils import PANDAS_VERSION
from tests.utils import get_module_version_as_tuple
Expand Down Expand Up @@ -297,3 +303,66 @@ def test_check_column_exists() -> None:
match=re.escape("Column(s) ['d', 'f'] not found in ['a', 'b', 'c']"),
):
check_column_exists(columns, subset)


def test_has_operation() -> None:
# smoke tests
assert has_operation(pd, nw.Expr.mean)
assert has_operation(pd, nw.Expr.str.to_uppercase)
assert has_operation(pd, nw.Expr.dt.date)
assert has_operation(pd, nw.Series.mean)
assert has_operation(pd, nw.Series.str.to_uppercase)
assert has_operation(pd, nw.Series.dt.date)
assert has_operation(pd, nw.DataFrame.select)
assert not has_operation(pd, nw.LazyFrame.select) # pandas does not have LazyFrame

from narwhals._pandas_like.dataframe import PandasLikeDataFrame
from narwhals._pandas_like.expr import PandasLikeExpr
from narwhals._pandas_like.expr_dt import PandasLikeExprDateTimeNamespace
from narwhals._pandas_like.expr_str import PandasLikeExprStringNamespace
from narwhals._pandas_like.series import PandasLikeSeries
from narwhals._pandas_like.series_dt import PandasLikeSeriesDateTimeNamespace
from narwhals._pandas_like.series_str import PandasLikeSeriesStringNamespace

@contextmanager
def out_attribute(unbound_method: Callable[..., Any]) -> Generator[None, None, None]:
cls = get_class_that_defines_method(unbound_method)
delattr(cls, unbound_method.__name__)
try:
yield
finally:
setattr(cls, unbound_method.__name__, unbound_method)

# re-run smoke tests after explicitly removing methods
with out_attribute(PandasLikeExpr.mean):
assert not has_operation(pd, nw.Expr.mean)
with out_attribute(PandasLikeExprStringNamespace.to_uppercase):
assert not has_operation(pd, nw.Expr.str.to_uppercase)
with out_attribute(PandasLikeExprDateTimeNamespace.date):
assert not has_operation(pd, nw.Expr.dt.date)

with out_attribute(PandasLikeSeries.mean):
assert not has_operation(pd, nw.Series.mean)
with out_attribute(PandasLikeSeriesStringNamespace.to_uppercase):
assert not has_operation(pd, nw.Series.str.to_uppercase)
with out_attribute(PandasLikeSeriesDateTimeNamespace.date):
assert not has_operation(pd, nw.Series.dt.date)

with out_attribute(PandasLikeDataFrame.select):
assert not has_operation(pd, nw.DataFrame.select)


def test_has_operation_raises() -> None:
import math

with pytest.raises(ValueError, match="Unknown namespace"):
has_operation(math, nw.Expr.mean)


def test_has_operation_raises_friendly_message() -> None:
pytest.importorskip("dask")
import dask
import dask.dataframe

with pytest.raises(ValueError, match="Unknown namespace .* did you mean"):
has_operation(dask, nw.Expr.mean)

0 comments on commit c9ae6c1

Please sign in to comment.