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

Closed
rittneje opened this issue May 22, 2022 · 5 comments · Fixed by #2496, #2500, grommir/cdk-test#8 or gentksb/splitwise_automation#15 · May be fixed by peterboni/jarvis-api#5
Closed

creating root construct in Python #1063

rittneje opened this issue May 22, 2022 · 5 comments · Fixed by #2496, #2500, grommir/cdk-test#8 or gentksb/splitwise_automation#15 · May be fixed by peterboni/jarvis-api#5

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?

@mrgrain
Copy link
Contributor

mrgrain commented Oct 10, 2024

A static helper to create a root construct should work. PRs are welcome!

@mergify mergify bot closed this as completed in #2496 Oct 11, 2024
@mergify mergify bot closed this as completed in bb7f237 Oct 11, 2024
@mrgrain mrgrain reopened this Oct 11, 2024
mrgrain added a commit that referenced this issue Oct 11, 2024
The previous attempt in #2499 caused the static helper to be added to every construct, which was unintentional and caused side-effects. Instead export a class that does the same.

Fixes #1063
@mergify mergify bot closed this as completed in #2500 Oct 14, 2024
@mergify mergify bot closed this as completed in 892bb11 Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment