You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sorry for the lengthy writeup here. I wanted to provide enough context to enable a discussion about this issue because I'm not 100% sure of the right solution.
/** * Try to place tasks spread across instance attributes. * * You can use one of the built-in attributes found on `BuiltInAttributes` * or supply your own custom instance attributes. If more than one attribute * is supplied, spreading is done in order. * * @default attributes instanceId * @deprecated Use addPlacementStrategies() instead. */publicplaceSpreadAcross(...fields: string[]){if(fields.length===0){this.addPlacementStrategies(PlacementStrategy.spreadAcrossInstances());}else{this.addPlacementStrategies(PlacementStrategy.spreadAcross(...fields));}}
The problem was that the tuple containing fields was then passed directly to the Python runtime and it didn't know how to deal with it. The fix was to change the Python code to this:
This expanded the tuple and fixed the problem. This was accomplished by changing the Python generator code as shown in #513. The change was very simple, just check if the parameter was variadic when generating the call to JSII and, if so, expand the tuple.
// If the last arg is variadic, expand the tupleconstparamNames: string[]=[];for(constparamofthis.parameters){paramNames.push((param.variadic ? '*' : '')+toPythonParameterName(param.name));
/** * Add one or more port mappings to this container */publicaddPortMappings(...portMappings: PortMapping[]){this.portMappings.push(...portMappings.map(pm=>{if(this.taskDefinition.networkMode===NetworkMode.AwsVpc||this.taskDefinition.networkMode===NetworkMode.Host){if(pm.containerPort!==pm.hostPort&&pm.hostPort!==undefined){thrownewError(`Host port ${pm.hostPort} does not match container port ${pm.containerPort}.`);}}if(this.taskDefinition.networkMode===NetworkMode.Bridge){if(pm.hostPort===undefined){pm={
...pm,hostPort: 0};}}returnpm;}));}
@jsii.member(jsii_name="addPortMappings")defadd_port_mappings(self, *, container_port: jsii.Number, host_port: typing.Optional[jsii.Number]=None, protocol: typing.Optional["Protocol"]=None) ->None:
"""Add one or more port mappings to this container. Arguments: portMappings: - containerPort: Port inside the container. hostPort: Port on the host. In AwsVpc or Host networking mode, leave this out or set it to the same value as containerPort. In Bridge networking mode, leave this out or set it to non-reserved non-ephemeral port. protocol: Protocol. Default: Tcp Stability: experimental """port_mappings: PortMapping= {"containerPort": container_port}
ifhost_portisnotNone:
port_mappings["hostPort"] =host_portifprotocolisnotNone:
port_mappings["protocol"] =protocolreturnjsii.invoke(self, "addPortMappings", [*port_mappings])
In this case, the parameters are passed as keyword args (the bare asterisk as the first param forces that), the PortMapping structure is built and then wrapped in a list and unpacked. This will result in a list of the keys present in the dict being passed to jsii.invoke, e.g. ['containerPort', 'hostPort'] which results in the following error:
Traceback (most recent call last):
File "app.py", line 40, in <module>
protocol=ecs.Protocol.Tcp
File "/Users/garnaat/projects/cdkdev/aws-cdk-examples/python/ecs/ecs-service-with-task-placement/.env/lib/python3.7/site-packages/aws_cdk/aws_ecs/__init__.py", line 1869, in add_port_mappings
return jsii.invoke(self, "addPortMappings", [*port_mappings])
File "/Users/garnaat/projects/cdkdev/aws-cdk-examples/python/ecs/ecs-service-with-task-placement/.env/lib/python3.7/site-packages/jsii/_kernel/__init__.py", line 104, in wrapped
return _recursize_dereference(kernel, fn(kernel, *args, **kwargs))
File "/Users/garnaat/projects/cdkdev/aws-cdk-examples/python/ecs/ecs-service-with-task-placement/.env/lib/python3.7/site-packages/jsii/_kernel/__init__.py", line 258, in invoke
args=_make_reference_for_native(self, args),
File "/Users/garnaat/projects/cdkdev/aws-cdk-examples/python/ecs/ecs-service-with-task-placement/.env/lib/python3.7/site-packages/jsii/_kernel/providers/process.py", line 346, in invoke
return self._process.send(request, InvokeResponse)
File "/Users/garnaat/projects/cdkdev/aws-cdk-examples/python/ecs/ecs-service-with-task-placement/.env/lib/python3.7/site-packages/jsii/_kernel/providers/process.py", line 316, in send
raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Expected object reference, got "containerPort"
Subprocess exited with error 1
Simply removing the * in the call to jsii.invoke fixes the problem but this raises two questions.
How to discriminate between scalar and non-scalar arguments in the Python code generator.
Isn't this signature for add_port_mappings wrong? You should be able to pass a list of PortMapping objects but that is not possible here.
The text was updated successfully, but these errors were encountered:
This change addresses 4 issues:
- Structs now use idiomatic (snake_case) capitalization of fields
(instead of JSII-inherited camelCase).
- IDE support -- replace TypedDict usage with regular classes. This
makes it so that we can't use dicts anymore, but mypy support in
IDEs wasn't great and by using POPOs (Plain Old Python Objects)
IDEs get their support back.
- Structs in a variadic argument use to be incorrectly lifted to
keyword arguments, this no longer happens.
- Stop emitting "Stable" stabilities in docstrings, "Stable" is implied.
In order to make this change, I've had to make `jsii-pacmak` depend on
`jsii-reflect`. This is the proper layering of libraries anyway, since
`jsii-reflect` adds much-needed--and otherwise
duplicated--interpretation of the spec.
Complete refactoring of `jsii-pacmak` to build on `jsii-reflect` is too
much work right now, however, so I've added "escape hatching" where
generators can take advantage of the power of jsii-reflect if they want
to, but most of the code still works on the raw spec level.
Added a refactoring where we load the assembly once and reuse the same
instance for all generators, instead of loading the assembly for every
generator. Assembly-loading, especially with a lot of dependencies, takes
a non-negligible amount of time, so this has the side effect of making
the packaging step faster (shaves off 100 packages * 3 targets * a
couple of seconds).
Fixes#537Fixes#577Fixes#578Fixes#588
Sorry for the lengthy writeup here. I wanted to provide enough context to enable a discussion about this issue because I'm not 100% sure of the right solution.
There was a previous issue (#483) with variadic arguments in Python. This issue involved methods with scalar variadic parameters such as https://github.com/awslabs/aws-cdk/blob/v0.34.0/packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts#L168:
This was mapped to a Python signature like this:
The problem was that the tuple containing
fields
was then passed directly to the Python runtime and it didn't know how to deal with it. The fix was to change the Python code to this:This expanded the tuple and fixed the problem. This was accomplished by changing the Python generator code as shown in #513. The change was very simple, just check if the parameter was variadic when generating the call to JSII and, if so, expand the tuple.
This change, however, introduced another issue. This issue shows up, among other places, in https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ecs.ContainerDefinition.html#addportmappingsportmappings-void which looks like this:
In this case the variadic argument is not a scalar value but an object conforming to this interface https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ecs.PortMapping.html. The generated Python code looks like this:
In this case, the parameters are passed as keyword args (the bare asterisk as the first param forces that), the PortMapping structure is built and then wrapped in a list and unpacked. This will result in a list of the keys present in the dict being passed to
jsii.invoke
, e.g.['containerPort', 'hostPort']
which results in the following error:Simply removing the
*
in the call tojsii.invoke
fixes the problem but this raises two questions.add_port_mappings
wrong? You should be able to pass a list ofPortMapping
objects but that is not possible here.The text was updated successfully, but these errors were encountered: