jsii icon indicating copy to clipboard operation
jsii copied to clipboard

Lazy still broken in Python

Open serverlessunicorn opened this issue 5 years ago • 4 comments

:bug: Bug Report

The following fails on CDK 1.49.1: class Foo: def produce(me, context): return self.api.rest_api_id lazy = core.Lazy.string_value(Foo()) with the error: AttributeError: type object 'Foo' has no attribute 'jsii_type'

Possibly recurrence or variant of 807 (https://github.com/aws/jsii/issues/807)

Affected Languages

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

General Information

  • JSII Version: <!-- Output of jsii --version --> CDK version is 1.49.1
  • Platform: Darwin MacBook-Pro.local 19.5.0 Darwin Kernel Version 19.5.0: Tue May 26 20:41:44 PDT 2020; root:xnu-6153.121.2~2/RELEASE_X86_64 x86_64

What is the problem?

Lazy in AWS CDK Python appears to have a jsii serialization problem; see discussion in 807.

Verbose Log

Traceback (most recent call last): File "deploy.py", line 80, in deployer.deploy() File "deploy.py", line 63, in deploy cdk = cdkstack.CdkStack(scope=app, id='VendiaShareDeployer', File "/Users/timwagner/Desktop/projects/vendia/src/share/venv/lib/python3.8/site-packages/jsii/_runtime.py", line 66, in call inst = super().call(*args, **kwargs) File "/Users/timwagner/Desktop/projects/vendia/src/share/server/deploy/cdkstack.py", line 32, in init lazy = core.Lazy.string_value(Foo()) File "/Users/timwagner/Desktop/projects/vendia/src/share/venv/lib/python3.8/site-packages/aws_cdk/core/init.py", line 6313, in string_value return jsii.sinvoke(cls, "stringValue", [producer, options]) File "/Users/timwagner/Desktop/projects/vendia/src/share/venv/lib/python3.8/site-packages/jsii/_kernel/init.py", line 113, in wrapped return _recursize_dereference(kernel, fn(kernel, *args, **kwargs)) File "/Users/timwagner/Desktop/projects/vendia/src/share/venv/lib/python3.8/site-packages/jsii/_kernel/init.py", line 311, in sinvoke args=_make_reference_for_native(self, args), File "/Users/timwagner/Desktop/projects/vendia/src/share/venv/lib/python3.8/site-packages/jsii/_kernel/init.py", line 124, in _make_reference_for_native return [_make_reference_for_native(kernel, i) for i in d] File "/Users/timwagner/Desktop/projects/vendia/src/share/venv/lib/python3.8/site-packages/jsii/_kernel/init.py", line 124, in return [_make_reference_for_native(kernel, i) for i in d] File "/Users/timwagner/Desktop/projects/vendia/src/share/venv/lib/python3.8/site-packages/jsii/_kernel/init.py", line 151, in _make_reference_for_native kernel.create(d.class, d) File "/Users/timwagner/Desktop/projects/vendia/src/share/venv/lib/python3.8/site-packages/jsii/_kernel/init.py", line 226, in create fqn=klass.jsii_type or "Object", AttributeError: type object 'Foo' has no attribute 'jsii_type'

serverlessunicorn avatar Jul 09 '20 00:07 serverlessunicorn

Ah, so perhaps this is an AWS CDK issue: The example at https://docs.aws.amazon.com/cdk/latest/guide/tokens.html (with Python selected) fails as written, but if you use the example from #807 and add @jsii.implements(core.IStringProducer) as an annotation to the producing class, it succeeds with the workaround.

serverlessunicorn avatar Jul 09 '20 00:07 serverlessunicorn

This issue has not received any attention in 2 years. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

github-actions[bot] avatar Jun 29 '23 06:06 github-actions[bot]

This seems stil an issue. Can you reopen this bug?

mschwab12 avatar Feb 17 '24 23:02 mschwab12

I found a workaround for this bug. The following allows for synth time creation of any constructs with a callback:

class LazyCallback:
    __jsii_type__ = None

    def __init__(self, callback):
        self.callback = callback

    @jsii.member(jsii_name='produce')
    def produce(self, context):
        return self.callback()

teemu-laakso avatar May 16 '24 13:05 teemu-laakso