From 1245950c768b7402e143a3f1bb9f0f430389dec6 Mon Sep 17 00:00:00 2001 From: Mitch Garnaat Date: Mon, 3 Jun 2019 03:15:10 -0700 Subject: [PATCH 1/6] Properly handle variadic args --- packages/jsii-pacmak/lib/targets/python.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/jsii-pacmak/lib/targets/python.ts b/packages/jsii-pacmak/lib/targets/python.ts index ac9023a0ad..7e56666355 100644 --- a/packages/jsii-pacmak/lib/targets/python.ts +++ b/packages/jsii-pacmak/lib/targets/python.ts @@ -472,7 +472,15 @@ abstract class BaseMethod implements PythonBase { paramNames.push(toPythonParameterName(param.name)); } - code.line(`${methodPrefix}jsii.${this.jsiiMethod}(${jsiiMethodParams.join(", ")}, [${paramNames.join(", ")}])`); + // If the args are variadic, expand the last one + const modifiedParamNames: string[] = paramNames.slice(); + if (this.parameters.length >= 1 && this.parameters[this.parameters.length - 1].variadic) { + modifiedParamNames.push("*" + modifiedParamNames.pop()); + } + + code.line(`${methodPrefix}jsii.${this.jsiiMethod}(${jsiiMethodParams.join(", ")}, [${modifiedParamNames.join(", ")}])`); + + // code.line(`${methodPrefix}jsii.${this.jsiiMethod}(${jsiiMethodParams.join(", ")}, [${paramNames.join(", ")}])`); } private getLiftedProperties(resolver: TypeResolver): spec.Property[] { From e748ac3125397c27bd368dea7c991463435fc7e9 Mon Sep 17 00:00:00 2001 From: Mitch Garnaat Date: Mon, 3 Jun 2019 06:22:33 -0700 Subject: [PATCH 2/6] update compliance test to call VariadicMethod. --- packages/jsii-python-runtime/tests/test_compliance.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/jsii-python-runtime/tests/test_compliance.py b/packages/jsii-python-runtime/tests/test_compliance.py index c55291df0f..4ba2ffe6d5 100644 --- a/packages/jsii-python-runtime/tests/test_compliance.py +++ b/packages/jsii-python-runtime/tests/test_compliance.py @@ -42,6 +42,7 @@ UsesInterfaceWithProperties, composition, EraseUndefinedHashValues, + VariadicMethod, ) from scope.jsii_calc_lib import IFriendly, EnumFromScopedModule, Number @@ -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] From f4fad1dff3db45b6120acf5f6a13407a1154cf26 Mon Sep 17 00:00:00 2001 From: Mitch Garnaat Date: Mon, 3 Jun 2019 06:27:21 -0700 Subject: [PATCH 3/6] Remove commented line. --- packages/jsii-pacmak/lib/targets/python.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/jsii-pacmak/lib/targets/python.ts b/packages/jsii-pacmak/lib/targets/python.ts index 7e56666355..5a4e61d75e 100644 --- a/packages/jsii-pacmak/lib/targets/python.ts +++ b/packages/jsii-pacmak/lib/targets/python.ts @@ -479,8 +479,6 @@ abstract class BaseMethod implements PythonBase { } code.line(`${methodPrefix}jsii.${this.jsiiMethod}(${jsiiMethodParams.join(", ")}, [${modifiedParamNames.join(", ")}])`); - - // code.line(`${methodPrefix}jsii.${this.jsiiMethod}(${jsiiMethodParams.join(", ")}, [${paramNames.join(", ")}])`); } private getLiftedProperties(resolver: TypeResolver): spec.Property[] { From e61d81535998e43879d273ac2329f8292847a5a1 Mon Sep 17 00:00:00 2001 From: Mitch Garnaat Date: Mon, 3 Jun 2019 06:59:11 -0700 Subject: [PATCH 4/6] Cleaning up code --- packages/jsii-pacmak/lib/targets/python.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/jsii-pacmak/lib/targets/python.ts b/packages/jsii-pacmak/lib/targets/python.ts index 5a4e61d75e..efa6ffc478 100644 --- a/packages/jsii-pacmak/lib/targets/python.ts +++ b/packages/jsii-pacmak/lib/targets/python.ts @@ -473,12 +473,11 @@ abstract class BaseMethod implements PythonBase { } // If the args are variadic, expand the last one - const modifiedParamNames: string[] = paramNames.slice(); if (this.parameters.length >= 1 && this.parameters[this.parameters.length - 1].variadic) { - modifiedParamNames.push("*" + modifiedParamNames.pop()); + paramNames.push("*" + paramNames.pop()); } - code.line(`${methodPrefix}jsii.${this.jsiiMethod}(${jsiiMethodParams.join(", ")}, [${modifiedParamNames.join(", ")}])`); + code.line(`${methodPrefix}jsii.${this.jsiiMethod}(${jsiiMethodParams.join(", ")}, [${paramNames.join(", ")}])`); } private getLiftedProperties(resolver: TypeResolver): spec.Property[] { From 137ff9024cf8eaf66af03268f65d223a07b525ca Mon Sep 17 00:00:00 2001 From: Mitch Garnaat Date: Mon, 3 Jun 2019 07:28:03 -0700 Subject: [PATCH 5/6] Update the expected output --- .../expected.jsii-calc/python/src/jsii_calc/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/python/src/jsii_calc/__init__.py b/packages/jsii-pacmak/test/expected.jsii-calc/python/src/jsii_calc/__init__.py index d2d53fec76..cc3a369245 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/python/src/jsii_calc/__init__.py +++ b/packages/jsii-pacmak/test/expected.jsii-calc/python/src/jsii_calc/__init__.py @@ -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"): @@ -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]: @@ -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"): From da11621cffd344c7c7b6455e3099012a903a0ee7 Mon Sep 17 00:00:00 2001 From: Mitch Garnaat Date: Mon, 3 Jun 2019 11:17:35 -0700 Subject: [PATCH 6/6] Further simplification of code --- packages/jsii-pacmak/lib/targets/python.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/jsii-pacmak/lib/targets/python.ts b/packages/jsii-pacmak/lib/targets/python.ts index efa6ffc478..40e411fa2b 100644 --- a/packages/jsii-pacmak/lib/targets/python.ts +++ b/packages/jsii-pacmak/lib/targets/python.ts @@ -467,14 +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)); - } - - // If the args are variadic, expand the last one - if (this.parameters.length >= 1 && this.parameters[this.parameters.length - 1].variadic) { - paramNames.push("*" + paramNames.pop()); + paramNames.push((param.variadic ? '*' : '') + toPythonParameterName(param.name)); } code.line(`${methodPrefix}jsii.${this.jsiiMethod}(${jsiiMethodParams.join(", ")}, [${paramNames.join(", ")}])`);