Skip to content

Commit

Permalink
avoid chaining if func/names_glue already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
samukweku committed Jan 28, 2023
1 parent a011a54 commit a8d9c0a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions janitor/functions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,8 @@ def __init__(self, *cols):
self.names_glue = None

def add_fns(self, func, **kwargs):
if self.func:
raise ValueError("A function has already been assigned")
check("Function", func, [str, list, tuple, callable])
if isinstance(func, (list, tuple)):
for funcn in func:
Expand All @@ -679,6 +681,8 @@ def add_fns(self, func, **kwargs):
return self

def rename(self, name):
if self.names_glue:
raise ValueError("A name has already been assigned")
check("name", name, [str])
self.names_glue = name
return self
22 changes: 22 additions & 0 deletions tests/functions/test_summarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ def test_SD_name_error(dataframe):
dataframe.summarize(SD("a").add_fns("sum").rename(1))


@pytest.mark.functions
def test_SD_name_dupe_error(dataframe):
"""Raise if names_glue already exists"""
with pytest.raises(
ValueError,
match=r"A name has already been assigned",
):
dataframe.summarize(SD("a").add_fns("sum").rename("1").rename("name"))


@pytest.mark.functions
def test_SD_func_dupe_error(dataframe):
"""Raise if func already exists"""
with pytest.raises(
ValueError,
match=r"A function has already been assigned",
):
dataframe.summarize(
SD("a").add_fns("sum").add_fns(np.sum).rename("name")
)


@pytest.mark.functions
def test_SD_no_func_error(dataframe):
"""Raise if func is not provided"""
Expand Down

0 comments on commit a8d9c0a

Please sign in to comment.