Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(annotable): allow optional arguments at any position #3732

Merged
merged 1 commit into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions ibis/common/grounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,23 @@ def __new__(metacls, clsname, bases, dct):

# mandatory fields without default values must preceed the optional
# ones in the function signature, the partial ordering will be kept
new_params, inherited_params, optional_inherited_params = [], [], []
new_args, new_kwargs = [], []
inherited_args, inherited_kwargs = [], []

for name, param in params.items():
if name in inherited:
if param.default is EMPTY:
inherited_params.append(param)
inherited_args.append(param)
else:
optional_inherited_params.append(param)
inherited_kwargs.append(param)
else:
new_params.append(param)
if param.default is EMPTY:
new_args.append(param)
else:
new_kwargs.append(param)

signature = inspect.Signature(
inherited_params + new_params + optional_inherited_params
inherited_args + new_args + new_kwargs + inherited_kwargs
cpcloud marked this conversation as resolved.
Show resolved Hide resolved
)

attribs["__slots__"] = tuple(slots)
Expand Down
22 changes: 22 additions & 0 deletions ibis/common/tests/test_grounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,28 @@ class NoHooves(Farm):
assert g1.goats == 0


def test_keyword_argument_reordering():
class Alpha(Annotable):
a = IsInt
b = IsInt

class Beta(Alpha):
c = IsInt
d = Optional(IsInt, default=0)
e = IsInt
cpcloud marked this conversation as resolved.
Show resolved Hide resolved

obj = Beta(1, 2, 3, 4)
assert obj.a == 1
assert obj.b == 2
assert obj.c == 3
assert obj.e == 4
assert obj.d == 0

obj = Beta(1, 2, 3, 4, 5)
assert obj.d == 5
assert obj.e == 4


def test_not_copy_default():
default = tuple()

Expand Down