Skip to content

Commit

Permalink
fix(annotable): allow optional arguments at any position
Browse files Browse the repository at this point in the history
  • Loading branch information
kszucs committed Apr 7, 2022
1 parent 18a06d3 commit 448355e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
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
)

attribs["__slots__"] = tuple(slots)
Expand Down
18 changes: 18 additions & 0 deletions ibis/common/tests/test_grounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,24 @@ 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

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


def test_not_copy_default():
default = tuple()

Expand Down

0 comments on commit 448355e

Please sign in to comment.