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

How to implement an interface in Python? #738

Closed
1 of 4 tasks
jonnyyu opened this issue Aug 26, 2019 · 3 comments
Closed
1 of 4 tasks

How to implement an interface in Python? #738

jonnyyu opened this issue Aug 26, 2019 · 3 comments
Assignees
Labels
documentation This is a problem with documentation. guidance Question that needs advice or information. language/python Related to Python bindings

Comments

@jonnyyu
Copy link

jonnyyu commented Aug 26, 2019

❓ Guidance

Affected Languages

  • TypeScript or Javascript
  • Python
  • Java
  • .NET (C#, F#, ...)

General Information

  • JSII Version: 0.14.3 (CDK 1.3.0)
  • Platform: macOS 10.15

The Question

Hi, I need some help on how to implement a JSII interface in Python.
I'm trying to write my own Aurora Serverless Resource based on rds.DatabaseCluster class.
The problem is I'm writing code in Python which needs go through jsii,
The concised code is like

@jsii.implements(secretsmanager.ISecretAttachmentTarget)
class AuroraServerless(core.Resource):
	def __init__(self, scope: core.Construct, id: str,**kwargs):
		<some init code>

		# FIXME: cdk gives error 
        # jsii.errors.JSIIError: props.target.asSecretAttachmentTarget is not a function
        if secret:
           self.secret = secret.add_target_attachment('AttachedSecret', target=self)
        
	@jsii.member(jsii_name="asSecretAttachmentTarget")
    def as_secret_attachment_target(self) -> secretsmanager.SecretAttachmentTargetProps:
            return secretsmanager.SecretAttachmentTargetProps(
                target_id=self.cluster_identifier,
                target_type=secretsmanager.AttachmentTargetType.CLUSTER
            )

complete code, see: https://gist.github.com/jonnyyu/980dfa9e40c7401130d78fe153b4d629

and when I run cdk ls, I got

jsii.errors.JavaScriptError: 
  Error: '@aws-cdk/core.Resource@10053'
      at completeCallback (/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6156:27)
      at KernelHost.callbackHandler (/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6145:16)
      at Kernel.callbackHandler (/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6129:59)
      at Resource.value (/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6848:41)
      at new SecretTargetAttachment (/private/var/folders/16/bz04xgld25s32g4s76vch7d80000gn/T/jsii-kernel-TqxA0P/node_modules/@aws-cdk/aws-secretsmanager/lib/secret.js:136:36)
      at DatabaseSecret.addTargetAttachment (/private/var/folders/16/bz04xgld25s32g4s76vch7d80000gn/T/jsii-kernel-TqxA0P/node_modules/@aws-cdk/aws-secretsmanager/lib/secret.js:97:16)
      at /Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6498:51
      at Kernel._wrapSandboxCode (/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7134:19)
      at /Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6498:25
      at Kernel._ensureSync (/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7102:20)
      at Kernel.invoke (/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6497:26)
      at KernelHost.processRequest (/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6191:28)
      at KernelHost.run (/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6137:14)
      at /Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6137:45
      at KernelHost.processRequest (/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6233:16)
      at KernelHost.run (/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6137:14)
      at /Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6137:45
      at KernelHost.processRequest (/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6233:16)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "app.py", line 16, in <module>
    db_stack = DBStack(app, 'task-service-db', vpc=base_stack.vpc, env=account_region_env)
  File "/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_runtime.py", line 66, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/Volumes/DATA/ci/aws/task-service/deploy/cdk/src/db_stack.py", line 27, in __init__
    max_capacity=4
  File "/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_runtime.py", line 66, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/Volumes/DATA/ci/aws/task-service/deploy/cdk/src/aurora_serverless.py", line 89, in __init__
    self.secret = secret.add_target_attachment('AttachedSecret', target=self)
  File "/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/aws_cdk/aws_secretsmanager/__init__.py", line 1162, in add_target_attachment
    return jsii.invoke(self, "addTargetAttachment", [id, options])
  File "/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_kernel/__init__.py", line 104, in wrapped
    return _recursize_dereference(kernel, fn(kernel, *args, **kwargs))
  File "/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_kernel/__init__.py", line 271, in invoke
    return _callback_till_result(self, response, InvokeResponse)
  File "/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_kernel/__init__.py", line 157, in _callback_till_result
    response = kernel.sync_complete(response.cbid, str(exc), None, response_type)
  File "/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_kernel/__init__.py", line 310, in sync_complete
    response_type=response_type
  File "/Volumes/DATA/ci/aws/task-service/.env/lib/python3.7/site-packages/jsii/_kernel/providers/process.py", line 367, in sync_complete
    resp = self._process.send(_CompleteRequest(complete=request), response_type)
  File "/Volumes/DATA/ci/aws/task-service/.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: '@aws-cdk/core.Resource@10053'

Thanks for your help!

@jonnyyu jonnyyu added guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. labels Aug 26, 2019
@skinny85
Copy link
Contributor

skinny85 commented Aug 26, 2019

Related issue: aws/aws-cdk#3744

@RomainMuller RomainMuller added language/python Related to Python bindings and removed needs-triage This issue or PR still needs to be triaged. labels Aug 28, 2019
@RomainMuller RomainMuller added the documentation This is a problem with documentation. label Aug 28, 2019
@RomainMuller RomainMuller self-assigned this Aug 28, 2019
@RomainMuller RomainMuller modified the milestones: Python Support, User & Developer Documentation Aug 28, 2019
@RomainMuller
Copy link
Contributor

I think the culprit is that you need to declare properties with getter and setter methods instead of just assigning those in your constructor, and annotate those with the @property (for the getter) and @<property_name>.setter.

Here's an example of this from our test suite:

class SuperSyncVirtualMethods(SyncVirtualMethods):
@property
def the_property(self):
return f"super:{super().the_property}"
@the_property.setter
def the_property(self, value):
super().the_property = value

Obviously - this is severely under-documented and we will be addressing this.

@RomainMuller
Copy link
Contributor

This relates to #576

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This is a problem with documentation. guidance Question that needs advice or information. language/python Related to Python bindings
Projects
None yet
Development

No branches or pull requests

4 participants