Skip to content

Commit 695ca6b

Browse files
authored
fix(python): support variadic arguments (#513)
* Properly handle variadic args * update compliance test to call VariadicMethod. * Remove commented line. * Cleaning up code * Update the expected output * Further simplification of code
1 parent 10e2bfe commit 695ca6b

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

packages/jsii-pacmak/lib/targets/python.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,10 @@ abstract class BaseMethod implements PythonBase {
467467
jsiiMethodParams.push(`"${this.jsName}"`);
468468
}
469469

470+
// If the last arg is variadic, expand the tuple
470471
const paramNames: string[] = [];
471472
for (const param of this.parameters) {
472-
paramNames.push(toPythonParameterName(param.name));
473+
paramNames.push((param.variadic ? '*' : '') + toPythonParameterName(param.name));
473474
}
474475

475476
code.line(`${methodPrefix}jsii.${this.jsiiMethod}(${jsiiMethodParams.join(", ")}, [${paramNames.join(", ")}])`);

packages/jsii-pacmak/test/expected.jsii-calc/python/src/jsii_calc/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def optional_and_variadic(self, optional: typing.Optional[str]=None, *things: st
678678
optional: -
679679
things: -
680680
"""
681-
return jsii.invoke(self, "optionalAndVariadic", [optional, things])
681+
return jsii.invoke(self, "optionalAndVariadic", [optional, *things])
682682

683683

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

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

30083008

30093009
class VirtualMethodPlayground(metaclass=jsii.JSIIMeta, jsii_type="jsii-calc.VirtualMethodPlayground"):

packages/jsii-python-runtime/tests/test_compliance.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
UsesInterfaceWithProperties,
4343
composition,
4444
EraseUndefinedHashValues,
45+
VariadicMethod,
4546
)
4647
from scope.jsii_calc_lib import IFriendly, EnumFromScopedModule, Number
4748

@@ -895,3 +896,8 @@ def consume_partially_initialized_this(self, obj, dt, en):
895896
reflector = PartiallyInitializedThisConsumerImpl()
896897
obj = ConstructorPassesThisOut(reflector)
897898
assert obj is not None
899+
900+
901+
def test_variadicMethodCanBeInvoked():
902+
variadic = VariadicMethod(1)
903+
assert variadic.as_array(3, 4, 5, 6) == [1, 3, 4, 5, 6]

0 commit comments

Comments
 (0)