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(python): support variadic arguments #513

Merged
merged 6 commits into from
Jun 3, 2019
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
3 changes: 2 additions & 1 deletion packages/jsii-pacmak/lib/targets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,10 @@ abstract class BaseMethod implements PythonBase {
jsiiMethodParams.push(`"${this.jsName}"`);
}

// If the last arg is variadic, expand the tuple
const paramNames: string[] = [];
for (const param of this.parameters) {
paramNames.push(toPythonParameterName(param.name));
paramNames.push((param.variadic ? '*' : '') + toPythonParameterName(param.name));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make more sense to simply check if param.variadic is true here and push a different value to paramNames?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I started with a change proposed in the original issue. Meant to clean it up but forgot. Testing my updates now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant is that you can just add this condition when paramNames is being created in the first place (line 471):

for (const param of this.parameters) {
  paramNames.push((param.variadic ? '*' : '') + toPythonParameterName(param.name));
}


code.line(`${methodPrefix}jsii.${this.jsiiMethod}(${jsiiMethodParams.join(", ")}, [${paramNames.join(", ")}])`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ def optional_and_variadic(self, optional: typing.Optional[str]=None, *things: st
optional: -
things: -
"""
return jsii.invoke(self, "optionalAndVariadic", [optional, things])
return jsii.invoke(self, "optionalAndVariadic", [optional, *things])


class EraseUndefinedHashValues(metaclass=jsii.JSIIMeta, jsii_type="jsii-calc.EraseUndefinedHashValues"):
Expand Down Expand Up @@ -2994,7 +2994,7 @@ def __init__(self, *prefix: jsii.Number) -> None:
Arguments:
prefix: a prefix that will be use for all values returned by ``#asArray``.
"""
jsii.create(VariadicMethod, self, [prefix])
jsii.create(VariadicMethod, self, [*prefix])

@jsii.member(jsii_name="asArray")
def as_array(self, first: jsii.Number, *others: jsii.Number) -> typing.List[jsii.Number]:
Expand All @@ -3003,7 +3003,7 @@ def as_array(self, first: jsii.Number, *others: jsii.Number) -> typing.List[jsii
first: the first element of the array to be returned (after the ``prefix`` provided at construction time).
others: other elements to be included in the array.
"""
return jsii.invoke(self, "asArray", [first, others])
return jsii.invoke(self, "asArray", [first, *others])


class VirtualMethodPlayground(metaclass=jsii.JSIIMeta, jsii_type="jsii-calc.VirtualMethodPlayground"):
Expand Down
6 changes: 6 additions & 0 deletions packages/jsii-python-runtime/tests/test_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
UsesInterfaceWithProperties,
composition,
EraseUndefinedHashValues,
VariadicMethod,
)
from scope.jsii_calc_lib import IFriendly, EnumFromScopedModule, Number

Expand Down Expand Up @@ -895,3 +896,8 @@ def consume_partially_initialized_this(self, obj, dt, en):
reflector = PartiallyInitializedThisConsumerImpl()
obj = ConstructorPassesThisOut(reflector)
assert obj is not None


def test_variadicMethodCanBeInvoked():
variadic = VariadicMethod(1)
assert variadic.as_array(3, 4, 5, 6) == [1, 3, 4, 5, 6]