Skip to content

Commit a8d9c0a

Browse files
committed
avoid chaining if func/names_glue already exists
1 parent a011a54 commit a8d9c0a

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

janitor/functions/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,8 @@ def __init__(self, *cols):
671671
self.names_glue = None
672672

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

681683
def rename(self, name):
684+
if self.names_glue:
685+
raise ValueError("A name has already been assigned")
682686
check("name", name, [str])
683687
self.names_glue = name
684688
return self

tests/functions/test_summarize.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ def test_SD_name_error(dataframe):
2929
dataframe.summarize(SD("a").add_fns("sum").rename(1))
3030

3131

32+
@pytest.mark.functions
33+
def test_SD_name_dupe_error(dataframe):
34+
"""Raise if names_glue already exists"""
35+
with pytest.raises(
36+
ValueError,
37+
match=r"A name has already been assigned",
38+
):
39+
dataframe.summarize(SD("a").add_fns("sum").rename("1").rename("name"))
40+
41+
42+
@pytest.mark.functions
43+
def test_SD_func_dupe_error(dataframe):
44+
"""Raise if func already exists"""
45+
with pytest.raises(
46+
ValueError,
47+
match=r"A function has already been assigned",
48+
):
49+
dataframe.summarize(
50+
SD("a").add_fns("sum").add_fns(np.sum).rename("name")
51+
)
52+
53+
3254
@pytest.mark.functions
3355
def test_SD_no_func_error(dataframe):
3456
"""Raise if func is not provided"""

0 commit comments

Comments
 (0)