Skip to content

Commit

Permalink
fix(python): support variadic arguments (#513)
Browse files Browse the repository at this point in the history
* Properly handle variadic args

* update compliance test to call VariadicMethod.

* Remove commented line.

* Cleaning up code

* Update the expected output

* Further simplification of code
  • Loading branch information
garnaat committed Jun 3, 2019
1 parent 10e2bfe commit 695ca6b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
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));
}

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]

0 comments on commit 695ca6b

Please sign in to comment.