Skip to content

Commit

Permalink
fix(ir): raise if Concrete.copy() receives unexpected arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
kszucs committed Dec 14, 2023
1 parent 2e7fb76 commit 442199a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
2 changes: 2 additions & 0 deletions ibis/common/grounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,7 @@ def argnames(self) -> tuple[str, ...]:

def copy(self, **overrides) -> Self:
kwargs = dict(zip(self.__argnames__, self.__args__))
if unknown_args := overrides.keys() - kwargs.keys():
raise AttributeError(f"Unexpected arguments: {unknown_args}")
kwargs.update(overrides)
return self.__recreate__(kwargs)
34 changes: 26 additions & 8 deletions ibis/common/tests/test_grounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,21 +616,39 @@ class Test(Annotable):
assert Test(1, 2, a=3, b=4, c=5).options == {"a": 3, "b": 4, "c": 5}


def test_concrete_copy_with_variadic_argument():
class Test(Annotable):
def test_copy_with_variadic_argument():
class Foo(Annotable):
a = is_int
b = is_int
args = varargs(is_int)

class Bar(Concrete):
a = is_int
b = is_int
args = varargs(is_int)

t = Test(1, 2, 3, 4, 5)
for t in [Foo(1, 2, 3, 4, 5), Bar(1, 2, 3, 4, 5)]:
assert t.a == 1
assert t.b == 2
assert t.args == (3, 4, 5)

u = t.copy(a=6, args=(8, 9, 10))
assert u.a == 6
assert u.b == 2
assert u.args == (8, 9, 10)


def test_concrete_copy_with_unknown_argument_raise():
class Bar(Concrete):
a = is_int
b = is_int

t = Bar(1, 2)
assert t.a == 1
assert t.b == 2
assert t.args == (3, 4, 5)

u = t.copy(a=6, args=(8, 9, 10))
assert u.a == 6
assert u.b == 2
assert u.args == (8, 9, 10)
with pytest.raises(AttributeError, match="Unexpected arguments"):
t.copy(c=3, d=4)


def test_concrete_pickling_variadic_arguments():
Expand Down

0 comments on commit 442199a

Please sign in to comment.