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

creating root construct in Python #1063

Open
rittneje opened this issue May 22, 2022 · 4 comments
Open

creating root construct in Python #1063

rittneje opened this issue May 22, 2022 · 4 comments

Comments

@rittneje
Copy link

I installed constructs version 10.1.14 via pip. I am using Python 3.9.12 and NodeJS 16.15.0. I tried to create a root construct, but it throws an exception.

import constructs

constructs.Construct(None, '')

jsii.errors.JavaScriptError:
Error: Got 'undefined' for non-optional instance of {"docs":{"summary":"The scope in which to define this construct."},"name":"scope","type":{"fqn":"constructs.Construct"}}
at nullAndOk (/tmp/tmpnx__46nv/lib/program.js:9608:23)
at Object.deserialize (/tmp/tmpnx__46nv/lib/program.js:9382:25)
at Kernel._toSandbox (/tmp/tmpnx__46nv/lib/program.js:8771:69)
at /tmp/tmpnx__46nv/lib/program.js:8819:42
at Array.map ()
at Kernel._boxUnboxParameters (/tmp/tmpnx__46nv/lib/program.js:8819:27)
at Kernel._toSandboxValues (/tmp/tmpnx__46nv/lib/program.js:8805:29)
at /tmp/tmpnx__46nv/lib/program.js:8420:75
at Kernel._wrapSandboxCode (/tmp/tmpnx__46nv/lib/program.js:8848:24)
at Kernel._create (/tmp/tmpnx__46nv/lib/program.js:8420:34)

Is this a bug? Or is there some other way I'm supposed to create a root construct in Python?

@Chriscbr
Copy link
Contributor

Chriscbr commented May 25, 2022

I'm not sure if there's a way to do this in non-TypeScript languages right now. @rittneje What kind of use case did you have in mind?

I wonder if it might make some sense to allow creating root constructs natively somehow, or to cast things into an "any" type in JSII languages? cc @rix0rrr @RomainMuller

@rittneje
Copy link
Author

@Chriscbr I don't have a particular use case per se, I was just trying to experiment with this library and immediately hit a wall.

@ThomasSteinbach
Copy link
Contributor

ThomasSteinbach commented Jun 3, 2022

@rittneje I think this is an https://github.com/aws/jsii issue.

Looking into the code of Construct the constructor does not allow an undefined scope and id. However in TypeScript you can forcefully tell TypeScript the type of a value - even if that type is wrong. The App class of CDK does this to create the root construct with an undefined scope, by declaring undefined as type any.

  /**
   * Initializes a CDK application.
   * @param props initialization properties
   */
  constructor(props: AppProps = {}) {
    super(undefined as any, '', {
...

Doing that "trick" you can still tell every user of the Construct class that the scope must not be null/undefined. In TypeScript you use this value as type thing only "if you know better" how things work, which is the case for the App class.

I think even if you could express this in Python, then jsii has to translate this expression back to TypeScript.

IMHO you could close the issue here and suggest this functionality in the jssi repo.

@pwrmiller
Copy link
Contributor

This would have been useful for testing. It might be useful to expose a root construct generator as part of this library. What do the maintainers think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants